• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang webmachine.NewMockResponseWriter函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/pomack/webmachine/go/webmachine.NewMockResponseWriter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewMockResponseWriter函数的具体用法?Golang NewMockResponseWriter怎么用?Golang NewMockResponseWriter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewMockResponseWriter函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestUpdateUserAccountMissingSignature

func TestUpdateUserAccountMissingSignature(t *testing.T) {
    ds, wm := initializeUpdateUserAccountDS()
    gw, _ := ds.FindUserAccountByUsername("firstpresident")
    otherUser, _ := ds.FindUserAccountByUsername("secondpresident")
    anobj, _ := jsonhelper.Marshal(otherUser)
    jsonobj := anobj.(jsonhelper.JSONObject)
    jsonobj.Set("name", "GW")
    jsonobj.Set("email", "[email protected]")
    jsonobj.Set("address", "Pre-White House")
    otherUser = new(dm.User)
    otherUser.InitFromJSONObject(jsonobj)
    jsonbuf, _ := json.Marshal(jsonobj)
    req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/account/user/update/" + gw.Id, bytes.NewBuffer(jsonbuf))
    req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept-Charset", "utf-8")
    req.Header.Set("Accept-Encoding", "identity")
    req.Header.Set("Accept-Language", "en-us")
    req.Header.Set("Connection", "close")
    resp := webmachine.NewMockResponseWriter(req)
    wm.ServeHTTP(resp, req)
    if resp.StatusCode != http.StatusUnauthorized {
        t.Error("Expected ", http.StatusUnauthorized, " status code but received ", resp.StatusCode)
    }
}
开发者ID:pombredanne,项目名称:dsocial.go,代码行数:25,代码来源:update_test.go


示例2: TestUpdateUserAccountInvalidUserId

func TestUpdateUserAccountInvalidUserId(t *testing.T) {
    ds, wm := initializeUpdateUserAccountDS()
    gw, _ := ds.FindUserAccountByUsername("firstpresident")
    accessKeys, _, _ := ds.RetrieveUserKeys(gw.Id, nil, 1)
    accessKey := accessKeys[0]
    otherUser, _ := ds.FindUserAccountByUsername("secondpresident")
    anobj, _ := jsonhelper.Marshal(otherUser)
    jsonobj := anobj.(jsonhelper.JSONObject)
    jsonobj.Set("name", "Tom J")
    jsonobj.Set("email", "[email protected]")
    jsonobj.Set("address", "White House")
    otherUser = new(dm.User)
    otherUser.InitFromJSONObject(jsonobj)
    jsonbuf, _ := json.Marshal(jsonobj)
    req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/account/user/update/sdflsjflsjfslf", bytes.NewBuffer(jsonbuf))
    req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept-Charset", "utf-8")
    req.Header.Set("Accept-Encoding", "identity")
    req.Header.Set("Accept-Language", "en-us")
    req.Header.Set("Connection", "close")
    apiutil.NewSigner(accessKey.Id, accessKey.PrivateKey).SignRequest(req, 0)
    resp := webmachine.NewMockResponseWriter(req)
    wm.ServeHTTP(resp, req)
    if resp.StatusCode != http.StatusNotFound {
        t.Error("Expected ", http.StatusNotFound, " status code but received ", resp.StatusCode)
    }
}
开发者ID:pombredanne,项目名称:dsocial.go,代码行数:28,代码来源:update_test.go


示例3: TestCreateUserAccount

