forked from 30x/libgozerian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_http_test.go
272 lines (235 loc) · 8 KB
/
remote_http_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Remote HTTP Tests", func() {
It("Pass GET", func() {
resp, err := http.Get(fmt.Sprintf("%s/pass", testURL))
Expect(err).Should(Succeed())
Expect(resp.StatusCode).Should(Equal(200))
})
It("Pass slow GET", func() {
resp, err := http.Get(fmt.Sprintf("%s/slowpass", testURL))
Expect(err).Should(Succeed())
Expect(resp.StatusCode).Should(Equal(200))
})
It("Return 404", func() {
resp, err := http.Get(fmt.Sprintf("%s/notfound", testURL))
Expect(err).Should(Succeed())
Expect(resp.StatusCode).Should(Equal(404))
})
It("Pass echo back POST", func() {
body := []byte("Hello, World!")
bodyBuf := bytes.NewBuffer(body)
resp, err :=
http.Post(fmt.Sprintf("%s/pass", testURL),
"text/plain", bodyBuf)
Expect(err).Should(Succeed())
Expect(resp.StatusCode).Should(Equal(200))
defer resp.Body.Close()
readBody, err := ioutil.ReadAll(resp.Body)
Expect(err).Should(Succeed())
Expect(bytes.Equal(body, readBody)).Should(BeTrue())
})
It("Discard body POST", func() {
body := []byte("Hello, World!")
bodyBuf := bytes.NewBuffer(body)
resp, err :=
http.Post(fmt.Sprintf("%s/readanddiscard", testURL),
"text/plain", bodyBuf)
Expect(err).Should(Succeed())
Expect(resp.StatusCode).Should(Equal(200))
defer resp.Body.Close()
readBody, err := ioutil.ReadAll(resp.Body)
Expect(err).Should(Succeed())
Expect(bytes.Equal(body, readBody)).Should(BeTrue())
})
It("Replace body POST", func() {
body := []byte("Hello, World!")
bodyBuf := bytes.NewBuffer(body)
resp, err :=
http.Post(fmt.Sprintf("%s/replacebody", testURL),
"text/plain", bodyBuf)
Expect(err).Should(Succeed())
Expect(resp.StatusCode).Should(Equal(200))
defer resp.Body.Close()
readBody, err := ioutil.ReadAll(resp.Body)
Expect(err).Should(Succeed())
expectedBody := []byte("Hello! I am the server!")
fmt.Fprintf(GinkgoWriter, "Body: %s\n", string(readBody))
Expect(bytes.Equal(expectedBody, readBody)).Should(BeTrue())
})
It("Write headers GET", func() {
resp, err := http.Get(fmt.Sprintf("%s/writeheaders", testURL))
Expect(err).Should(Succeed())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(200))
// TODO check for something!
})
It("Write path GET", func() {
resp, err := http.Get(fmt.Sprintf("%s/writepath", testURL))
Expect(err).Should(Succeed())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(200))
// TODO check for something!
})
It("Return 201 GET", func() {
resp, err := http.Get(fmt.Sprintf("%s/return201", testURL))
Expect(err).Should(Succeed())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(201))
})
It("Return Headers GET", func() {
resp, err := http.Get(fmt.Sprintf("%s/returnheaders", testURL))
Expect(err).Should(Succeed())
Expect(resp.StatusCode).Should(Equal(200))
Expect(resp.Header.Get("X-Apigee-Test")).Should(Equal("Return Header Test"))
})
It("Return Body GET", func() {
resp, err := http.Get(fmt.Sprintf("%s/returnbody", testURL))
Expect(err).Should(Succeed())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(200))
body, err := ioutil.ReadAll(resp.Body)
Expect(err).Should(Succeed())
expectedBody := []byte("Hello! I am the server!")
Expect(bytes.Equal(expectedBody, body)).Should(BeTrue())
})
It("Return MessageID GET", func() {
resp, err := http.Get(fmt.Sprintf("%s/replacewithid", testURL))
Expect(err).Should(Succeed())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(200))
body, err := ioutil.ReadAll(resp.Body)
Expect(err).Should(Succeed())
msgID := string(body)
fmt.Fprintf(GinkgoWriter, "Message ID: \"%s\"\n", msgID)
Expect(resp.Header.Get("X-Apigee-MsgID")).Should(Equal(msgID))
})
It("Complete request POST", func() {
reqBody := []byte("Hello, World!")
bodyBuf := bytes.NewBuffer(reqBody)
resp, err :=
http.Post(fmt.Sprintf("%s/completerequest", testURL),
"text/plain", bodyBuf)
Expect(err).Should(Succeed())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(200))
body, err := ioutil.ReadAll(resp.Body)
Expect(err).Should(Succeed())
expectedBody := []byte("Hello Again! Time for a complete rewrite!")
Expect(bytes.Equal(expectedBody, body)).Should(BeTrue())
})
It("Complete response GET", func() {
resp, err := http.Get(fmt.Sprintf("%s/completeresponse", testURL))
Expect(err).Should(Succeed())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(http.StatusCreated))
Expect(resp.Header.Get("X-Apigee-Test")).Should(Equal("Complete"))
body, err := ioutil.ReadAll(resp.Body)
Expect(err).Should(Succeed())
expectedBody := []byte("Hello Again! Time for a complete rewrite!")
Expect(bytes.Equal(expectedBody, body)).Should(BeTrue())
})
It("Write response headers GET", func() {
resp, err := http.Get(fmt.Sprintf("%s/writeresponseheaders", testURL))
Expect(err).Should(Succeed())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(200))
Expect(resp.Header.Get("X-Apigee-ResponseHeader")).Should(Equal("yes"))
})
It("Write response body POST", func() {
reqBody := []byte("Hello, World!")
bodyBuf := bytes.NewBuffer(reqBody)
resp, err :=
http.Post(fmt.Sprintf("%s/transformbody", testURL),
"text/plain", bodyBuf)
Expect(err).Should(Succeed())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(200))
body, err := ioutil.ReadAll(resp.Body)
Expect(err).Should(Succeed())
expectedBody := []byte("We have transformed the response!")
fmt.Fprintf(GinkgoWriter, "Transformed body: %s\n", string(body))
Expect(bytes.Equal(expectedBody, body)).Should(BeTrue())
})
It("Write response status GET", func() {
resp, err := http.Get(fmt.Sprintf("%s/responseerror", testURL))
Expect(err).Should(Succeed())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(http.StatusInternalServerError))
body, err := ioutil.ReadAll(resp.Body)
Expect(err).Should(Succeed())
expectedBody := []byte("Error in the server!")
fmt.Fprintf(GinkgoWriter, "Transformed body: %s\n", string(body))
Expect(bytes.Equal(expectedBody, body)).Should(BeTrue())
})
It("Write response status GET 2", func() {
resp, err := http.Get(fmt.Sprintf("%s/responseerror2", testURL))
Expect(err).Should(Succeed())
defer resp.Body.Close()
Expect(resp.StatusCode).Should(Equal(http.StatusGatewayTimeout))
Expect(resp.Header.Get("X-Apigee-Response")).Should(Equal("error"))
body, err := ioutil.ReadAll(resp.Body)
Expect(err).Should(Succeed())
expectedBody := []byte("Response Error")
fmt.Fprintf(GinkgoWriter, "Transformed body: %s\n", string(body))
Expect(bytes.Equal(expectedBody, body)).Should(BeTrue())
})
It("Transform response body POST", func() {
err := testResponseTransformation()
Expect(err).Should(Succeed())
})
It("Transform response body in parallel", func() {
testParallelTransformation(2, 10)
})
It("Transform response body in more parallel", func() {
testParallelTransformation(100, 10)
})
})
func testResponseTransformation() error {
reqBody := []byte("Hello, World!")
bodyBuf := bytes.NewBuffer(reqBody)
resp, err :=
http.Post(fmt.Sprintf("%s/transformbodychunks", testURL),
"text/plain", bodyBuf)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Invalid status code: %d", resp.StatusCode)
}
if resp.Header.Get("X-Apigee-Transformed") != "yes" {
return fmt.Errorf("X-Apigee-Transformed header not set")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if string(body) != "{Hello, World!}" {
return fmt.Errorf("Body is invalid: \"%s\"", string(body))
}
return nil
}
func testParallelTransformation(concurrency, count int) {
doneCh := make(chan error, concurrency)
for c := 0; c < concurrency; c++ {
go func() {
var err error
for i := 0; err != nil && i < count; i++ {
err = testResponseTransformation()
}
doneCh <- err
}()
}
for c := 0; c < concurrency; c++ {
err := <-doneCh
Expect(err).Should(Succeed())
}
}