-
Notifications
You must be signed in to change notification settings - Fork 2
/
bogus_test.go
190 lines (154 loc) · 5.37 KB
/
bogus_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package bogus
import (
"bytes"
"io/ioutil"
"net"
"net/http"
"net/url"
"testing"
"github.com/franela/goblin"
. "github.com/onsi/gomega"
)
func TestBogus(t *testing.T) {
g := goblin.Goblin(t)
RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })
g.Describe("Bogus Functions", func() {
var server *Bogus
var host, port string
g.BeforeEach(func() {
server = New()
host, port = server.HostPort()
})
g.AfterEach(func() {
server.Close()
})
g.It("should return the host and port", func() {
Expect(host).To(Equal("127.0.0.1"))
Expect(port).ToNot(Equal(""))
})
g.It("should count hits against the server", func() {
resp, err := http.Get("http://" + net.JoinHostPort(host, port))
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
Expect(server.Hits()).To(Equal(1))
})
g.It("should track full hit records", func() {
resp, err := http.Get("http://" + net.JoinHostPort(host, port))
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
_, err = http.Post(
"http://"+net.JoinHostPort(host, port)+"/foo/bar",
"application/octet-stream",
bytes.NewBuffer([]byte("post body")))
Expect(err).NotTo(HaveOccurred())
req, err := http.NewRequest(
"PUT",
"http://"+net.JoinHostPort(host, port)+"/foo/bar/cool?foo=bar&baz=fiz",
bytes.NewBuffer([]byte("put body")))
Expect(err).NotTo(HaveOccurred())
req.Header.Add("X-CustomHeader01", "custom value 01")
client := http.Client{}
_, err = client.Do(req)
Expect(err).NotTo(HaveOccurred())
Expect(server.HitRecords()).To(HaveLen(3))
firstHit := server.HitRecords()[0]
Expect(firstHit.Verb).To(Equal("GET"))
Expect(firstHit.Path).To(Equal("/"))
Expect(firstHit.Query).To(HaveLen(0))
Expect(firstHit.Body).To(HaveLen(0))
secondHit := server.HitRecords()[1]
Expect(secondHit.Verb).To(Equal("POST"))
Expect(secondHit.Path).To(Equal("/foo/bar"))
Expect(secondHit.Query).To(HaveLen(0))
Expect(string(secondHit.Body)).To(Equal("post body"))
thirdHit := server.HitRecords()[2]
Expect(thirdHit.Verb).To(Equal("PUT"))
Expect(thirdHit.Path).To(Equal("/foo/bar/cool"))
Expect(thirdHit.Query).To(HaveLen(2))
Expect(thirdHit.Query["foo"]).To(HaveLen(1))
Expect(thirdHit.Query["foo"][0]).To(Equal("bar"))
Expect(thirdHit.Query["baz"]).To(HaveLen(1))
Expect(thirdHit.Query["baz"][0]).To(Equal("fiz"))
Expect(string(thirdHit.Body)).To(Equal("put body"))
Expect(thirdHit.Header.Get("X-CustomHeader01")).To(Equal("custom value 01"))
})
g.It("should allow adding a new path", func() {
p1 := "some other payload"
s1 := http.StatusOK
server.AddPath("/").
SetMethods("GET").
SetPayload([]byte(p1)).
SetStatus(s1)
resp, err := http.Get("http://" + net.JoinHostPort(host, port))
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
Expect(resp.StatusCode).To(Equal(s1))
Expect(server.Hits()).To(Equal(1))
})
g.It("should allow expecting url params", func() {
p1 := "some other payload"
s1 := http.StatusOK
server.AddPath("/").
SetMethods("GET").
SetParams(url.Values{"q": []string{"value"}}).
SetPayload([]byte(p1)).
SetStatus(s1)
resp, err := http.Get("http://" + net.JoinHostPort(host, port) + "?q=value")
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
Expect(resp.StatusCode).To(Equal(s1))
Expect(server.Hits()).To(Equal(1))
})
g.It("should return unique payloads per path", func() {
p1 := "some other payload"
server.AddPath("/").
SetMethods("GET").
SetPayload([]byte(p1))
p2 := "foobar"
server.AddPath("/foo/bar").
SetMethods("GET").
SetPayload([]byte(p2))
resp, err := http.Get("http://" + net.JoinHostPort(host, port))
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(body)).To(Equal(p1))
Expect(server.Hits()).To(Equal(1))
resp, err = http.Get("http://" + net.JoinHostPort(host, port) + "/foo/bar")
Expect(err).NotTo(HaveOccurred())
body, err = ioutil.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(body)).To(Equal(p2))
Expect(server.Hits()).To(Equal(2))
})
g.It("should return 404 on an unregistered path", func() {
server.AddPath("/foo/bar").
SetPayload([]byte("this is /foo/bar payload"))
resp, err := http.Get("http://" + net.JoinHostPort(host, port) + "/foo/bar/baz")
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusNotFound))
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(body)).To(Equal("Not Found"))
})
g.It("should return unique headers per path", func() {
headers1 := map[string]string{"Content-Type": "plain/text"}
server.AddPath("/").
SetMethods("GET").
SetHeaders(headers1)
headers2 := map[string]string{"Content-Type": "application/json"}
server.AddPath("/foo/bar").
SetMethods("GET").
SetHeaders(headers2)
resp, err := http.Get("http://" + net.JoinHostPort(host, port))
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()
Expect(resp.Header.Get("Content-Type")).To(Equal("plain/text"))
resp, err = http.Get("http://" + net.JoinHostPort(host, port) + "/foo/bar")
Expect(err).NotTo(HaveOccurred())
Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
})
})
}