func TestCreateUserAccount(t *testing.T) {
	ds := inmemory.NewInMemoryDataStore()
	wm := webmachine.NewWebMachine()
	wm.AddRouteHandler(account.NewCreateAccountRequestHandler(ds, ds))
	buf := bytes.NewBufferString(`{"role": 9999999999999999, "name": "George Washington", "username": "firstpresident", "email":"[email protected]", "phone_number": "+1-405-555-5555", "address": "Valley Forge"}`)
	oldUser := new(dm.User)
	json.Unmarshal(buf.Bytes(), &oldUser)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/account/user/create/", buf)
	req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept-Charset", "utf-8")
	req.Header.Set("Accept-Encoding", "identity")
	req.Header.Set("Accept-Language", "en-us")
	req.Header.Set("Connection", "close")
	resp := webmachine.NewMockResponseWriter(req)
	reqb, _ := httputil.DumpRequest(req, true)
	wm.ServeHTTP(resp, req)
	if resp.StatusCode != 200 {
		t.Error("Expected 200 status code but received ", resp.StatusCode)
	}
	if resp.Header().Get("Content-Type") != req.Header.Get("Accept") {
		t.Error("Expected Content-Type \"", req.Header.Get("Accept"), "\" but received ", resp.Header().Get("Content-Type"))
	}
	user := new(dm.User)
	obj := jsonhelper.NewJSONObject()
	err := json.Unmarshal(resp.Buffer.Bytes(), &obj)
	user.InitFromJSONObject(obj.GetAsObject("result").GetAsObject("user"))
	if err != nil {
		t.Error("Error while unmarshaling JSON: ", err.Error())
	}
	if obj.GetAsString("status") != "success" {
		t.Error("Expected status = \"success\", but was \"", obj.GetAsString("status"), "\"")
	}
	if user.Name != oldUser.Name {
		t.Logf("Request was\n%s\n================\n", string(reqb))
		t.Log("Response is:\n", resp.String(), "\n\n")
		t.Error("Expected name = \"", oldUser.Name, "\", but was ", user.Name)
	}
	if user.Username != oldUser.Username {
		t.Error("Expected username = \"", oldUser.Username, "\", but was ", user.Username)
	}
	if user.Email != oldUser.Email {
		t.Error("Expected email = \"", oldUser.Email, "\", but was ", user.Email)
	}
	if user.PhoneNumber != oldUser.PhoneNumber {
		t.Error("Expected phone_number = \"", oldUser.PhoneNumber, "\", but was ", user.PhoneNumber)
	}
	if user.Address != oldUser.Address {
		t.Error("Expected address = \"", oldUser.Address, "\", but was ", user.Address)
	}
	if user.Role != dm.ROLE_STANDARD {
		t.Error("Expected role = ", dm.ROLE_STANDARD, " but was ", user.Role)
	}
	if user.Id == "" {
		t.Error("Expected id to be populated, but was empty")
	}
}
开发者ID:pomack,项目名称:dsocial.go,代码行数:57,代码来源:create_test.go


示例4: TestAuthSetPasswordAdmin

func TestAuthSetPasswordAdmin(t *testing.T) {
	ds, wm := initializeAuthUserAccountDS()
	user, _ := ds.FindUserAccountByUsername("firstpresident")
	accessKeys, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000)
	if len(accessKeys) != 1 {
		t.Error("Expected to find 1 access key stored, but found", len(accessKeys))
	}
	accessKey := accessKeys[0]
	jsonobj := jsonhelper.NewJSONObject()
	jsonobj.Set("password", "hi ho hi ho")
	jsonbuf, _ := json.Marshal(jsonobj)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/auth/set_password/", bytes.NewBuffer(jsonbuf))
	req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept-Charset", "utf-8")
	req.Header.Set("Accept-Encoding", "identity")
	req.Header.Set("Accept-Language", "en-us")
	req.Header.Set("Connection", "close")
	apiutil.NewSigner(accessKey.Id, accessKey.PrivateKey).SignRequest(req, 0)
	reqbytes, _ := httputil.DumpRequest(req, true)
	t.Log("Request is:\n", string(reqbytes), "\n\n")
	resp := webmachine.NewMockResponseWriter(req)
	wm.ServeHTTP(resp, req)
	t.Log("Response is:\n", resp.String(), "\n\n")
	if resp.StatusCode != http.StatusOK {
		t.Error("Expected ", http.StatusOK, " status code but received ", resp.StatusCode)
	}
	if resp.Header().Get("Content-Type") != req.Header.Get("Accept") {
		t.Error("Expected Content-Type \"", req.Header.Get("Accept"), "\" but received ", resp.Header().Get("Content-Type"))
	}
	obj := jsonhelper.NewJSONObject()
	err := json.Unmarshal(resp.Buffer.Bytes(), &obj)
	if err != nil {
		t.Error("Unable to unmarshal setPassword response due to error:", err.Error())
	}
	if status := obj.GetAsString("status"); status != "success" {
		t.Error("Expected successful operation, but had status:", status)
	}
	result := obj.GetAsObject("result")
	if message := result.GetAsString("message"); message != "password changed" {
		t.Error("Expected message == \"password changed\", but was \"", message, "\"")
	}
	user2 := result.GetAsObject("user")
	uid := user2.GetAsString("id")
	if uid != user.Id {
		t.Error("Expected user id of", user.Id, ", but was", uid)
	}
	accessKeys2, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000)
	if len(accessKeys2) != 1 {
		t.Error("Expected to find one access key stored, but found", len(accessKeys))
	}
}
开发者ID:pomack,项目名称:dsocial.go,代码行数:51,代码来源:set_password_test.go


