-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
79 lines (70 loc) · 1.7 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
77
78
79
package testclient
import (
"encoding/json"
"flag"
"fmt"
"testing"
)
const testURL = "https://api.github.com/users/google/repos"
type repo struct {
RepoId int `json:"id"`
URL string `json:"html_url"`
Description string `json:"description"`
}
var reposFunc = func(body []byte) (interface{}, error) {
repos := make([]repo, 0)
err := json.Unmarshal(body, &repos)
return repos, err
}
var refresh = flag.Bool("refresh", false, "Refresh recorded results")
const ResponseAsRecordedFilename = "response-as-recorded.json"
func getExpectedResponses(t *testing.T) []*ExpectedResponse {
return []*ExpectedResponse{
{
Fixture: NewFixture(t, ResponseAsRecordedFilename),
URL: testURL,
StatusCode: 200,
ContentType: "application/json; charset=utf-8",
},
{
URL: fmt.Sprintf("%s/not-there", testURL),
StatusCode: 404,
},
{
URL: "http://localhost/not-there",
StatusCode: 0,
},
}
}
func TestMain(m *testing.M) {
m.Run()
}
func TestMockTransportGet(t *testing.T) {
if !*refresh {
t.Log("-refresh=false: Skipping refresh of recorded results")
} else {
tc := NewTestContext(TestContextArgs{
TestManager: t,
TestURL: testURL,
ValidateBodyFunc: reposFunc,
Filename: "response-as-recorded.json",
})
tc.RefreshJSONFixture()
}
var testNum = 1
for _, er := range getExpectedResponses(t) {
tc := NewTestContext(TestContextArgs{
TestManager: t,
ExpectedResponse: er,
TestNum: testNum,
ValidateBodyFunc: reposFunc,
})
_, err := tc.TestJSONGet(NewClientWithTransport(&MockTransport{
ExpectedResponse: er,
}))
if err != nil {
tc.Error("Failed to HTTP GET", err)
}
testNum++
}
}