Skip to content

Commit 34ec3ed

Browse files
committed
added customTypes unit tests and improved doc for that option
1 parent 65c8491 commit 34ec3ed

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ fastify.register(
6666
{ threshold: 2048 }
6767
)
6868
```
69-
### Compression Header Customization
69+
### customTypes
7070
[mime-db](https://github.com/jshttp/mime-db) is used to determine if a `Content-Type` should be compressed. You can compress additional content types via regular expression.
7171
```javascript
7272
fastify.register(
7373
require('fastify-compress'),
74-
{ customTypes: /^x-protobuf$/ }
74+
{ customTypes: /x-protobuf$/ }
7575
)
7676
```
7777
### Brotli

test.js

+68
Original file line numberDiff line numberDiff line change
@@ -801,3 +801,71 @@ test('onSend hook should remove content-length', t => {
801801
t.strictEqual(payload.toString('utf-8'), file)
802802
})
803803
})
804+
805+
test('Should compress if customTypes is set and matches content type', t => {
806+
t.plan(3)
807+
const fastify = Fastify()
808+
fastify.register(compressPlugin, { customTypes: /x-user-header$/ })
809+
810+
fastify.get('/', (req, reply) => {
811+
reply.type('application/x-user-header').send(createReadStream('./package.json'))
812+
})
813+
814+
fastify.inject({
815+
url: '/',
816+
method: 'GET',
817+
headers: {
818+
'accept-encoding': 'gzip'
819+
}
820+
}, (err, res) => {
821+
t.error(err)
822+
t.strictEqual(res.headers['content-encoding'], 'gzip')
823+
const file = readFileSync('./package.json', 'utf8')
824+
const payload = zlib.gunzipSync(res.rawPayload)
825+
t.strictEqual(payload.toString('utf-8'), file)
826+
})
827+
})
828+
829+
test('Should not compress if customTypes is set and does not match content type or mime-db', t => {
830+
t.plan(3)
831+
const fastify = Fastify()
832+
fastify.register(compressPlugin, { customTypes: /x-user-header$/ })
833+
834+
fastify.get('/', (req, reply) => {
835+
reply.type('application/x-other-type').send(createReadStream('./package.json'))
836+
})
837+
838+
fastify.inject({
839+
url: '/',
840+
method: 'GET',
841+
headers: {
842+
'accept-encoding': 'gzip'
843+
}
844+
}, (err, res) => {
845+
t.error(err)
846+
t.notOk(res.headers['content-encoding'])
847+
t.strictEqual(res.statusCode, 200)
848+
})
849+
})
850+
851+
test('Should not apply customTypes if value passed is not RegExp', t => {
852+
t.plan(3)
853+
const fastify = Fastify()
854+
fastify.register(compressPlugin, { customTypes: 'x-user-header' })
855+
856+
fastify.get('/', (req, reply) => {
857+
reply.type('application/x-user-header').send(createReadStream('./package.json'))
858+
})
859+
860+
fastify.inject({
861+
url: '/',
862+
method: 'GET',
863+
headers: {
864+
'accept-encoding': 'gzip'
865+
}
866+
}, (err, res) => {
867+
t.error(err)
868+
t.notOk(res.headers['content-encoding'])
869+
t.strictEqual(res.statusCode, 200)
870+
})
871+
})

0 commit comments

Comments
 (0)