-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
86 lines (78 loc) · 2.57 KB
/
main_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/airdb/bbs-api/web"
"github.com/stretchr/testify/assert"
)
func performRequest(method, path string) (*httptest.ResponseRecorder, error) {
router := web.NewRouter()
req, err := http.NewRequest(method, path, nil)
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
return w, err
}
//You could write the init logic like reset database code here
var testCases = []struct {
TestID int
Uri string
Method string
bodyData string
ExpectedCode int
responseRegexg string
msg string
}{
//Testing will run one by one, so you can combine it to a user story till another init().
//And you can modified the header or body in the func(req *http.Request) {}
//--------------------- Testing case register ---------------------
{
1,
"/v1/notice/list",
"GET",
`{"user":{"username": "wangzitian0","email": "[email protected]","password": "jakejxke"}}`,
http.StatusOK,
`{"user":{"username":"wangzitian0","email":"[email protected]","bio":"","image":null,"token":"([a-zA-Z0-9-_.]{115})"}}`,
"valid data and should return StatusCreated",
},
{
2,
"/v1/article/list",
"GET",
`{"user":{"username": "wangzitian0","email": "[email protected]","password": "jakejxke"}}`,
http.StatusOK,
`{"user":{"username":"wangzitian0","email":"[email protected]","bio":"","image":null,"token":"([a-zA-Z0-9-_.]{115})"}}`,
"valid data and should return StatusCreated",
},
{
3,
"/v1/square/list",
"GET",
`{"user":{"username": "wangzitian0","email": "[email protected]","password": "jakejxke"}}`,
http.StatusOK,
`{"user":{"username":"wangzitian0","email":"[email protected]","bio":"","image":null,"token":"([a-zA-Z0-9-_.]{115})"}}`,
"valid data and should return StatusCreated",
},
{
4,
"/v1/article/list",
"GET",
`{"user":{"username": "wangzitian0","email": "[email protected]","password": "jakejxke"}}`,
http.StatusOK,
`{"user":{"username":"wangzitian0","email":"[email protected]","bio":"","image":null,"token":"([a-zA-Z0-9-_.]{115})"}}`,
"valid data and should return StatusCreated",
},
}
// https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/unit_test.go
func Test_API(t *testing.T) {
asserts := assert.New(t)
for _, testCase := range testCases {
t.Log("test case:", testCase.TestID, testCase.Uri)
uri := "/apis/bbs" + testCase.Uri
resp, err := performRequest(testCase.Method, uri)
asserts.NoError(err)
assert.Equal(t, testCase.ExpectedCode, resp.Code)
// assert.Regexp(testCase.responseRegexg, resp.Body.String(), )
}
}