Skip to content

Commit 94b54c2

Browse files
committed
update blocking tests to asset they work with gzipped data
1 parent dc153b5 commit 94b54c2

File tree

1 file changed

+67
-22
lines changed

1 file changed

+67
-22
lines changed

relay/plugins/traffic/content-blocker-plugin/content-blocker-plugin_test.go

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@ package content_blocker_plugin_test
22

33
import (
44
"bytes"
5+
"fmt"
56
"net/http"
67
"strconv"
78
"testing"
89

910
"github.com/fullstorydev/relay-core/catcher"
1011
"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"
1213
"github.com/fullstorydev/relay-core/relay/test"
1314
"github.com/fullstorydev/relay-core/relay/traffic"
1415
"github.com/fullstorydev/relay-core/relay/version"
1516
)
1617

18+
type Encoding int
19+
20+
const (
21+
Identity Encoding = iota
22+
Gzip
23+
)
24+
1725
func TestContentBlocking(t *testing.T) {
1826
testCases := []contentBlockerTestCase{
1927
{
@@ -133,7 +141,8 @@ func TestContentBlocking(t *testing.T) {
133141
}
134142

135143
for _, testCase := range testCases {
136-
runContentBlockerTest(t, testCase)
144+
runContentBlockerTest(t, testCase, Identity)
145+
runContentBlockerTest(t, testCase, Gzip)
137146
}
138147
}
139148

@@ -185,7 +194,18 @@ type contentBlockerTestCase struct {
185194
expectedHeaders map[string]string
186195
}
187196

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+
189209
plugins := []traffic.PluginFactory{
190210
content_blocker_plugin.Factory,
191211
}
@@ -203,36 +223,46 @@ func runContentBlockerTest(t *testing.T, testCase contentBlockerTestCase) {
203223
expectedHeaders[content_blocker_plugin.PluginVersionHeaderName] = version.RelayRelease
204224

205225
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+
206232
request, err := http.NewRequest(
207233
"POST",
208234
relayService.HttpUrl(),
209-
bytes.NewBufferString(testCase.originalBody),
235+
bytes.NewBuffer(b),
210236
)
211237
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)
213239
return
214240
}
215241

242+
if encoding == Gzip {
243+
request.Header.Set("Content-Encoding", "gzip")
244+
}
245+
216246
request.Header.Set("Content-Type", "application/json")
217247
for header, headerValue := range originalHeaders {
218248
request.Header.Set(header, headerValue)
219249
}
220250

221251
response, err := http.DefaultClient.Do(request)
222252
if err != nil {
223-
t.Errorf("Test '%v': Error POSTing: %v", testCase.desc, err)
253+
t.Errorf("Test '%v': Error POSTing: %v", desc, err)
224254
return
225255
}
226256
defer response.Body.Close()
227257

228258
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)
230260
return
231261
}
232262

233263
lastRequest, err := catcherService.LastRequest()
234264
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)
236266
return
237267
}
238268

@@ -241,43 +271,58 @@ func runContentBlockerTest(t *testing.T, testCase contentBlockerTestCase) {
241271
if expectedHeaderValue != actualHeaderValue {
242272
t.Errorf(
243273
"Test '%v': Expected header '%v' with value '%v' but got: %v",
244-
testCase.desc,
274+
desc,
245275
expectedHeader,
246276
expectedHeaderValue,
247277
actualHeaderValue,
248278
)
249279
}
250280
}
251281

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+
252291
lastRequestBody, err := catcherService.LastRequestBody()
253292
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)
255294
return
256295
}
257296

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-
268297
contentLength, err := strconv.Atoi(lastRequest.Header.Get("Content-Length"))
269298
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)
271300
return
272301
}
273302

274303
if contentLength != len(lastRequestBody) {
275304
t.Errorf(
276305
"Test '%v': Content-Length is %v but actual body length is %v",
277-
testCase.desc,
306+
desc,
278307
contentLength,
279308
len(lastRequestBody),
280309
)
281310
}
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+
}
282327
})
283328
}

0 commit comments

Comments
 (0)