-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_test.go
53 lines (42 loc) · 1.22 KB
/
user_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"net/http"
"github.com/gavv/httpexpect/v2"
)
// referenced doc:https://github.com/gavv/httpexpect/blob/master/_examples/echo_test.go
// TODO: add more testcase
func UserTest(e *httpexpect.Expect) {
//create user
resp := e.POST("/v1/users").
WithJSON(map[string]string{
"username": "sample_user",
}).Expect().Status(http.StatusCreated).JSON().Object()
// check
resp.ValueEqual("username", "sample_user")
resp.Keys().ContainsOnly("id", "username")
user1 := resp.Raw()
// get user
e.GET("/v1/users/" + user1["id"].(string)).
Expect().
Status(http.StatusOK).
JSON().Object()
// check
resp.ValueEqual("username", "sample_user")
resp.Keys().ContainsOnly("id", "username")
//get users
e.GET("/v1/users").
WithJSON(map[string]string{
"username": "sample_user",
}).Expect().Status(http.StatusOK).JSON().Array()
// update user
resp = e.PATCH("/v1/users/" + user1["id"].(string)).
WithJSON(map[string]string{
"username": "updated_user",
}).
Expect().Status(http.StatusOK).JSON().Object()
resp.ValueEqual("username", "updated_user")
resp.Keys().ContainsOnly("id", "username")
// delete user
e.DELETE("/v1/users/" + user1["id"].(string)).
Expect().Status(http.StatusNoContent)
}