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

Golang jsonapi.HandlerTest函数代码示例

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

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



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

示例1: TestListAll

func TestListAll(t *testing.T) {
	presetData := []Order{
		Order{1468248039, 100, -100, "USD"},
		Order{1468248040, -50, 51, "USD"},
		Order{1468248041, 100, -100, "JPY"},
		Order{1468248042, -50, 51, "JPY"},
	}
	h, token, mgr := makeListAll(presetData)
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/listall", "", `{"token":"`+token+`"}`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing listall: %s", err)
	}

	if resp.Code != http.StatusOK {
		t.Fatalf("unexpected error occured when testing listall with status code %d: %s", resp.Code, resp.Body.String())
	}

	var orders []Order
	if resp.Body == nil {
		t.Fatal(resp)
	}
	if err := json.Unmarshal(resp.Body.Bytes(), &orders); err != nil {
		t.Fatalf("cannot encode returned data from listall: %s", err)
	}

	msgs := validateOrders(presetData, orders)
	for _, msg := range msgs {
		t.Errorf("listall: %s", msg)
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:33,代码来源:listall_test.go


示例2: TestAuthWrongPin

func TestAuthWrongPin(t *testing.T) {
	fake := FakeAuthenticator("123456")
	h := &auth{fake}

	cases := []struct {
		in  string
		msg string
	}{
		{`{"pin":"654321"}`, "auth: sent wrong pin, expect 400, got %d"},
		{`{}`, "auth: sent no pin, expect 400, got %d"},
		{`"123456"`, "auth: sent wrong format, expect 400, got %d"},
	}

	for _, c := range cases {
		resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/auth", "", c.in)

		if err != nil {
			t.Fatalf("unexpected error occured when testing auth: %s", err)
		}

		if resp.Code != http.StatusBadRequest {
			t.Errorf(c.msg, resp.Code)
		}
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:25,代码来源:auth_test.go


示例3: TestPadEditAclCoop

func TestPadEditAclCoop(t *testing.T) {
	sess := session(pc().SF, t)
	sess.Login(peAclCoop)
	sess.Save()
	if err := sess.Err(); err != nil {
		t.Fatalf("While loging in as coop: %s", err)
	}
	param := map[string]interface{}{
		"title":      peAclPad.Title,
		"content":    peAclPad.Title,
		"tags":       peAclPad.Tags,
		"cooperator": []int{peAclCoop.ID + 1},
		"version":    peAclPad.Version,
	}
	resp, err := jsonapi.HandlerTest(pc().Edit).PostJSON(peAclURI, sess.Cookie(), param)
	if err != nil {
		t.Fatalf("Error occured when testing edit ACL with coop: %s", err)
	}

	if !testResult(resp.Body, false) {
		t.Errorf("Edit cooperators success with coop: %s", resp.Body.String())
	}

	if !testData(3, resp.Body, "code") {
		t.Errorf("Expect error code 3 when edit cooperators with coop, got response %s", resp.Body.String())
	}
}
开发者ID:Patrolavia,项目名称:darius,代码行数:27,代码来源:pad_edit_acl_test.go


示例4: TestListNotJSON

func TestListNotJSON(t *testing.T) {
	h, _, mgr := makeList([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/list", "", ``)

	if err != nil {
		t.Fatalf("unexpected error occured when testing list: %s", err)
	}

	if resp.Code != http.StatusBadRequest {
		t.Fatalf("unexpected status code %d for bad request: %s", resp.Code, resp.Body.String())
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:14,代码来源:list_test.go


示例5: TestListAllNotJSON

func TestListAllNotJSON(t *testing.T) {
	h, _, mgr := makeListAll([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/listall", "", `1234`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing listall: %s", err)
	}

	if resp.Code != http.StatusBadRequest {
		t.Fatalf("expected listall return bad request when not sending json, got %d: %s", resp.Code, resp.Body.String())
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:14,代码来源:listall_test.go


示例6: TestListAllWrongToken

func TestListAllWrongToken(t *testing.T) {
	h, _, mgr := makeListAll([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/listall", "", `{"token":"1234"}`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing listall: %s", err)
	}

	if resp.Code != http.StatusForbidden {
		t.Fatalf("expected listall return forbidden when wrong token, got %d: %s", resp.Code, resp.Body.String())
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:14,代码来源:listall_test.go


示例7: TestPadVersionMismatch

func TestPadVersionMismatch(t *testing.T) {
	pad, err := model.NewPad(db, pvTestUser.ID, "pm version", "test pm version", nil, nil)
	if err != nil {
		t.Fatalf("Cannot create pad for version testing: %s", err)
	}
	sess := session(pc().SF, t)
	sess.Login(pvTestUser)
	sess.Save()
	if err := sess.Err(); err != nil {
		t.Fatalf("Failed to login for testing version: %s", err)
	}
	cookie := sess.Cookie()
	uri := fmt.Sprintf("/api/edit/%d", pad.ID)
	param := map[string]interface{}{
		"title":   "pm version modified",
		"content": "content",
		"version": pad.Version + 1,
	}

	fn := func() {
		resp, err := jsonapi.HandlerTest(pc().Edit).PostJSON(uri, cookie, param)
		if err != nil {
			t.Fatalf("Cannot run /api/edit for version test: %s", err)
		}

		if !testResult(resp.Body, false) {
			t.Errorf("Version (%d) mismatch but pad is updates: %s", param["version"].(int), resp.Body.String())
		}
		code, ok := testHasData(resp.Body, "code")
		if !ok {
			t.Errorf("Not returning code wher version mismatch: %s", resp.Body.String())
		}
		var c int
		switch v := code.(type) {
		case float32, float64:
			c = int(reflect.ValueOf(v).Float())
		case int64, int32, int16, int8, int:
			c = int(reflect.ValueOf(v).Int())
		default:
			t.Fatalf("Returned code is not integer: %#v", code)
		}
		if c != 5 {
			t.Errorf("Error code is not 5: %d", c)
		}
	}

	fn()
	param["version"] = pad.Version - 1
	fn()
}
开发者ID:Patrolavia,项目名称:darius,代码行数:50,代码来源:pad_version_test.go


示例8: TestAddNoData

func TestAddNoData(t *testing.T) {
	h, token, mgr := makeAdd([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/add", "", `{"token":"`+token+`"}`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing add: %s", err)
	}

	if resp.Code != http.StatusBadRequest {
		t.Fatalf("unexpected status code %d for bas request: %s", resp.Code, resp.Body.String())
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:14,代码来源:add_test.go


示例9: TestAddOK

func TestAddOK(t *testing.T) {
	h, token, mgr := makeAdd([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post(
		"/api/add",
		"",
		`{"data":{"when":1468248043,"foreign":100,"local":-100,"code":"AUD"},"token":"`+token+`"}`,
	)

	if err != nil {
		t.Fatalf("unexpected error occured when testing add: %s", err)
	}

	if resp.Code != http.StatusOK {
		t.Fatalf("unexpected error occured when testing add with status code %d: %s", resp.Code, resp.Body.String())
	}

	if resp.Body == nil {
		t.Fatal(resp)
	}

	if str := strings.TrimSpace(resp.Body.String()); str != "null" {
		t.Errorf("add: returned non-null result: '%s'", str)
	}

	qstr := `SELECT * FROM orders`
	rows := mgr.Query(Order{}, qstr)
	expect := []Order{
		Order{1468248043, 100, -100, "AUD"},
	}

	var orders []Order
	for rows.Next() {
		var o Order
		rows.Scan(&o)
		orders = append(orders, o)
	}

	if err := rows.Err(); err != nil {
		t.Fatalf("add: error reading records from db: %s", err)
	}

	msgs := validateOrders(expect, orders)
	for _, msg := range msgs {
		t.Errorf("add: %s", msg)
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:48,代码来源:add_test.go


示例10: TestAddWrongToken

func TestAddWrongToken(t *testing.T) {
	h, _, mgr := makeAdd([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post(
		"/api/add",
		"",
		`{"data":{"when":1468248043,"foreign":100,"local":-100,"code":"AUD"},"token":"1234"}`,
	)

	if err != nil {
		t.Fatalf("unexpected error occured when testing add: %s", err)
	}

	if resp.Code != http.StatusForbidden {
		t.Fatalf("expect add return forbidden when wrong token, got %d: %s", resp.Code, resp.Body.String())
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:18,代码来源:add_test.go


示例11: TestAuthOK

func TestAuthOK(t *testing.T) {
	fake := FakeAuthenticator("123456")
	h := &auth{fake}

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/auth", "", `{"pin":"123456"}`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing auth: %s", err)
	}

	if resp.Code != http.StatusOK {
		t.Fatalf("auth: sent correct pin but got http status %d", resp.Code)
	}

	if resp.Body == nil {
		t.Fatal("auth: got 200 OK, but no token")
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:18,代码来源:auth_test.go


示例12: TestListWrongToken

func TestListWrongToken(t *testing.T) {
	h, _, mgr := makeList([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post(
		"/api/list",
		"",
		`{"code":"USD","token":"1234"}`,
	)

	if err != nil {
		t.Fatalf("unexpected error occured when testing list: %s", err)
	}

	if resp.Code != http.StatusForbidden {
		t.Fatalf("unexpected status code %d for forbidden: %s", resp.Code, resp.Body.String())
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:18,代码来源:list_test.go


示例13: TestPadDelAclNoLogin

func TestPadDelAclNoLogin(t *testing.T) {
	resp, err := jsonapi.HandlerTest(pc().Delete).Get(pdAclURI, "")
	if err != nil {
		t.Fatalf("Error occured while testing pad delete ACL without login: %s", err)
	}

	if !testResult(resp.Body, false) {
		t.Errorf("Delete success without login: %s", resp.Body.String())
	}

	code, ok := testHasData(resp.Body, "code")
	if !ok {
		t.Errorf("Error code not found in deletion without login: %s", resp.Body.String())
	}
	if !eqInt(1, code) {
		t.Errorf("Expected error code is 1 when delete without login, got %d", code)
	}
}
开发者ID:Patrolavia,项目名称:darius,代码行数:18,代码来源:pad_del_acl_test.go


示例14: TestPads

func TestPads(t *testing.T) {
	p, err := model.NewPad(db, padTestUser.ID, "list pad", "test list pad", []string{}, []int{})
	if err != nil {
		t.Fatalf("Cannot create pad for testing list: %s", err)
	}

	resp, err := jsonapi.HandlerTest(pc().List).Get("/api/pads", "")
	if err != nil {
		t.Fatalf("While getting response of /api/pads: %s", err)
	}

	if !testResult(resp.Body, true) {
		t.Fatalf("/api/pads resurns an error: %s", resp.Body.String())
	}
	if !testArrayHas(p.ID, resp.Body) {
		t.Fatalf("Cannot find pad#%d in response of /api/pads: %s", p.ID, resp.Body.String())
	}
}
开发者ID:Patrolavia,项目名称:darius,代码行数:18,代码来源:pad_test.go


示例15: realTestEdit

func realTestEdit(t *testing.T, pid int, tags []string, coops []int, cookie string) {
	pad, err := model.LoadPad(pid)
	if err != nil {
		t.Fatalf("Cannot load pad to edit: %s", err)
	}
	t.Logf("Test pad modification with tags %#v and coops %#v", tags, coops)
	ts := tags
	if tags == nil {
		ts = []string{}
	}
	c := coops
	if coops == nil {
		c = []int{}
	}
	randomTitle := fmt.Sprintf("randomTitle-%d", rand.Int())
	uri := fmt.Sprintf("/api/edit/%d", pad.ID)
	param := map[string]interface{}{
		"title":      randomTitle,
		"content":    pad.Content,
		"tags":       tags,
		"cooperator": coops,
		"version":    pad.Version,
	}

	resp, err := jsonapi.HandlerTest(pc().Edit).PostJSON(uri, cookie, param)
	if err != nil {
		t.Fatalf("Cannot save pad modification to db: %s", err)
	}
	if !testResult(resp.Body, true) {
		t.Errorf("/apd/edit returns false: %s", resp.Body.String())
	}
	actual, err := model.LoadPad(pad.ID)
	if err != nil {
		t.Fatalf("Cannot load just updated pad#%d: %s", pad.ID, err)
	}
	if !reflect.DeepEqual(actual.Tags, ts) {
		t.Errorf("Expected tags %v, got %v", tags, pad.Tags)
	}
	if !reflect.DeepEqual(actual.Cooperators, c) {
		t.Errorf("Expected coops %v, got %v", coops, pad.Cooperators)
	}

}
开发者ID:Patrolavia,项目名称:darius,代码行数:43,代码来源:pad_modify_test.go


示例16: TestPad

func TestPad(t *testing.T) {
	p, err := model.NewPad(db, padTestUser.ID, "pad", "test pad", []string{}, []int{})
	if err != nil {
		t.Fatalf("Cannot create pad for later testing: %s", err)
	}

	resp, err := jsonapi.HandlerTest(pc().View).Get(fmt.Sprintf("/api/pad/%d", p.ID), "")
	if err != nil {
		t.Fatalf("While getting response of /api/pad/%d: %s", p.ID, err)
	}

	if !testResult(resp.Body, true) {
		t.Fatalf("/api/pad/%d returns an error: %s", p.ID, resp.Body.String())
	}

	if !testData(p.ID, resp.Body, "id") {
		t.Fatalf("/api/pad/%d returns wrong data: %s", p.ID, resp.Body.String())
	}
}
开发者ID:Patrolavia,项目名称:darius,代码行数:19,代码来源:pad_test.go


示例17: TestPadEditAclNoLogin

func TestPadEditAclNoLogin(t *testing.T) {
	param := map[string]interface{}{
		"title":      peAclPad.Title,
		"content":    peAclPad.Title,
		"tags":       peAclPad.Tags,
		"cooperator": peAclPad.Cooperators,
	}
	resp, err := jsonapi.HandlerTest(pc().Edit).PostJSON(peAclURI, "", param)
	if err != nil {
		t.Fatalf("Error occured when testing edit ACL without login: %s", err)
	}

	if !testResult(resp.Body, false) {
		t.Errorf("Edit success while no user logged in!: %s", resp.Body.String())
	}

	if !testData(1, resp.Body, "code") {
		t.Errorf("Expect error code 1 when edit without login, got response %s", resp.Body.String())
	}
}
开发者ID:Patrolavia,项目名称:darius,代码行数:20,代码来源:pad_edit_acl_test.go


示例18: TestListAllEmpty

func TestListAllEmpty(t *testing.T) {
	h, token, mgr := makeListAll([]Order{})
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post("/api/listall", "", `{"token":"`+token+`"}`)

	if err != nil {
		t.Fatalf("unexpected error occured when testing listall: %s", err)
	}

	if resp.Code != http.StatusOK {
		t.Fatalf("unexpected error occured when testing listall with status code %d: %s", resp.Code, resp.Body.String())
	}

	if resp.Body == nil {
		t.Fatal(resp)
	}
	if str := strings.TrimSpace(resp.Body.String()); str != "[]" {
		t.Errorf("listall: not returning empty array: '%s'", str)
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:21,代码来源:listall_test.go


示例19: TestAddDuplicated

func TestAddDuplicated(t *testing.T) {
	presetData := []Order{
		Order{1468248039, 100, -100, "USD"},
	}
	h, token, mgr := makeAdd(presetData)
	defer mgr.Connection().Close()

	resp, err := jsonapi.HandlerTest(jsonapi.APIHandler(h.Handle).Handler).Post(
		"/api/add",
		"",
		`{"data":{"when":1468248039,"foreign":100,"local":-100,"code":"USD"},"token":"`+token+`"}`,
	)

	if err != nil {
		t.Fatalf("unexpected error occured when testing add: %s", err)
	}

	if resp.Code != http.StatusInternalServerError {
		t.Fatalf("unexpected status code %d for duplicated record: %s", resp.Code, resp.Body.String())
	}
}
开发者ID:Ronmi,项目名称:xchg,代码行数:21,代码来源:add_test.go


示例20: TestDeletePad

func TestDeletePad(t *testing.T) {
	sess := session(pc().SF, t)
	sess.Login(pmTestUser)
	sess.Save()
	if err := sess.Err(); err != nil {
		t.Fatalf("Failed to login for deleting pad: %s", err)
	}
	cookie := sess.Cookie()
	pad, err := model.NewPad(db, pmTestUser.ID, "pm del", "test pm del", nil, nil)
	if err != nil {
		t.Fatalf("Cannot create pad for later deletion: %s", err)
	}

	resp, err := jsonapi.HandlerTest(pc().Delete).Get(fmt.Sprintf("/api/delete/%d", pad.ID), cookie)
	if err != nil {
		t.Fatalf("Error occured when delete pad: %s", err)
	}

	if !testResult(resp.Body, true) {
		t.Errorf("Failed to delete pad#%d: returned %s", pad.ID, resp.Body.String())
	}
}
开发者ID:Patrolavia,项目名称:darius,代码行数:22,代码来源:pad_modify_test.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang mango.Env类代码示例发布时间:2022-05-28
下一篇:
Golang lex.Lexer类代码示例发布时间: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