示例5: TestAuthLoginNoPassword

func TestAuthLoginNoPassword(t *testing.T) {
	ds, wm := initializeAuthUserAccountDS()
	user, _ := ds.FindUserAccountByUsername("firstpresident")
	accessKeys, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000)
	if len(accessKeys) == 0 {
		t.Error("Expected to find at least one access key stored.")
	}
	jsonobj := jsonhelper.NewJSONObject()
	jsonobj.Set("username", user.Username)
	jsonbuf, _ := json.Marshal(jsonobj)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/auth/login/", bytes.NewBuffer(jsonbuf))
	req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept-Charset", "utf-8")
	req.Header.Set("Accept-Encoding", "identity")
	req.Header.Set("Accept-Language", "en-us")
	req.Header.Set("Connection", "close")
	reqbytes, _ := httputil.DumpRequest(req, true)
	t.Log("Request is:\n", string(reqbytes), "\n\n")
	resp := webmachine.NewMockResponseWriter(req)
	wm.ServeHTTP(resp, req)
	t.Log("Response is:\n", resp.String(), "\n\n")
	if resp.StatusCode != http.StatusUnauthorized {
		t.Error("Expected ", http.StatusUnauthorized, " status code but received ", resp.StatusCode)
	}
	if resp.Header().Get("Content-Type") != req.Header.Get("Accept") {
		t.Error("Expected Content-Type \"", req.Header.Get("Accept"), "\" but received ", resp.Header().Get("Content-Type"))
	}
	obj := jsonhelper.NewJSONObject()
	err := json.Unmarshal(resp.Buffer.Bytes(), &obj)
	if err != nil {
		t.Error("Unable to unmarshal login response due to error:", err.Error())
	}
	if status := obj.GetAsString("status"); status != "error" {
		t.Error("Expected error operation, but had status:", status)
	}
	result := obj.GetAsObject("result")
	if result.Len() != 1 {
		t.Error("Expected a result object with 1 entry, but has", result.Len(), "entries as:", result)
	}
	if password := result.GetAsArray("password"); len(password) != 1 || password[0] != auth.ERR_MUST_SPECIFY_PASSWORD.Error() {
		t.Error("Expected one error for missing password, but was", result)
	}
	if message := obj.GetAsString("message"); message != auth.ERR_VALUE_ERRORS.Error() {
		t.Error("Expected ERR_VALUE_ERRORS for message, but was", message)
	}
	if accessKeys2, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000); len(accessKeys2) != 1 {
		t.Error("Expected 1 access key after logging in, but found", len(accessKeys2))
	}
}
开发者ID:pomack,项目名称:dsocial.go,代码行数:50,代码来源:login_test.go


示例6: TestViewUserAccountMissingSignature

func TestViewUserAccountMissingSignature(t *testing.T) {
	ds, wm := initializeViewUserAccountDS()
	gw, _ := ds.FindUserAccountByUsername("firstpresident")
	req, _ := http.NewRequest(webmachine.GET, "http://localhost/api/v1/json/account/user/view/"+gw.Id, nil)
	req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept-Charset", "utf-8")
	req.Header.Set("Accept-Encoding", "identity")
	req.Header.Set("Accept-Language", "en-us")
	req.Header.Set("Connection", "close")
	resp := webmachine.NewMockResponseWriter(req)
	wm.ServeHTTP(resp, req)
	if resp.StatusCode != http.StatusUnauthorized {
		t.Error("Expected ", http.StatusUnauthorized, " status code but received ", resp.StatusCode)
	}
}
开发者ID:pomack,项目名称:dsocial.go,代码行数:15,代码来源:view_test.go


