@@ -2,13 +2,14 @@ package content_blocker_plugin_test
22
33import (
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"
@@ -133,7 +134,8 @@ func TestContentBlocking(t *testing.T) {
133134 }
134135
135136 for _ , testCase := range testCases {
136- runContentBlockerTest (t , testCase )
137+ runContentBlockerTest (t , testCase , traffic .Identity )
138+ runContentBlockerTest (t , testCase , traffic .Gzip )
137139 }
138140}
139141
@@ -185,7 +187,18 @@ type contentBlockerTestCase struct {
185187 expectedHeaders map [string ]string
186188}
187189
188- func runContentBlockerTest (t * testing.T , testCase contentBlockerTestCase ) {
190+ func runContentBlockerTest (t * testing.T , testCase contentBlockerTestCase , encoding traffic.Encoding ) {
191+ var encodingStr string
192+ switch encoding {
193+ case traffic .Gzip :
194+ encodingStr = "gzip"
195+ case traffic .Identity :
196+ encodingStr = ""
197+ }
198+
199+ // Add encoding to the test description
200+ desc := fmt .Sprintf ("%s (encoding: %v)" , testCase .desc , encodingStr )
201+
189202 plugins := []traffic.PluginFactory {
190203 content_blocker_plugin .Factory ,
191204 }
@@ -203,36 +216,46 @@ func runContentBlockerTest(t *testing.T, testCase contentBlockerTestCase) {
203216 expectedHeaders [content_blocker_plugin .PluginVersionHeaderName ] = version .RelayRelease
204217
205218 test .WithCatcherAndRelay (t , testCase .config , plugins , func (catcherService * catcher.Service , relayService * relay.Service ) {
219+ b , err := traffic .EncodeData ([]byte (testCase .originalBody ), encoding )
220+ if err != nil {
221+ t .Errorf ("Test '%v': Error encoding data: %v" , desc , err )
222+ return
223+ }
224+
206225 request , err := http .NewRequest (
207226 "POST" ,
208227 relayService .HttpUrl (),
209- bytes .NewBufferString ( testCase . originalBody ),
228+ bytes .NewBuffer ( b ),
210229 )
211230 if err != nil {
212- t .Errorf ("Test '%v': Error creating request: %v" , testCase . desc , err )
231+ t .Errorf ("Test '%v': Error creating request: %v" , desc , err )
213232 return
214233 }
215234
235+ if encoding == traffic .Gzip {
236+ request .Header .Set ("Content-Encoding" , "gzip" )
237+ }
238+
216239 request .Header .Set ("Content-Type" , "application/json" )
217240 for header , headerValue := range originalHeaders {
218241 request .Header .Set (header , headerValue )
219242 }
220243
221244 response , err := http .DefaultClient .Do (request )
222245 if err != nil {
223- t .Errorf ("Test '%v': Error POSTing: %v" , testCase . desc , err )
246+ t .Errorf ("Test '%v': Error POSTing: %v" , desc , err )
224247 return
225248 }
226249 defer response .Body .Close ()
227250
228251 if response .StatusCode != 200 {
229- t .Errorf ("Test '%v': Expected 200 response: %v" , testCase . desc , response )
252+ t .Errorf ("Test '%v': Expected 200 response: %v" , desc , response )
230253 return
231254 }
232255
233256 lastRequest , err := catcherService .LastRequest ()
234257 if err != nil {
235- t .Errorf ("Test '%v': Error reading last request from catcher: %v" , testCase . desc , err )
258+ t .Errorf ("Test '%v': Error reading last request from catcher: %v" , desc , err )
236259 return
237260 }
238261
@@ -241,43 +264,58 @@ func runContentBlockerTest(t *testing.T, testCase contentBlockerTestCase) {
241264 if expectedHeaderValue != actualHeaderValue {
242265 t .Errorf (
243266 "Test '%v': Expected header '%v' with value '%v' but got: %v" ,
244- testCase . desc ,
267+ desc ,
245268 expectedHeader ,
246269 expectedHeaderValue ,
247270 actualHeaderValue ,
248271 )
249272 }
250273 }
251274
275+ if lastRequest .Header .Get ("Content-Encoding" ) != encodingStr {
276+ t .Errorf (
277+ "Test '%v': Expected Content-Encoding '%v' but got: %v" ,
278+ desc ,
279+ encodingStr ,
280+ lastRequest .Header .Get ("Content-Encoding" ),
281+ )
282+ }
283+
252284 lastRequestBody , err := catcherService .LastRequestBody ()
253285 if err != nil {
254- t .Errorf ("Test '%v': Error reading last request body from catcher: %v" , testCase . desc , err )
286+ t .Errorf ("Test '%v': Error reading last request body from catcher: %v" , desc , err )
255287 return
256288 }
257289
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-
268290 contentLength , err := strconv .Atoi (lastRequest .Header .Get ("Content-Length" ))
269291 if err != nil {
270- t .Errorf ("Test '%v': Error parsing Content-Length: %v" , testCase . desc , err )
292+ t .Errorf ("Test '%v': Error parsing Content-Length: %v" , desc , err )
271293 return
272294 }
273295
274296 if contentLength != len (lastRequestBody ) {
275297 t .Errorf (
276298 "Test '%v': Content-Length is %v but actual body length is %v" ,
277- testCase . desc ,
299+ desc ,
278300 contentLength ,
279301 len (lastRequestBody ),
280302 )
281303 }
304+
305+ decodedRequestBody , err := traffic .DecodeData (lastRequestBody , encoding )
306+ if err != nil {
307+ t .Errorf ("Test '%v': Error decoding data: %v" , desc , err )
308+ return
309+ }
310+
311+ lastRequestBodyStr := string (decodedRequestBody )
312+ if testCase .expectedBody != lastRequestBodyStr {
313+ t .Errorf (
314+ "Test '%v': Expected body '%v' but got: %v" ,
315+ desc ,
316+ testCase .expectedBody ,
317+ lastRequestBodyStr ,
318+ )
319+ }
282320 })
283321}
0 commit comments