Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Accept-Encoding header should not result in compression #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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