示例7: TestCreateUserAccountMissingSeveralFields

func TestCreateUserAccountMissingSeveralFields(t *testing.T) {
	ds := inmemory.NewInMemoryDataStore()
	wm := webmachine.NewWebMachine()
	wm.AddRouteHandler(account.NewCreateAccountRequestHandler(ds, ds))
	buf := bytes.NewBufferString(`{"role": 9999999999999999, "name": "    ", "username": "\n\r\n", "email": "hi ho hi ho", "phone_number": "+1-405-555-5555", "address": "Valley Forge"}`)
	oldUser := new(dm.User)
	json.Unmarshal(buf.Bytes(), &oldUser)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/account/user/create/", buf)
	req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept-Charset", "utf-8")
	req.Header.Set("Accept-Encoding", "identity")
	req.Header.Set("Accept-Language", "en-us")
	req.Header.Set("Connection", "close")
	resp := webmachine.NewMockResponseWriter(req)
	wm.ServeHTTP(resp, req)
	if resp.StatusCode != 400 {
		t.Error("Expected 400 status code but received ", resp.StatusCode)
	}
	if resp.Header().Get("Content-Type") != req.Header.Get("Accept") {
		t.Error("Expected Content-Type \"", req.Header.Get("Accept"), "\" but received ", resp.Header().Get("Content-Type"))
	}
	obj := jsonhelper.NewJSONObject()
	err := json.Unmarshal(resp.Buffer.Bytes(), &obj)
	if err != nil {
		t.Error("Error while unmarshaling JSON: ", err.String())
	}
	if obj.GetAsString("status") != "error" {
		t.Error("Expected status = \"error\", but was \"", obj.GetAsString("status"), "\"")
	}
	result := obj.GetAsObject("result")
	if result == nil {
		t.Error("Expected result != nil, but was nil")
	} else {
		if result.GetAsArray("name").Len() == 0 {
			t.Error("Expected an error on attribute \"name\", but was not found")
		}
		if result.GetAsArray("username").Len() == 0 {
			t.Error("Expected an error on attribute \"username\", but was not found")
		}
		if result.GetAsArray("email").Len() == 0 {
			t.Error("Expected an error on attribute \"email\", but was not found")
		}
	}
}
开发者ID:pombredanne,项目名称:dsocial.go,代码行数:45,代码来源:create_test.go


示例8: TestViewUserAccountInvalidUserId

func TestViewUserAccountInvalidUserId(t *testing.T) {
	ds, wm := initializeViewUserAccountDS()
	gw, _ := ds.FindUserAccountByUsername("firstpresident")
	accessKeys, _, _ := ds.RetrieveUserKeys(gw.Id, nil, 1)
	accessKey := accessKeys[0]
	req, _ := http.NewRequest(webmachine.GET, "http://localhost/api/v1/json/account/user/view/sdflsjflsjfslf", nil)
	req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept-Charset", "utf-8")
	req.Header.Set("Accept-Encoding", "identity")
	req.Header.Set("Accept-Language", "en-us")
	req.Header.Set("Connection", "close")
	apiutil.NewSigner(accessKey.Id, accessKey.PrivateKey).SignRequest(req, 0)
	resp := webmachine.NewMockResponseWriter(req)
	wm.ServeHTTP(resp, req)
	if resp.StatusCode != http.StatusNotFound {
		t.Error("Expected ", http.StatusNotFound, " status code but received ", resp.StatusCode)
	}
}
开发者ID:pomack,项目名称:dsocial.go,代码行数:18,代码来源:view_test.go


示例9: TestAuthLoginNoUsername

