Skip to content

Commit 895fbe8

Browse files
committed
tests++ to check #107
1 parent 9200434 commit 895fbe8

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

__tests__/middleware.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const request = require('supertest')
2+
const crypto = require('crypto')
3+
const assert = require('assert')
4+
const Koa = require('koa')
5+
6+
const compress = require('..')
7+
8+
let server
9+
10+
afterEach(() => server && server.close())
11+
12+
describe('Accept-Encodings', () => {
13+
const fixtures = [
14+
{
15+
acceptEncoding: 'gzip',
16+
preferredEncoding: 'gzip'
17+
},
18+
{
19+
acceptEncoding: 'gzip, identity',
20+
preferredEncoding: 'gzip'
21+
},
22+
{
23+
acceptEncoding: 'br, gzip',
24+
preferredEncoding: 'br'
25+
},
26+
{
27+
acceptEncoding: 'identity',
28+
preferredEncoding: undefined
29+
}
30+
]
31+
32+
fixtures.forEach(({ acceptEncoding, preferredEncoding }) => {
33+
test(`When ${acceptEncoding} should return ${preferredEncoding}`, async () => {
34+
const app = new Koa()
35+
app.use(compress())
36+
app.use(async (ctx) => {
37+
ctx.body = await crypto.randomBytes(2048).toString('base64')
38+
})
39+
40+
server = app.listen()
41+
42+
const res = await request(server)
43+
.get('/')
44+
.set('Accept-Encoding', acceptEncoding)
45+
.expect(200)
46+
47+
assert.strictEqual(res.headers['content-encoding'], preferredEncoding)
48+
})
49+
})
50+
})

0 commit comments

Comments
 (0)