1- const nock = require ( 'nock' ) ;
2-
31describe ( 'brotli middleware' , ( ) => {
42 beforeEach ( ( ) => {
53 jest . resetModules ( ) ;
6- nock . cleanAll ( ) ;
74 } ) ;
85
9- test ( 'returns null response when brotli is unsupported ' , async ( ) => {
6+ test ( 'does not change request when brotli is not supported ' , async ( ) => {
107 jest . mock ( '../../../lib/brotli/isSupported' , ( ) => {
118 return jest . fn ( ( ) => false ) ;
129 } ) ;
1310
14- const middleware = require ( '../../../lib/brotli/middleware' ) ;
15- const response = await middleware ( { } ) ;
16-
17- expect ( response ) . toEqual ( null ) ;
18- } ) ;
19-
20- test ( 'returns response object when brotli is supported' , async ( ) => {
21- jest . mock ( '../../../lib/brotli/isSupported' , ( ) => {
22- return jest . fn ( ( ) => true ) ;
23- } ) ;
24- jest . mock ( '../../../lib/brotli/compress' , ( ) => {
25- return jest . fn ( input => global . Buffer . from ( input ) ) ;
26- } ) ;
27- jest . mock ( '../../../lib/getOriginUrl' , ( ) => {
28- return jest . fn ( ( ) => 'http://example.com/brotli-supported' ) ;
29- } ) ;
30- nock ( 'http://example.com' )
31- . get ( '/brotli-supported' )
32- . reply ( 200 , '' , { 'content-type' : 'text/html' } ) ;
11+ const request = Object . freeze ( { } ) ;
12+ const response = { body : '' } ;
3313
3414 const middleware = require ( '../../../lib/brotli/middleware' ) ;
3515
36- const response = await middleware ( { } , { headers : { } } ) ;
37- expect ( Object . keys ( response ) . length ) . toBeGreaterThan ( 0 ) ;
16+ expect ( middleware ( { request, response } ) ) . resolves . toEqual ( {
17+ request,
18+ response,
19+ } ) ;
3820 } ) ;
3921
40- test ( 'does not modify request object when brotli is unsupported ' , async ( ) => {
22+ test ( 'does not change response when brotli is not supported ' , async ( ) => {
4123 jest . mock ( '../../../lib/brotli/isSupported' , ( ) => {
4224 return jest . fn ( ( ) => false ) ;
4325 } ) ;
4426
45- const middleware = require ( '../../../lib/brotli/middleware' ) ;
4627 const request = { } ;
47-
48- await middleware ( request , { } ) ;
49- expect ( { } ) . toEqual ( { } ) ;
50- } ) ;
51-
52- test ( 'does not modify response object when brotli is unsupported' , async ( ) => {
53- jest . mock ( '../../../lib/brotli/isSupported' , ( ) => {
54- return jest . fn ( ( ) => false ) ;
55- } ) ;
28+ const response = Object . freeze ( { body : '' } ) ;
5629
5730 const middleware = require ( '../../../lib/brotli/middleware' ) ;
58- const response = { } ;
5931
60- await middleware ( response , { } ) ;
61- expect ( response ) . toEqual ( { } ) ;
32+ expect ( middleware ( { request, response } ) ) . resolves . toEqual ( {
33+ request,
34+ response,
35+ } ) ;
6236 } ) ;
6337
64- test ( 'sets proper content-encoding header' , async ( ) => {
38+ test ( 'sets proper Content-Encoding header when supported ' , async ( ) => {
6539 jest . mock ( '../../../lib/brotli/isSupported' , ( ) => {
6640 return jest . fn ( ( ) => true ) ;
6741 } ) ;
6842 jest . mock ( '../../../lib/brotli/compress' , ( ) => {
6943 return jest . fn ( input => global . Buffer . from ( input ) ) ;
7044 } ) ;
71- jest . mock ( '../../../lib/getOriginUrl' , ( ) => {
72- return jest . fn ( ( ) => 'http://example.com/content-encoding' ) ;
73- } ) ;
74- nock ( 'http://example.com' )
75- . get ( '/content-encoding' )
76- . reply ( 200 , '' , { 'content-type' : 'text/html' } ) ;
7745
7846 const middleware = require ( '../../../lib/brotli/middleware' ) ;
79- const response = await middleware ( { } , { headers : { } } ) ;
47+ const { response } = await middleware ( {
48+ request : { } ,
49+ response : { body : '' } ,
50+ } ) ;
8051
8152 expect ( response . headers ) . toEqual ( {
8253 'content-encoding' : [ { key : 'Content-Encoding' , value : 'br' } ] ,
83- 'content-type' : [ { key : 'Content-Type' , value : 'text/html' } ] ,
84- 'x-orig-size' : [
85- {
86- key : 'X-Orig-Size' ,
87- value : '0' ,
88- } ,
89- ] ,
9054 } ) ;
9155 } ) ;
9256
93- test ( 'returns response for null argument' , async ( ) => {
94- jest . mock ( '../../../lib/brotli/isSupported' , ( ) => {
95- return jest . fn ( ( ) => true ) ;
96- } ) ;
97- jest . mock ( '../../../lib/brotli/compress' , ( ) => {
98- return jest . fn ( input => global . Buffer . from ( input ) ) ;
99- } ) ;
100- jest . mock ( '../../../lib/getOriginUrl' , ( ) => {
101- return jest . fn ( ( ) => 'http://example.com/null-parameter' ) ;
102- } ) ;
103- nock ( 'http://example.com' )
104- . get ( '/null-parameter' )
105- . reply ( 200 , '' , { 'content-type' : 'text/html' } ) ;
106-
107- const middleware = require ( '../../../lib/brotli/middleware' ) ;
108- const response = await middleware ( { } , null ) ;
109-
110- expect ( response ) . not . toEqual ( null ) ;
111- } ) ;
112-
11357 test ( 'response body matches snapshot when supported' , async ( ) => {
11458 jest . mock ( '../../../lib/brotli/isSupported' , ( ) => {
11559 return jest . fn ( ( ) => true ) ;
11660 } ) ;
11761 jest . mock ( '../../../lib/brotli/compress' , ( ) => {
11862 return jest . fn ( input => global . Buffer . from ( input ) ) ;
11963 } ) ;
120- jest . mock ( '../../../lib/getOriginUrl' , ( ) => {
121- return jest . fn ( ( ) => 'http://example.com/response-body-snapshot' ) ;
122- } ) ;
123- nock ( 'http://example.com' )
124- . get ( '/response-body-snapshot' )
125- . reply ( 200 , '' , { 'content-type' : 'text/html' } ) ;
12664
12765 const middleware = require ( '../../../lib/brotli/middleware' ) ;
128- const response = await middleware ( { } , { headers : { } } ) ;
66+ const { response } = await middleware ( {
67+ request : { } ,
68+ response : { body : '' } ,
69+ } ) ;
12970
13071 expect ( response . body ) . toMatchSnapshot ( ) ;
13172 } ) ;
@@ -137,37 +78,15 @@ describe('brotli middleware', () => {
13778 jest . mock ( '../../../lib/brotli/compress' , ( ) => {
13879 return jest . fn ( input => global . Buffer . from ( input ) ) ;
13980 } ) ;
140- jest . mock ( '../../../lib/getOriginUrl' , ( ) => {
141- return jest . fn (
142- ( ) => 'http://example.com/response-headers-snapshot'
143- ) ;
144- } ) ;
145- nock ( 'http://example.com' )
146- . get ( '/response-headers-snapshot' )
147- . reply ( 200 , '' , { 'content-type' : 'text/html' } ) ;
14881
14982 const middleware = require ( '../../../lib/brotli/middleware' ) ;
15083
151- const response = await middleware ( { } , { headers : { } } ) ;
152-
153- expect ( response . headers ) . toMatchSnapshot ( ) ;
154- } ) ;
155-
156- test ( 'rejects promise when server errors' , async ( ) => {
157- jest . mock ( '../../../lib/brotli/isSupported' , ( ) => {
158- return jest . fn ( ( ) => true ) ;
159- } ) ;
160- jest . mock ( '../../../lib/getOriginUrl' , ( ) => {
161- return jest . fn ( ( ) => 'http://example.com/server-error' ) ;
84+ const { response } = await middleware ( {
85+ request : { } ,
86+ response : { body : '' } ,
16287 } ) ;
16388
164- nock ( 'http://example.com' )
165- . get ( '/server-error' )
166- . reply ( 503 , '' ) ;
167-
168- const middleware = require ( '../../../lib/brotli/middleware' ) ;
169-
170- expect ( middleware ( { } , { headers : { } } ) ) . rejects . toThrow ( ) ;
89+ expect ( response . headers ) . toMatchSnapshot ( ) ;
17190 } ) ;
17291
17392 test ( 'rejects promise when compression errors' , async ( ) => {
@@ -179,40 +98,11 @@ describe('brotli middleware', () => {
17998 throw new Error ( ) ;
18099 } ) ;
181100 } ) ;
182- jest . mock ( '../../../lib/getOriginUrl' , ( ) => {
183- return jest . fn ( ( ) => 'http://example.com/compression-error' ) ;
184- } ) ;
185-
186- nock ( 'http://example.com' )
187- . get ( '/compression-error' )
188- . reply ( 200 , '' , { 'content-type' : 'image/png' } ) ;
189-
190- const middleware = require ( '../../../lib/brotli/middleware' ) ;
191-
192- expect ( middleware ( { } , { headers : { } } ) ) . rejects . toThrow ( ) ;
193- } ) ;
194-
195- test ( 'forwards origin response headers when supported' , async ( ) => {
196- jest . mock ( '../../../lib/brotli/isSupported' , ( ) => {
197- return jest . fn ( ( ) => true ) ;
198- } ) ;
199- jest . mock ( '../../../lib/brotli/compress' , ( ) => {
200- return jest . fn ( input => global . Buffer . from ( input ) ) ;
201- } ) ;
202- jest . mock ( '../../../lib/getOriginUrl' , ( ) => {
203- return jest . fn ( ( ) => 'http://example.com/response-headers-forward' ) ;
204- } ) ;
205- nock ( 'http://example.com' )
206- . get ( '/response-headers-forward' )
207- . reply ( 200 , '' , {
208- 'content-type' : 'text/html' ,
209- 'access-control-allow-origin' : '*' ,
210- } ) ;
211101
212102 const middleware = require ( '../../../lib/brotli/middleware' ) ;
213103
214- const response = await middleware ( { } , { headers : { } } ) ;
215-
216- expect ( response . headers [ 'access-control-allow-origin' ] ) . toBeTruthy ( ) ;
104+ expect (
105+ middleware ( { request : { } , response : { body : '' } } )
106+ ) . rejects . toThrow ( ) ;
217107 } ) ;
218108} ) ;
0 commit comments