func TestAuthLoginNoUsername(t *testing.T) {
	_, wm := initializeAuthUserAccountDS()
	jsonobj := jsonhelper.NewJSONObject()
	jsonobj.Set("password", "number two")
	jsonbuf, _ := json.Marshal(jsonobj)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/auth/login/", bytes.NewBuffer(jsonbuf))
	req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept-Charset", "utf-8")
	req.Header.Set("Accept-Encoding", "identity")
	req.Header.Set("Accept-Language", "en-us")
	req.Header.Set("Connection", "close")
	reqbytes, _ := httputil.DumpRequest(req, true)
	t.Log("Request is:\n", string(reqbytes), "\n\n")
	resp := webmachine.NewMockResponseWriter(req)
	wm.ServeHTTP(resp, req)
	t.Log("Response is:\n", resp.String(), "\n\n")
	if resp.StatusCode != http.StatusUnauthorized {
		t.Error("Expected ", http.StatusUnauthorized, " status code but received ", resp.StatusCode)
	}
	if resp.Header().Get("Content-Type") != req.Header.Get("Accept") {
		t.Error("Expected Content-Type \"", req.Header.Get("Accept"), "\" but received ", resp.Header().Get("Content-Type"))
	}
	obj := jsonhelper.NewJSONObject()
	err := json.Unmarshal(resp.Buffer.Bytes(), &obj)
	if err != nil {
		t.Error("Unable to unmarshal login response due to error:", err.Error())
	}
	if status := obj.GetAsString("status"); status != "error" {
		t.Error("Expected error operation, but had status:", status)
	}
	result := obj.GetAsObject("result")
	if result.Len() != 1 {
		t.Error("Expected a result object with 1 entry, but has", result.Len(), "entries as:", result)
	}
	if username := result.GetAsArray("username"); len(username) != 1 || username[0] != auth.ERR_MUST_SPECIFY_USERNAME.Error() {
		t.Error("Expected one error for missing username, but was", result)
	}
	if message := obj.GetAsString("message"); message != auth.ERR_VALUE_ERRORS.Error() {
		t.Error("Expected ERR_VALUE_ERRORS for message, but was", message)
	}
}
开发者ID:pomack,项目名称:dsocial.go,代码行数:42,代码来源:login_test.go


示例10: TestViewUserAccountAsNonAdminForOtherUser

func TestViewUserAccountAsNonAdminForOtherUser(t *testing.T) {
	ds, wm := initializeViewUserAccountDS()
	ja, _ := ds.FindUserAccountByUsername("thirdpresident")
	accessKeys, _, _ := ds.RetrieveUserKeys(ja.Id, nil, 1000)
	if len(accessKeys) == 0 {
		t.Error("Expected to find at least one access key stored.")
	}
	accessKey := accessKeys[0]
	otherUser, _ := ds.FindUserAccountByUsername("secondpresident")
	req, _ := http.NewRequest(webmachine.GET, "http://localhost/api/v1/json/account/user/view/"+otherUser.Id, nil)
	req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept-Charset", "utf-8")
	req.Header.Set("Accept-Encoding", "identity")
	req.Header.Set("Accept-Language", "en-us")
	req.Header.Set("Connection", "close")
	apiutil.NewSigner(accessKey.Id, accessKey.PrivateKey).SignRequest(req, 0)
	resp := webmachine.NewMockResponseWriter(req)
	wm.ServeHTTP(resp, req)
	if resp.StatusCode != http.StatusForbidden {
		t.Error("Expected ", http.StatusForbidden, " status code but received ", resp.StatusCode)
	}
}
开发者ID:pomack,项目名称:dsocial.go,代码行数:22,代码来源:view_test.go


示例11: TestAuthLoginAccountDoesNotExist

func TestAuthLoginAccountDoesNotExist(t *testing.T) {
	_, wm := initializeAuthUserAccountDS()
	jsonobj := jsonhelper.NewJSONObject()
	jsonobj.Set("username", "dudewhatever")
	jsonobj.Set("password", "blah blah")
	jsonbuf, _ := json.Marshal(jsonobj)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/auth/login/", bytes.NewBuffer(jsonbuf))
	req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept-Charset", "utf-8")
	req.Header.Set("Accept-Encoding", "identity")
	req.Header.Set("Accept-Language", "en-us")
	req.Header.Set("Connection", "close")
	reqbytes, _ := httputil.DumpRequest(req, true)
	t.Log("Request is:\n", string(reqbytes), "\n\n")
	resp := webmachine.NewMockResponseWriter(req)
	wm.ServeHTTP(resp, req)
	t.Log("Response is:\n", resp.String(), "\n\n")
	if resp.StatusCode != http.StatusUnauthorized {
		t.Error("Expected ", http.StatusUnauthorized, " status code but received ", resp.StatusCode)
	}
	if resp.Header().Get("Content-Type") != req.Header.Get("Accept") {
		t.Error("Expected Content-Type \"", req.Header.Get("Accept"), "\" but received ", resp.Header().Get("Content-Type"))
	}
	obj := jsonhelper.NewJSONObject()
	err := json.Unmarshal(resp.Buffer.Bytes(), &obj)
	if err != nil {
		t.Error("Unable to unmarshal login response due to error:", err.Error())
	}
	if status := obj.GetAsString("status"); status != "error" {
		t.Error("Expected error operation, but had status:", status)
	}
	if result := obj.Get("result"); result != nil {
		t.Error("Expected result to be nil, but was", result)
	}
	if message := obj.GetAsString("message"); message != auth.ERR_INVALID_USERNAME_PASSWORD_COMBO.Error() {
		t.Error("Expected ERR_INVALID_USERNAME_PASSWORD_COMBO for message, but was", message)
	}
}
开发者ID:pomack,项目名称:dsocial.go,代码行数:39,代码来源:login_test.go


