-
Notifications
You must be signed in to change notification settings - Fork 3
/
mock_test.go
86 lines (76 loc) · 1.81 KB
/
mock_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 request
import (
"encoding/xml"
"testing"
assert "github.com/blendlabs/go-assert"
)
type mockObject struct {
XMLName xml.Name `json:"-" xml:"Borrower"`
ID int `json:"id" xml:"id,attr"`
Email string `json:"email" xml:"-"`
DeploymentID int `json:"deployment_id" xml:"-"`
}
func testServiceRequest(t *testing.T, additionalTests ...func(*Request)) {
assert := assert.New(t)
sr := New().
WithMockedResponse(MockedResponseInjector).
AsDelete().
AsPatch().
AsPut().
AsPost().
AsGet().
WithScheme("http").
WithHost("localhost:5001").
WithPath("/api/v1/borrowers/2").
WithHeader("deployment", "test").
WithPostData("test", "regressions").
WithQueryString("foo", "bar").
WithTimeout(500).
WithQueryString("moobar", "zoobar").
WithScheme("http")
assert.Equal("http", sr.Scheme)
assert.Equal("localhost:5001", sr.Host)
assert.Equal("GET", sr.Verb)
req, err := sr.Request()
assert.Nil(err)
assert.NotNil(req)
for _, additionalTest := range additionalTests {
additionalTest(sr)
}
}
func testForID(id int, assert *assert.Assertions) func(sr *Request) {
return func(sr *Request) {
res := mockObject{}
err := sr.JSON(&res)
assert.Nil(err)
assert.Equal(id, res.ID)
}
}
func TestFileServiceRequestScheduler(t *testing.T) {
assert := assert.New(t)
defer ClearMockedResponses()
res := []string{
"{\"id\" : 0, \"deployment_id\": 2 }",
"{\"id\" : 1, \"deployment_id\": 2 }",
"{\"id\" : 2, \"deployment_id\": 2 }",
}
i := 0
MockResponse(
"GET",
"http://localhost:5001/api/v1/borrowers/2?foo=bar&moobar=zoobar",
func() MockedResponse {
r := res[i]
i++
return MockedResponse{
ResponseBody: []byte(r),
StatusCode: 200,
}
},
)
testServiceRequest(
t,
testForID(0, assert),
testForID(1, assert),
testForID(2, assert),
)
}