Skip to content

Commit

Permalink
Use identity as default value for Accept-Encoding if it is blank or n…
Browse files Browse the repository at this point in the history
…ot provided (#110)
  • Loading branch information
dobesv authored Jul 6, 2020
1 parent cce99c4 commit 3d4c0ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,30 @@ describe('Compress', () => {
.expect(200, done)
})

it('should not crash if no accept-encoding is sent', (done) => {
it('should not compress if no accept-encoding is sent', (done) => {
const app = new Koa()

app.use(compress())
app.use(sendBuffer)
app.use(compress({
threshold: 0
}))
app.use((ctx) => {
ctx.type = 'text'
ctx.body = buffer
})
server = app.listen()

request(server)
.get('/')
.expect(200, done)
.unset('accept-encoding')
.end((err, res) => {
if (err) { return done(err) }

assert(!res.headers['content-encoding'])
assert(!res.headers['transfer-encoding'])
assert.equal(res.headers['content-length'], '1024')
assert.equal(res.headers.vary, 'Accept-Encoding')

done()
})
})

it('should not crash if a type does not pass the filter', (done) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = (options = {}) => {
const encodings = new Encodings({
preferredEncodings
})
encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || undefined)
encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || 'identity')
const encoding = encodings.getPreferredContentEncoding()

// identity === no compression
Expand Down

0 comments on commit 3d4c0ac

Please sign in to comment.