本文整理汇总了Golang中github.com/Patrolavia/jsonapi.HandlerTest函数的典型用法代码示例。如果您正苦于以下问题:Golang HandlerTest函数的具体用法?Golang HandlerTest怎么用?Golang HandlerTest使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了HandlerTest函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
}
}
示例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)
}
}
}
示例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())
}
}
示例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())
}
}
示例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())
}
}
示例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())
}
}
示例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()
}
示例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())
}
}
示例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)
}
}
示例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())
}
}
示例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")
}
}
示例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())
}
}
示例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)
}
}
示例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())
}
}
示例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)
}
}