示例12: TestViewUserAccount

func TestViewUserAccount(t *testing.T) {
	ds, wm := initializeViewUserAccountDS()
	gw, _ := ds.FindUserAccountByUsername("firstpresident")
	accessKeys, _, _ := ds.RetrieveUserKeys(gw.Id, nil, 1000)
	if len(accessKeys) == 0 {
		t.Error("Expected to find at least one access key stored.")
	}
	accessKey := accessKeys[0]
	otherUser := gw
	req, _ := http.NewRequest(webmachine.GET, "http://localhost/api/v1/json/account/user/view/"+otherUser.Id, nil)
	req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept-Charset", "utf-8")
	req.Header.Set("Accept-Encoding", "identity")
	req.Header.Set("Accept-Language", "en-us")
	req.Header.Set("Connection", "close")
	apiutil.NewSigner(accessKey.Id, accessKey.PrivateKey).SignRequest(req, 0)
	resp := webmachine.NewMockResponseWriter(req)
	wm.ServeHTTP(resp, req)
	if resp.StatusCode != http.StatusOK {
		t.Error("Expected ", http.StatusOK, " status code but received ", resp.StatusCode)
	}
	if resp.Header().Get("Content-Type") != req.Header.Get("Accept") {
		t.Error("Expected Content-Type \"", req.Header.Get("Accept"), "\" but received ", resp.Header().Get("Content-Type"))
	}
	user := new(dm.User)
	obj := jsonhelper.NewJSONObject()
	err := json.Unmarshal(resp.Buffer.Bytes(), &obj)
	user.InitFromJSONObject(obj.GetAsObject("result"))
	if err != nil {
		t.Error("Error while unmarshaling JSON: ", err.Error())
	}
	if obj.GetAsString("status") != "success" {
		t.Error("Expected status = \"success\", but was \"", obj.GetAsString("status"), "\"")
	}
	if user.Name != otherUser.Name {
		t.Error("Expected name = \"", otherUser.Name, "\", but was ", user.Name)
	}
	if user.Username != otherUser.Username {
		t.Error("Expected username = \"", otherUser.Username, "\", but was ", user.Username)
	}
	if user.Email != otherUser.Email {
		t.Error("Expected email = \"", otherUser.Email, "\", but was ", user.Email)
	}
	if user.PhoneNumber != otherUser.PhoneNumber {
		t.Error("Expected phone_number = \"", otherUser.PhoneNumber, "\", but was ", user.PhoneNumber)
	}
	if user.Address != otherUser.Address {
		t.Error("Expected address = \"", otherUser.Address, "\", but was ", user.Address)
	}
	if user.Role != otherUser.Role {
		t.Error("Expected role = ", otherUser.Role, " but was ", user.Role)
	}
	if user.Id != otherUser.Id {
		t.Error("Expected id to be ", otherUser.Id, ", but was ", user.Id)
	}
	if theuser, err := ds.RetrieveUserAccountById(otherUser.Id); err != nil || theuser == nil {
		if theuser == nil {
			t.Error("Unable to find User account by id ", otherUser.Id)
		}
		if err != nil {
			t.Error("Error trying to find user account by id: ", err.Error())
		}
	}
	if theuser, err := ds.FindUserAccountByUsername(otherUser.Username); err != nil || theuser == nil {
		if theuser == nil {
			t.Error("Unable to find User account by username ", otherUser.Username)
		}
		if err != nil {
			t.Error("Error trying to find user account by username: ", err.Error())
		}
	}
	if theusers, _, err := ds.FindUserAccountsByEmail(otherUser.Email, nil, 1000); err != nil || len(theusers) != 1 {
		if len(theusers) != 1 {
			t.Error("Found ", len(theusers), " User accounts by email for ", otherUser.Email, " rather than 1: ", theusers)
		}
		if err != nil {
			t.Error("Error trying to find user accounts by email: ", err.Error())
		}
	}
}
开发者ID:pomack,项目名称:dsocial.go,代码行数:80,代码来源:view_test.go


