2017-12-02 09:52:40 +01:00
|
|
|
import assert from 'assert';
|
|
|
|
import zlib from 'zlib';
|
|
|
|
import {readFile} from '../lib/test.utils';
|
2014-03-30 23:05:42 +02:00
|
|
|
|
2017-12-02 09:52:40 +01:00
|
|
|
export default function(server, express) {
|
2017-04-19 21:15:28 +02:00
|
|
|
|
2017-12-02 09:52:40 +01:00
|
|
|
describe('test gzip support', () => {
|
|
|
|
beforeAll(function() {
|
2014-11-12 12:14:37 +01:00
|
|
|
express.get('/testexp_gzip', function(req, res) {
|
2017-08-06 21:54:15 +02:00
|
|
|
const pkg = eval(
|
2017-12-02 09:52:40 +01:00
|
|
|
'(' + readFile('../fixtures/publish.json5')
|
2014-11-12 12:14:37 +01:00
|
|
|
.toString('utf8')
|
|
|
|
.replace(/__NAME__/g, 'testexp_gzip')
|
|
|
|
.replace(/__VERSION__/g, '0.0.1')
|
|
|
|
+ ')'
|
2017-04-19 21:15:28 +02:00
|
|
|
);
|
2014-03-30 23:05:42 +02:00
|
|
|
|
2014-11-12 12:14:37 +01:00
|
|
|
// overcoming compress threshold
|
2017-08-06 21:54:15 +02:00
|
|
|
pkg.versions['0.0.2'] = pkg.versions['0.0.1'];
|
|
|
|
pkg.versions['0.0.3'] = pkg.versions['0.0.1'];
|
|
|
|
pkg.versions['0.0.4'] = pkg.versions['0.0.1'];
|
|
|
|
pkg.versions['0.0.5'] = pkg.versions['0.0.1'];
|
|
|
|
pkg.versions['0.0.6'] = pkg.versions['0.0.1'];
|
|
|
|
pkg.versions['0.0.7'] = pkg.versions['0.0.1'];
|
|
|
|
pkg.versions['0.0.8'] = pkg.versions['0.0.1'];
|
|
|
|
pkg.versions['0.0.9'] = pkg.versions['0.0.1'];
|
2014-03-30 23:05:42 +02:00
|
|
|
|
2017-08-06 21:54:15 +02:00
|
|
|
zlib.gzip(JSON.stringify(pkg), function(err, buf) {
|
2017-04-19 21:15:28 +02:00
|
|
|
assert.equal(err, null);
|
|
|
|
assert.equal(req.headers['accept-encoding'], 'gzip');
|
|
|
|
res.header('content-encoding', 'gzip');
|
|
|
|
res.send(buf);
|
|
|
|
});
|
|
|
|
});
|
2014-03-30 23:05:42 +02:00
|
|
|
|
2014-11-12 12:14:37 +01:00
|
|
|
express.get('/testexp_baddata', function(req, res) {
|
2017-04-19 21:15:28 +02:00
|
|
|
assert.equal(req.headers['accept-encoding'], 'gzip');
|
|
|
|
res.header('content-encoding', 'gzip');
|
|
|
|
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
|
|
|
|
2017-12-02 09:52:40 +01:00
|
|
|
test('should not fail on bad gzip', () => {
|
2017-08-06 21:54:15 +02:00
|
|
|
return server.getPackage('testexp_baddata').status(404);
|
2017-04-19 21:15:28 +02:00
|
|
|
});
|
2014-03-30 23:05:42 +02:00
|
|
|
|
2017-12-02 09:52:40 +01:00
|
|
|
test('should understand gzipped data from uplink', () => {
|
2017-06-28 22:56:02 +02:00
|
|
|
return server.getPackage('testexp_gzip')
|
2015-04-11 19:11:04 +02:00
|
|
|
.status(200)
|
2017-04-19 21:15:28 +02:00
|
|
|
.response(function(res) {
|
|
|
|
assert.equal(res.headers['content-encoding'], undefined);
|
2015-04-11 19:11:04 +02:00
|
|
|
})
|
2017-04-19 21:15:28 +02:00
|
|
|
.then(function(body) {
|
|
|
|
assert.equal(body.name, 'testexp_gzip');
|
|
|
|
assert.equal(Object.keys(body.versions).length, 9);
|
|
|
|
});
|
|
|
|
});
|
2014-03-30 23:05:42 +02:00
|
|
|
|
2017-12-02 09:52:40 +01:00
|
|
|
test('should serve gzipped data', () => {
|
2015-04-11 19:11:04 +02:00
|
|
|
return server.request({
|
2014-11-12 12:14:37 +01:00
|
|
|
uri: '/testexp_gzip',
|
|
|
|
encoding: null,
|
|
|
|
headers: {
|
|
|
|
'Accept-encoding': 'gzip',
|
|
|
|
},
|
|
|
|
json: false,
|
2015-04-11 19:11:04 +02:00
|
|
|
}).status(200)
|
2017-04-19 21:15:28 +02:00
|
|
|
.response(function(res) {
|
|
|
|
assert.equal(res.headers['content-encoding'], 'gzip');
|
2014-11-12 12:14:37 +01:00
|
|
|
})
|
2017-04-19 21:15:28 +02:00
|
|
|
.then(function(body) {
|
2015-04-11 19:11:04 +02:00
|
|
|
assert.throws(function() {
|
2017-04-19 21:15:28 +02:00
|
|
|
JSON.parse(body.toString('utf8'));
|
|
|
|
});
|
2015-04-11 19:11:04 +02:00
|
|
|
|
2017-04-19 21:15:28 +02:00
|
|
|
return new Promise(function(resolve) {
|
2017-08-06 21:54:15 +02:00
|
|
|
zlib.gunzip(body, function(err, buf) {
|
2017-04-19 21:15:28 +02:00
|
|
|
assert.equal(err, null);
|
|
|
|
body = JSON.parse(buf);
|
|
|
|
assert.equal(body.name, 'testexp_gzip');
|
|
|
|
assert.equal(Object.keys(body.versions).length, 9);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-12-02 09:52:40 +01:00
|
|
|
}
|