-
Notifications
You must be signed in to change notification settings - Fork 2
/
fakepoint_test.go
143 lines (122 loc) · 5.03 KB
/
fakepoint_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package fakepoint
import (
"bytes"
. "github.com/smartystreets/goconvey/convey"
"io/ioutil"
"testing"
"net/http"
)
const foo = "hello world"
func TestFakeRoundTrip(t *testing.T) {
Convey("Basic DSL - planning roundtrips", t, func() {
maker := NewFakepointMaker()
Convey("it returns the document with a 200", func() {
maker.NewGet("https://api.opsgenie.com/v1/json/alert", 200).SetHeader("Content-Type", "text/plain").SetResponse(foo)
resp, _ := maker.Client().Get("https://api.opsgenie.com/v1/json/alert")
text, _ := ioutil.ReadAll(resp.Body)
So(resp.Header.Get("Content-Type"), ShouldEqual, "text/plain")
So(string(text), ShouldEqual, "hello world")
So(resp.StatusCode, ShouldEqual, 200)
})
Convey("it doesnt resolve to the wrong URL", func() {
resp, _ := maker.Client().Get("https://somethingelse.com")
So(resp.StatusCode, ShouldEqual, 404)
})
Convey("the header", func() {
maker.NewGet("https://api.opsgenie.com/v1/json/alert", 200).SetHeader("Content-Type", "application/json").SetResponse("{ \"code\": 200 }")
resp, err := maker.Client().Get("https://api.opsgenie.com/v1/json/alert")
So(err, ShouldBeNil)
So(resp.Header.Get("Content-Type"), ShouldEqual, "application/json")
})
})
Convey("Specify the response document", t, func() {
maker := NewFakepointMaker()
maker.NewGet("http://abc.com", 200).SetResponseDocument("./README.md")
resp, err := maker.Client().Get("http://abc.com")
text, _ := ioutil.ReadAll(resp.Body)
So(err, ShouldBeNil)
So(string(text), ShouldContainSubstring, "Create Fake endpoints for HTTP testing. Specify the response data sent back.")
})
Convey("The maker handles multiple fake round trips", t, func() {
maker := NewFakepointMaker()
Convey("distinct URLs", func() {
maker.NewGet("https://api.opsgenie.com/v1/json/alert", 200).SetResponse("hello world")
maker.NewGet("https://another.system.com", 200).SetResponse("not with a bang but a whimper")
resp1, err := maker.Client().Get("https://api.opsgenie.com/v1/json/alert")
text, _ := ioutil.ReadAll(resp1.Body)
So(err, ShouldBeNil)
So(string(text), ShouldEqual, "hello world")
So(resp1.StatusCode, ShouldEqual, 200)
resp2, err2 := maker.Client().Get("https://another.system.com")
text, _ = ioutil.ReadAll(resp2.Body)
So(err2, ShouldBeNil)
So(string(text), ShouldEqual, "not with a bang but a whimper")
So(resp2.StatusCode, ShouldEqual, 200)
})
Convey("Identical URLs, different HTTP methods", func() {
maker.NewPost("abc.com/greeting", 201).SetResponse("hello world1")
maker.NewGet("abc.com/greeting", 200).SetResponse("hello world2")
resp1, err := maker.Client().Post("abc.com/greeting", "application/text", bytes.NewReader([]byte{'g', 'o'}))
text, _ := ioutil.ReadAll(resp1.Body)
So(err, ShouldBeNil)
So(string(text), ShouldEqual, "hello world1")
So(resp1.StatusCode, ShouldEqual, 201)
resp2, err2 := maker.Client().Get("abc.com/greeting")
text, _ = ioutil.ReadAll(resp2.Body)
So(err2, ShouldBeNil)
So(string(text), ShouldEqual, "hello world2")
So(resp2.StatusCode, ShouldEqual, 200)
})
})
Convey("302s (following redirects)", t, func() {
maker := NewFakepointMaker()
Convey("302 generates an interal 200 for /new-location/ according to 302 rules", func() {
maker.NewGet("abc.com", 302).SetResponse("")
resp, err := maker.Client().Get("abc.com")
So(err, ShouldBeNil)
So(resp.Header.Get("Location"), ShouldNotBeNil)
So(resp.StatusCode, ShouldEqual, 200)
})
Convey("the status code, with Location headers for 201, 202", func() {
codes := []int{201, 202}
for _, code := range codes {
maker.NewGet("abc.com", code).SetResponse("")
resp, err := maker.Client().Get("abc.com")
So(err, ShouldBeNil)
So(resp.Header.Get("Location"), ShouldNotBeNil)
So(resp.StatusCode, ShouldEqual, code)
}
})
})
Convey("Repetition of Fakepoint default to a repetition of 1", t, func() {
maker := NewFakepointMaker()
Convey("defaults to 1", func() {
maker.NewGet("http://abc.com", 200).SetResponse("")
resp, err := maker.Client().Get("http://abc.com")
resp2, err := maker.Client().Get("http://abc.com")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, 200)
So(resp2.StatusCode, ShouldEqual, 404)
})
Convey("is configurable", func() {
maker.NewGet("http://abc.com", 200).SetResponse("").Duplicate(1)
resp, err := maker.Client().Get("http://abc.com")
resp2, err := maker.Client().Get("http://abc.com")
resp3, err := maker.Client().Get("http://abc.com")
So(err, ShouldBeNil)
So(resp.StatusCode, ShouldEqual, 200)
So(resp2.StatusCode, ShouldEqual, 200)
So(resp3.StatusCode, ShouldEqual, 404)
})
})
Convey("It works with a Request object", t, func() {
maker := NewFakepointMaker()
maker.NewGet("http://example.com", 200).SetResponse("hello world")
req, err := http.NewRequest("GET", "http://example.com", nil)
resp, err := maker.Client().Do(req)
text, _ := ioutil.ReadAll(resp.Body)
So(err, ShouldBeNil)
So(string(text), ShouldEqual, "hello world")
So(resp.StatusCode, ShouldEqual, 200)
})
}