Skip to content

Commit 9c4086d

Browse files
authored
Merge pull request #6 from SerayaEryn/allow-no-accept-encoding-header
Allow missing accept-encoding header
2 parents c248ac6 + 16e8e37 commit 9c4086d

File tree

3 files changed

+14
-29
lines changed

3 files changed

+14
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Currently the following headers are supported:
1818
- `'gzip'`
1919
- `'br'`
2020

21-
If an unsupported encoding is received, it will automatically return a `406` error, if the `'accept-encoding'` header is missing, it will return a `400` error.
21+
If an unsupported encoding is received, it will automatically return a `406` error, if the `'accept-encoding'` header is missing, it will not compress the payload.
2222

2323
It automatically defines if a payload should be compressed or not based on its `Content-Type`, if no content type is present, it will assume is `application/json`.
2424

index.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,7 @@ function compressPlugin (fastify, opts, next) {
4545

4646
var encoding = getEncodingHeader(this.request)
4747

48-
if (encoding === undefined) {
49-
closeStream(payload)
50-
this.code(400).send(new Error('Missing `accept encoding` header'))
51-
return
52-
}
53-
54-
if (encoding === 'identity') {
48+
if (encoding === undefined || encoding === 'identity') {
5549
return this.send(payload)
5650
}
5751

@@ -96,21 +90,14 @@ function compressPlugin (fastify, opts, next) {
9690

9791
var encoding = getEncodingHeader(req)
9892

99-
if (encoding === undefined) {
100-
closeStream(payload)
101-
reply.code(400)
102-
next(new Error('Missing `accept encoding` header'))
103-
return
104-
}
105-
10693
if (encoding === null) {
10794
closeStream(payload)
10895
reply.code(406)
10996
next(new Error('Unsupported encoding'))
11097
return
11198
}
11299

113-
if (encoding === 'identity') {
100+
if (encoding === undefined || encoding === 'identity') {
114101
return next()
115102
}
116103

test.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ test('Unsupported encoding', t => {
128128
})
129129
})
130130

131-
test('Missing header', t => {
131+
test('should not compress on missing header', t => {
132132
t.plan(2)
133133
const fastify = Fastify()
134134
fastify.register(compressPlugin, { global: false })
@@ -141,13 +141,8 @@ test('Missing header', t => {
141141
url: '/',
142142
method: 'GET'
143143
}, res => {
144-
const payload = JSON.parse(res.payload)
145-
t.strictEqual(res.statusCode, 400)
146-
t.deepEqual({
147-
error: 'Bad Request',
148-
message: 'Missing `accept encoding` header',
149-
statusCode: 400
150-
}, payload)
144+
t.strictEqual(res.statusCode, 200)
145+
t.notOk(res.headers['content-encoding'])
151146
})
152147
})
153148

@@ -164,14 +159,17 @@ test('Should close the stream', t => {
164159

165160
fastify.inject({
166161
url: '/',
167-
method: 'GET'
162+
method: 'GET',
163+
headers: {
164+
'accept-encoding': 'compress'
165+
}
168166
}, res => {
169167
const payload = JSON.parse(res.payload)
170-
t.strictEqual(res.statusCode, 400)
168+
t.strictEqual(res.statusCode, 406)
171169
t.deepEqual({
172-
error: 'Bad Request',
173-
message: 'Missing `accept encoding` header',
174-
statusCode: 400
170+
error: 'Not Acceptable',
171+
message: 'Unsupported encoding',
172+
statusCode: 406
175173
}, payload)
176174
})
177175
})

0 commit comments

Comments
 (0)