1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/test/functional/package/gzip.js

91 lines
3.1 KiB
JavaScript
Raw Normal View History

import zlib from 'zlib';
import {readFile} from '../lib/test.utils';
2018-06-22 07:49:10 +02:00
import {HEADER_TYPE, HEADERS, HTTP_STATUS} from "../../../src/lib/constants";
2014-03-30 23:05:42 +02:00
export default function(server, express) {
2018-06-22 07:49:10 +02:00
const PKG_NAME = 'testexp_gzip';
2017-04-19 21:15:28 +02:00
describe('test gzip support', () => {
beforeAll(function() {
2018-06-22 07:49:10 +02:00
express.get(`/${PKG_NAME}`, function(req, res) {
const version = '0.0.1';
const pkg = eval(
'(' + readFile('../fixtures/publish.json5')
.toString('utf8')
2018-06-22 07:49:10 +02:00
.replace(/__NAME__/g, PKG_NAME)
.replace(/__VERSION__/g, version)
+ ')'
2017-04-19 21:15:28 +02:00
);
2014-03-30 23:05:42 +02:00
// overcoming compress threshold
2018-06-22 07:49:10 +02:00
pkg.versions['0.0.2'] = pkg.versions[version];
pkg.versions['0.0.3'] = pkg.versions[version];
pkg.versions['0.0.4'] = pkg.versions[version];
pkg.versions['0.0.5'] = pkg.versions[version];
pkg.versions['0.0.6'] = pkg.versions[version];
pkg.versions['0.0.7'] = pkg.versions[version];
pkg.versions['0.0.8'] = pkg.versions[version];
pkg.versions['0.0.9'] = pkg.versions[version];
2014-03-30 23:05:42 +02:00
zlib.gzip(JSON.stringify(pkg), function(err, buf) {
2018-06-22 07:49:10 +02: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 21:15:28 +02:00
res.send(buf);
});
});
2014-03-30 23:05:42 +02:00
express.get('/testexp_baddata', function(req, res) {
2018-06-22 07:49:10 +02:00
expect(req.headers[HEADER_TYPE.ACCEPT_ENCODING]).toBe(HEADERS.GZIP);
res.header(HEADER_TYPE.CONTENT_ENCODING, HEADERS.GZIP);
2017-04-19 21:15:28 +02:00
res.send(new Buffer([1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1]));
});
});
2014-03-30 23:05:42 +02:00
test('should not fail on bad gzip', () => {
2018-06-22 07:49:10 +02:00
return server.getPackage('testexp_baddata').status(HTTP_STATUS.NOT_FOUND);
2017-04-19 21:15:28 +02:00
});
2014-03-30 23:05:42 +02:00
test('should understand gzipped data from uplink', () => {
2018-06-22 07:49:10 +02:00
return server.getPackage(PKG_NAME)
.status(HTTP_STATUS.OK)
2017-04-19 21:15:28 +02:00
.response(function(res) {
2018-06-22 07:49:10 +02:00
expect(res.headers[HEADER_TYPE.CONTENT_ENCODING]).toBeUndefined();
})
2017-04-19 21:15:28 +02:00
.then(function(body) {
2018-06-22 07:49:10 +02:00
expect(body.name).toBe(PKG_NAME);
expect(Object.keys(body.versions)).toHaveLength(9);
2017-04-19 21:15:28 +02:00
});
});
2014-03-30 23:05:42 +02:00
test('should serve gzipped data', () => {
return server.request({
uri: '/testexp_gzip',
encoding: null,
headers: {
2018-06-22 07:49:10 +02:00
[HEADER_TYPE.ACCEPT_ENCODING]: HEADERS.GZIP,
},
json: false,
2018-06-22 07:49:10 +02:00
}).status(HTTP_STATUS.OK)
2017-04-19 21:15:28 +02:00
.response(function(res) {
2018-06-22 07:49:10 +02:00
expect(res.headers[HEADER_TYPE.CONTENT_ENCODING]).toBe(HEADERS.GZIP);
})
2017-04-19 21:15:28 +02:00
.then(function(body) {
2018-06-22 07:49:10 +02:00
expect(function() {
2017-04-19 21:15:28 +02:00
JSON.parse(body.toString('utf8'));
2018-06-22 07:49:10 +02:00
}).toThrow(/Unexpected/);
2017-04-19 21:15:28 +02:00
return new Promise(function(resolve) {
zlib.gunzip(body, function(err, buf) {
2018-06-22 07:49:10 +02:00
expect(err).toBeNull();
2017-04-19 21:15:28 +02:00
body = JSON.parse(buf);
2018-06-22 07:49:10 +02:00
expect(body.name).toBe(PKG_NAME);
expect(Object.keys(body.versions)).toHaveLength(9)
2017-04-19 21:15:28 +02:00
resolve();
});
});
});
});
});
}