Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Commit 48887ae

Browse files
author
Mateusz Krzeszowiak
committed
Refactor whole lambda, add support for brotli
1 parent 1ba20ba commit 48887ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1808
-3112
lines changed

.nvmrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

__tests__/lib/brotli/__snapshots__/middleware.test.js.snap

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,5 @@ Object {
1010
"value": "br",
1111
},
1212
],
13-
"content-type": Array [
14-
Object {
15-
"key": "Content-Type",
16-
"value": "text/html",
17-
},
18-
],
19-
"x-orig-size": Array [
20-
Object {
21-
"key": "X-Orig-Size",
22-
"value": "0",
23-
},
24-
],
2513
}
2614
`;

__tests__/lib/brotli/isSupported.test.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('brotli isSupported', () => {
55
jest.resetModules();
66
});
77

8-
test('returns false when no x-compression header is present', () => {
8+
test('returns false when no accept-encoding header is present', () => {
99
const request = {
1010
headers: {},
1111
uri: 'test.js',
@@ -23,7 +23,7 @@ describe('brotli isSupported', () => {
2323
test('returns false when URI is missing', () => {
2424
const request = {
2525
headers: {
26-
'x-compression': [{ value: 'deflate' }],
26+
'accept-encoding': [{ value: 'deflate' }],
2727
},
2828
};
2929
expect(isSupported(request)).toBe(false);
@@ -33,20 +33,20 @@ describe('brotli isSupported', () => {
3333
expect(isSupported({})).toBe(false);
3434
});
3535

36-
test('returns false when x-compression does not contain br', () => {
36+
test('returns false when accept-encoding does not contain br', () => {
3737
const request = {
3838
headers: {
39-
'x-compression': [{ value: 'deflate' }],
39+
'accept-encoding': [{ value: 'deflate' }],
4040
},
4141
uri: 'test.js',
4242
};
4343
expect(isSupported(request)).toBe(false);
4444
});
4545

46-
test('returns true when x-compression contains br and file extension is supported', () => {
46+
test('returns true when accept-encoding contains br and file extension is supported', () => {
4747
const request = {
4848
headers: {
49-
'x-compression': [
49+
'accept-encoding': [
5050
{ value: 'br' },
5151
{ value: 'gzip' },
5252
{ value: 'deflate' },
@@ -57,13 +57,23 @@ describe('brotli isSupported', () => {
5757
expect(isSupported(request)).toBe(true);
5858
});
5959

60-
test('returns true when x-compression contains br but file extension is unsupported', () => {
60+
test('returns true when accept-encoding is string, contains br and file extension is supported', () => {
6161
const request = {
6262
headers: {
63-
'x-compression': [{ value: 'br' }],
63+
'accept-encoding': [{ value: 'gzip br deflate' }],
6464
},
6565
uri: 'test.js',
6666
};
6767
expect(isSupported(request)).toBe(true);
6868
});
69+
70+
test('returns false when accept-encoding contains br but file extension is unsupported', () => {
71+
const request = {
72+
headers: {
73+
'accept-encoding': [{ value: 'br' }],
74+
},
75+
uri: 'test.jpeg',
76+
};
77+
expect(isSupported(request)).toBe(false);
78+
});
6979
});
Lines changed: 29 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,72 @@
1-
const nock = require('nock');
2-
31
describe('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

Comments
 (0)