-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclient_test.go
76 lines (67 loc) · 1.49 KB
/
client_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
package core
import (
"context"
"net/http"
"testing"
"time"
)
const (
testTimeout = 5 * time.Second
)
type TestRequestRequest struct {
FirstField string `json:"first_field"`
SecondField int32 `json:"second_field"`
}
type TestRequestResponse struct {
FirstField string `json:"first_header"`
SecondField struct {
Key int32 `json:"key"`
} `json:"second_header"`
ThirdField []struct {
FirstElement string `json:"first_element"`
} `json:"third_header"`
}
func TestRequest(t *testing.T) {
tests := []struct {
statusCode int
headers map[string]string
params *TestRequestRequest
response string
}{
{
http.StatusOK,
map[string]string{
"first_header": "first-value",
"second_header": "second-value",
},
&TestRequestRequest{
FirstField: "test",
SecondField: 123,
},
`{
"first_field": "first-value",
"second_field": {
"key": 12
},
"third_field": [
{
"first_element": "first-element-value"
}
]
}`,
},
}
for _, test := range tests {
c := NewMockClient(NewMockHttpHandler(test.statusCode, test.response, test.headers))
respStruct := &TestRequestResponse{}
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
resp, err := c.Request(ctx, http.MethodPost, "/", test.params, respStruct, nil)
if err != nil {
t.Error(err)
continue
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}