-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_test.go
71 lines (61 loc) · 1.68 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* @Author: charley zhu
* @Date: 2023-10-11 12:24:57
* @LastEditTime: 2023-10-22 12:28:49
* @LastEditors: charley zhu
* @Description:
*/
package user_test
import (
"encoding/json"
"fmt"
"global-backend/src/lib"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_User(t *testing.T) {
r, err := lib.SetupRouter()
if err != nil {
t.Fatal(err)
}
t.Run("test hello", func(t *testing.T) {
req := httptest.NewRequest("GET", "/user/hello", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
var resp map[string]string
err := json.Unmarshal([]byte(w.Body.Bytes()), &resp)
assert.Nil(t, err)
assert.Equal(t, "user hello", resp["message"])
})
t.Run("test auth", func(t *testing.T) {
body := `{ "username": "root", "password": "hello"}`
req := httptest.NewRequest("POST", "/user/authorize", strings.NewReader(body))
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
var resp map[string]string
json.Unmarshal([]byte(w.Body.Bytes()), &resp)
fmt.Println(resp["data"])
})
t.Run("test sync cond", func(t *testing.T) {
req := httptest.NewRequest("GET", "/user/test/syncCond", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
var resp map[string]string
json.Unmarshal([]byte(w.Body.Bytes()), &resp)
fmt.Println(resp["data"])
})
t.Run("test sync pool", func(t *testing.T) {
req := httptest.NewRequest("GET", "/user/test/syncPool", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
var resp map[string]string
json.Unmarshal([]byte(w.Body.Bytes()), &resp)
fmt.Println(resp["data"])
})
}