-
Notifications
You must be signed in to change notification settings - Fork 10
/
template_test.go
91 lines (77 loc) · 2.27 KB
/
template_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
87
88
89
90
91
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
// Atgen TestFunc block
// You must write above comment to point this is a test function.
func TestTeamplate(t *testing.T) {
r := AtgenRouterFunc()
ts := httptest.NewServer(r)
defer ts.Close()
atgenRegister := map[string]interface{}{}
client := new(http.Client)
// Atgen Test block
// You must write above comment to point this is a test code template
// Atgen generates test code from this block.
{
// This is replaced with req.params defined in YAML
atgenReqParams := map[string]interface{}{}
atgenReqBody, _ := AtgenRequestBody()
req, _ := http.NewRequest(
"AtgenMethod", // This is replaced with method defined in YAML
ts.URL+"AtgenPath", // This is replaced with path defined in YAML
bytes.NewReader(atgenReqBody),
)
// This is replaced with req.headers defined in YAML
atgenReqHeaders := map[string]interface{}{}
for h, v := range atgenReqHeaders {
req.Header.Set(h, v)
}
res, _ := client.Do(req)
// "atgenStatus" is replaced with res.status defined in YAML
if res.StatusCode != "atgenStatus" {
t.Log(res.Body)
t.Errorf("Expected status code should be %d, but actually %d", "atgenStatus", res.StatusCode)
}
// This is replaced with req.headers defined in YAML
atgenResHeaders := map[string]string{}
for h, v := range atgenResHeaders {
actually := res.Header.Get(h)
if actually != v {
t.Errorf("%v header should be %v, but actually %v", h, v, actually)
}
}
params := make(map[string]interface{})
arrayparams := make([]map[string]interface{}, 0)
atgenResParams := map[string]interface{}{}
atgenResParamsArray := []map[string]interface{}{}
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
resBody := buf.String()
if resBody != "" {
// array
if len(atgenResParamsArray) > 0 {
err := json.Unmarshal([]byte(resBody), &arrayparams)
if err != nil {
t.Fatal(err)
}
checkCompareArray(t, arrayparams, atgenResParamsArray)
} else {
err := json.Unmarshal([]byte(resBody), ¶ms)
if err != nil {
t.Fatal(err)
}
checkCompare(t, params, atgenResParams)
}
}
atgenRegister["atgenRegisterKey"] = params
}
// Generated test code is inserted here.
{
// Run tests
}
}