示例13: TestAuthLoginAdmin

func TestAuthLoginAdmin(t *testing.T) {
	ds, wm := initializeAuthUserAccountDS()
	user, _ := ds.FindUserAccountByUsername("firstpresident")
	accessKeys, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000)
	if len(accessKeys) == 0 {
		t.Error("Expected to find at least one access key stored.")
	}
	accessKey := accessKeys[0]
	jsonobj := jsonhelper.NewJSONObject()
	jsonobj.Set("username", user.Username)
	jsonobj.Set("password", "number one")
	jsonbuf, _ := json.Marshal(jsonobj)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/auth/login/", bytes.NewBuffer(jsonbuf))
	req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
	req.Header.Set("Accept-Charset", "utf-8")
	req.Header.Set("Accept-Encoding", "identity")
	req.Header.Set("Accept-Language", "en-us")
	req.Header.Set("Connection", "close")
	reqbytes, _ := httputil.DumpRequest(req, true)
	t.Log("Request is:\n", string(reqbytes), "\n\n")
	resp := webmachine.NewMockResponseWriter(req)
	wm.ServeHTTP(resp, req)
	t.Log("Response is:\n", resp.String(), "\n\n")
	if resp.StatusCode != http.StatusOK {
		t.Error("Expected ", http.StatusOK, " status code but received ", resp.StatusCode)
	}
	if resp.Header().Get("Content-Type") != req.Header.Get("Accept") {
		t.Error("Expected Content-Type \"", req.Header.Get("Accept"), "\" but received ", resp.Header().Get("Content-Type"))
	}
	obj := jsonhelper.NewJSONObject()
	err := json.Unmarshal(resp.Buffer.Bytes(), &obj)
	if err != nil {
		t.Error("Unable to unmarshal login response due to error:", err.Error())
	}
	if status := obj.GetAsString("status"); status != "success" {
		t.Error("Expected successful operation, but had status:", status)
	}
	result := obj.GetAsObject("result")
	if result == nil {
		t.Error("Expected an object for result, but was nil")
	} else {
		accessKeys2, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000)
		if len(accessKeys2) != 2 {
			t.Error("Expected 2 access keys after logging in, but found", len(accessKeys2))
		} else {
			var checkAccessKey *dm.AccessKey
			if accessKeys2[0].Id == accessKey.Id {
				checkAccessKey = accessKeys2[1]
			} else {
				checkAccessKey = accessKeys2[0]
			}
			if access_key_id := result.GetAsString("access_key_id"); access_key_id != checkAccessKey.Id {
				t.Error("Expected access_key_id with value", checkAccessKey.Id, "but was", access_key_id)
			}
			if private_key := result.GetAsString("private_key"); private_key != checkAccessKey.PrivateKey {
				t.Error("Expected private_key with value", checkAccessKey.PrivateKey, "but was", private_key)
			}
		}
		if username := result.GetAsString("username"); username != user.Username {
			t.Error("Expected username", user.Username, "but was", username)
		}
		if name := result.GetAsString("name"); name != user.Name {
			t.Error("Expected name", user.Name, "but was", name)
		}
		if user_id := result.GetAsString("user_id"); user_id != user.Id {
			t.Error("Expected user_id", user.Id, "but was", user_id)
		}
	}
}
开发者ID:pomack,项目名称:dsocial.go,代码行数:70,代码来源:login_test.go



注:本文中的github.com/pomack/webmachine/go/webmachine.NewMockResponseWriter函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang webmachine.Request类代码示例发布时间:2022-05-28
下一篇:
Golang thrift.TMap类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap