@@ -2,18 +2,26 @@ package content_blocker_plugin_test
2
2
3
3
import (
4
4
"bytes"
5
+ "fmt"
5
6
"net/http"
6
7
"strconv"
7
8
"testing"
8
9
9
10
"github.com/fullstorydev/relay-core/catcher"
10
11
"github.com/fullstorydev/relay-core/relay"
11
- "github.com/fullstorydev/relay-core/relay/plugins/traffic/content-blocker-plugin"
12
+ content_blocker_plugin "github.com/fullstorydev/relay-core/relay/plugins/traffic/content-blocker-plugin"
12
13
"github.com/fullstorydev/relay-core/relay/test"
13
14
"github.com/fullstorydev/relay-core/relay/traffic"
14
15
"github.com/fullstorydev/relay-core/relay/version"
15
16
)
16
17
18
+ type Encoding int
19
+
20
+ const (
21
+ Identity Encoding = iota
22
+ Gzip
23
+ )
24
+
17
25
func TestContentBlocking (t * testing.T ) {
18
26
testCases := []contentBlockerTestCase {
19
27
{
@@ -133,7 +141,8 @@ func TestContentBlocking(t *testing.T) {
133
141
}
134
142
135
143
for _ , testCase := range testCases {
136
- runContentBlockerTest (t , testCase )
144
+ runContentBlockerTest (t , testCase , Identity )
145
+ runContentBlockerTest (t , testCase , Gzip )
137
146
}
138
147
}
139
148
@@ -185,7 +194,18 @@ type contentBlockerTestCase struct {
185
194
expectedHeaders map [string ]string
186
195
}
187
196
188
- func runContentBlockerTest (t * testing.T , testCase contentBlockerTestCase ) {
197
+ func runContentBlockerTest (t * testing.T , testCase contentBlockerTestCase , encoding Encoding ) {
198
+ var encodingStr string
199
+ switch encoding {
200
+ case Gzip :
201
+ encodingStr = "gzip"
202
+ case Identity :
203
+ encodingStr = ""
204
+ }
205
+
206
+ // Add encoding to the test description
207
+ desc := fmt .Sprintf ("%s (encoding: %v)" , testCase .desc , encodingStr )
208
+
189
209
plugins := []traffic.PluginFactory {
190
210
content_blocker_plugin .Factory ,
191
211
}
@@ -203,36 +223,46 @@ func runContentBlockerTest(t *testing.T, testCase contentBlockerTestCase) {
203
223
expectedHeaders [content_blocker_plugin .PluginVersionHeaderName ] = version .RelayRelease
204
224
205
225
test .WithCatcherAndRelay (t , testCase .config , plugins , func (catcherService * catcher.Service , relayService * relay.Service ) {
226
+ b , err := traffic .EncodeData ([]byte (testCase .originalBody ), encodingStr )
227
+ if err != nil {
228
+ t .Errorf ("Test '%v': Error encoding data: %v" , desc , err )
229
+ return
230
+ }
231
+
206
232
request , err := http .NewRequest (
207
233
"POST" ,
208
234
relayService .HttpUrl (),
209
- bytes .NewBufferString ( testCase . originalBody ),
235
+ bytes .NewBuffer ( b ),
210
236
)
211
237
if err != nil {
212
- t .Errorf ("Test '%v': Error creating request: %v" , testCase . desc , err )
238
+ t .Errorf ("Test '%v': Error creating request: %v" , desc , err )
213
239
return
214
240
}
215
241
242
+ if encoding == Gzip {
243
+ request .Header .Set ("Content-Encoding" , "gzip" )
244
+ }
245
+
216
246
request .Header .Set ("Content-Type" , "application/json" )
217
247
for header , headerValue := range originalHeaders {
218
248
request .Header .Set (header , headerValue )
219
249
}
220
250
221
251
response , err := http .DefaultClient .Do (request )
222
252
if err != nil {
223
- t .Errorf ("Test '%v': Error POSTing: %v" , testCase . desc , err )
253
+ t .Errorf ("Test '%v': Error POSTing: %v" , desc , err )
224
254
return
225
255
}
226
256
defer response .Body .Close ()
227
257
228
258
if response .StatusCode != 200 {
229
- t .Errorf ("Test '%v': Expected 200 response: %v" , testCase . desc , response )
259
+ t .Errorf ("Test '%v': Expected 200 response: %v" , desc , response )
230
260
return
231
261
}
232
262
233
263
lastRequest , err := catcherService .LastRequest ()
234
264
if err != nil {
235
- t .Errorf ("Test '%v': Error reading last request from catcher: %v" , testCase . desc , err )
265
+ t .Errorf ("Test '%v': Error reading last request from catcher: %v" , desc , err )
236
266
return
237
267
}
238
268
@@ -241,43 +271,58 @@ func runContentBlockerTest(t *testing.T, testCase contentBlockerTestCase) {
241
271
if expectedHeaderValue != actualHeaderValue {
242
272
t .Errorf (
243
273
"Test '%v': Expected header '%v' with value '%v' but got: %v" ,
244
- testCase . desc ,
274
+ desc ,
245
275
expectedHeader ,
246
276
expectedHeaderValue ,
247
277
actualHeaderValue ,
248
278
)
249
279
}
250
280
}
251
281
282
+ if lastRequest .Header .Get ("Content-Encoding" ) != encodingStr {
283
+ t .Errorf (
284
+ "Test '%v': Expected Content-Encoding '%v' but got: %v" ,
285
+ desc ,
286
+ encodingStr ,
287
+ lastRequest .Header .Get ("Content-Encoding" ),
288
+ )
289
+ }
290
+
252
291
lastRequestBody , err := catcherService .LastRequestBody ()
253
292
if err != nil {
254
- t .Errorf ("Test '%v': Error reading last request body from catcher: %v" , testCase . desc , err )
293
+ t .Errorf ("Test '%v': Error reading last request body from catcher: %v" , desc , err )
255
294
return
256
295
}
257
296
258
- lastRequestBodyStr := string (lastRequestBody )
259
- if testCase .expectedBody != lastRequestBodyStr {
260
- t .Errorf (
261
- "Test '%v': Expected body '%v' but got: %v" ,
262
- testCase .desc ,
263
- testCase .expectedBody ,
264
- lastRequestBodyStr ,
265
- )
266
- }
267
-
268
297
contentLength , err := strconv .Atoi (lastRequest .Header .Get ("Content-Length" ))
269
298
if err != nil {
270
- t .Errorf ("Test '%v': Error parsing Content-Length: %v" , testCase . desc , err )
299
+ t .Errorf ("Test '%v': Error parsing Content-Length: %v" , desc , err )
271
300
return
272
301
}
273
302
274
303
if contentLength != len (lastRequestBody ) {
275
304
t .Errorf (
276
305
"Test '%v': Content-Length is %v but actual body length is %v" ,
277
- testCase . desc ,
306
+ desc ,
278
307
contentLength ,
279
308
len (lastRequestBody ),
280
309
)
281
310
}
311
+
312
+ decodedRequestBody , err := traffic .DecodeData (lastRequestBody , encodingStr )
313
+ if err != nil {
314
+ t .Errorf ("Test '%v': Error decoding data: %v" , desc , err )
315
+ return
316
+ }
317
+
318
+ lastRequestBodyStr := string (decodedRequestBody )
319
+ if testCase .expectedBody != lastRequestBodyStr {
320
+ t .Errorf (
321
+ "Test '%v': Expected body '%v' but got: %v" ,
322
+ desc ,
323
+ testCase .expectedBody ,
324
+ lastRequestBodyStr ,
325
+ )
326
+ }
282
327
})
283
328
}
0 commit comments