2021-03-14 02:42:46 -05:00
|
|
|
/* eslint-disable jest/no-standalone-expect */
|
2017-12-02 03:52:40 -05:00
|
|
|
import zlib from 'zlib';
|
2021-03-14 02:42:46 -05:00
|
|
|
import { readFile } from '../lib/test.utils';
|
|
|
|
import { HEADER_TYPE, HEADERS, HTTP_STATUS, CHARACTER_ENCODING } from '../../../src/lib/constants';
|
2014-03-30 16:05:42 -05:00
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
export default function (server, express) {
|
2018-06-22 00:49:10 -05:00
|
|
|
const PKG_NAME = 'testexp_gzip';
|
2018-06-24 03:11:52 -05:00
|
|
|
const PKG_VERSION = '0.0.1';
|
|
|
|
const PKG_BAD_DATA = 'testexp_baddata';
|
|
|
|
const VERSION_TOTAL = 4;
|
|
|
|
|
2017-12-02 03:52:40 -05:00
|
|
|
describe('test gzip support', () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
beforeAll(function () {
|
|
|
|
express.get(`/${PKG_NAME}`, function (req, res) {
|
|
|
|
const pkg = JSON.parse(
|
|
|
|
readFile('../fixtures/publish.json5')
|
|
|
|
.toString(CHARACTER_ENCODING.UTF8)
|
|
|
|
.replace(/__NAME__/g, PKG_NAME)
|
|
|
|
.replace(/__VERSION__/g, PKG_VERSION)
|
|
|
|
);
|
2014-03-30 16:05:42 -05:00
|
|
|
|
2014-11-12 06:14:37 -05:00
|
|
|
// overcoming compress threshold
|
2018-06-24 03:11:52 -05:00
|
|
|
for (let i = 1; i <= VERSION_TOTAL; i++) {
|
|
|
|
pkg.versions[`0.0.${i}`] = pkg.versions[PKG_VERSION];
|
|
|
|
}
|
2014-03-30 16:05:42 -05:00
|
|
|
|
2018-06-24 03:11:52 -05:00
|
|
|
zlib.gzip(JSON.stringify(pkg), (err, buf) => {
|
2018-06-22 00:49:10 -05:00
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(req.headers[HEADER_TYPE.ACCEPT_ENCODING]).toBe(HEADERS.GZIP);
|
|
|
|
res.header(HEADER_TYPE.CONTENT_ENCODING, HEADERS.GZIP);
|
2017-04-19 14:15:28 -05:00
|
|
|
res.send(buf);
|
|
|
|
});
|
|
|
|
});
|
2014-03-30 16:05:42 -05:00
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
express.get(`/${PKG_BAD_DATA}`, function (req, res) {
|
2018-06-24 03:11:52 -05:00
|
|
|
expect(req).toBeDefined();
|
|
|
|
expect(res).toBeDefined();
|
2018-06-22 00:49:10 -05:00
|
|
|
expect(req.headers[HEADER_TYPE.ACCEPT_ENCODING]).toBe(HEADERS.GZIP);
|
|
|
|
res.header(HEADER_TYPE.CONTENT_ENCODING, HEADERS.GZIP);
|
2020-11-03 01:55:08 -05:00
|
|
|
res.send(Buffer.from([1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1]));
|
2017-04-19 14:15:28 -05:00
|
|
|
});
|
|
|
|
});
|
2014-03-30 16:05:42 -05:00
|
|
|
|
2017-12-02 03:52:40 -05:00
|
|
|
test('should not fail on bad gzip', () => {
|
2018-06-24 03:11:52 -05:00
|
|
|
return server.getPackage(PKG_BAD_DATA).status(HTTP_STATUS.NOT_FOUND);
|
2017-04-19 14:15:28 -05:00
|
|
|
});
|
2014-03-30 16:05:42 -05:00
|
|
|
|
2018-06-24 03:11:52 -05:00
|
|
|
test('should understand non gzipped data from uplink', () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return server
|
|
|
|
.getPackage(PKG_NAME)
|
2019-07-16 01:40:01 -05:00
|
|
|
.status(HTTP_STATUS.OK)
|
|
|
|
.response((res) => {
|
|
|
|
expect(res.headers[HEADER_TYPE.CONTENT_ENCODING]).toBeUndefined();
|
2021-03-14 02:42:46 -05:00
|
|
|
})
|
|
|
|
.then((body) => {
|
2019-07-16 01:40:01 -05:00
|
|
|
expect(body.name).toBe(PKG_NAME);
|
|
|
|
expect(Object.keys(body.versions)).toHaveLength(VERSION_TOTAL);
|
|
|
|
});
|
2017-04-19 14:15:28 -05:00
|
|
|
});
|
2014-03-30 16:05:42 -05:00
|
|
|
|
2017-12-02 03:52:40 -05:00
|
|
|
test('should serve gzipped data', () => {
|
2021-03-14 02:42:46 -05:00
|
|
|
return server
|
|
|
|
.request({
|
|
|
|
uri: `/${PKG_NAME}`,
|
|
|
|
encoding: null,
|
|
|
|
headers: {
|
|
|
|
[HEADER_TYPE.ACCEPT_ENCODING]: HEADERS.GZIP
|
|
|
|
},
|
|
|
|
json: false
|
|
|
|
})
|
|
|
|
.status(HTTP_STATUS.OK)
|
|
|
|
.response(function (res) {
|
2018-06-22 00:49:10 -05:00
|
|
|
expect(res.headers[HEADER_TYPE.CONTENT_ENCODING]).toBe(HEADERS.GZIP);
|
2014-11-12 06:14:37 -05:00
|
|
|
})
|
2021-03-14 02:42:46 -05:00
|
|
|
.then(async function (body) {
|
2018-06-24 03:11:52 -05:00
|
|
|
// should fails since is zipped
|
2021-03-14 02:42:46 -05:00
|
|
|
expect(function () {
|
2018-09-21 10:34:12 -05:00
|
|
|
JSON.parse(body.toString(CHARACTER_ENCODING.UTF8));
|
2018-06-22 00:49:10 -05:00
|
|
|
}).toThrow(/Unexpected/);
|
2015-04-11 12:11:04 -05:00
|
|
|
|
2018-06-24 03:11:52 -05:00
|
|
|
// we unzip content and check content
|
2021-03-14 02:42:46 -05:00
|
|
|
await new Promise(function (resolve) {
|
|
|
|
zlib.gunzip(body, function (err, buffer) {
|
2018-06-22 00:49:10 -05:00
|
|
|
expect(err).toBeNull();
|
2018-06-24 03:11:52 -05:00
|
|
|
expect(buffer).not.toBeNull();
|
2019-07-16 01:40:01 -05:00
|
|
|
const unzipedBody = JSON.parse(buffer.toString());
|
2018-06-24 03:11:52 -05:00
|
|
|
|
|
|
|
expect(unzipedBody.name).toBe(PKG_NAME);
|
|
|
|
expect(Object.keys(unzipedBody.versions)).toHaveLength(VERSION_TOTAL);
|
2017-04-19 14:15:28 -05:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-12-02 03:52:40 -05:00
|
|
|
}
|