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

99 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-04-19 21:15:28 +02:00
'use strict';
2014-03-30 23:05:42 +02:00
2017-04-19 21:15:28 +02:00
require('./lib/startup');
let assert = require('assert');
2014-03-30 23:05:42 +02:00
function readfile(x) {
2017-04-19 21:15:28 +02:00
return require('fs').readFileSync(__dirname + '/' + x);
2014-03-30 23:05:42 +02:00
}
module.exports = function() {
2017-04-19 21:15:28 +02:00
let server = process.server;
let express = process.express;
2014-03-30 23:05:42 +02:00
describe('testexp_gzip', function() {
before(function() {
express.get('/testexp_gzip', function(req, res) {
2017-04-19 21:15:28 +02:00
let x = eval(
'(' + readfile('fixtures/publish.json5')
.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
// overcoming compress threshold
2017-04-19 21:15:28 +02:00
x.versions['0.0.2'] = x.versions['0.0.1'];
x.versions['0.0.3'] = x.versions['0.0.1'];
x.versions['0.0.4'] = x.versions['0.0.1'];
x.versions['0.0.5'] = x.versions['0.0.1'];
x.versions['0.0.6'] = x.versions['0.0.1'];
x.versions['0.0.7'] = x.versions['0.0.1'];
x.versions['0.0.8'] = x.versions['0.0.1'];
x.versions['0.0.9'] = x.versions['0.0.1'];
2014-03-30 23:05:42 +02:00
require('zlib').gzip(JSON.stringify(x), 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
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-04-19 21:15:28 +02:00
it('should not fail on bad gzip', function() {
return server.getPackage('testexp_baddata')
2017-04-19 21:15:28 +02:00
.status(404);
});
2014-03-30 23:05:42 +02:00
2017-04-19 21:15:28 +02:00
it('should understand gzipped data from uplink', function() {
return server.getPackage('testexp_gzip')
.status(200)
2017-04-19 21:15:28 +02:00
.response(function(res) {
assert.equal(res.headers['content-encoding'], undefined);
})
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-04-19 21:15:28 +02:00
it('should serve gzipped data', function() {
return server.request({
uri: '/testexp_gzip',
encoding: null,
headers: {
'Accept-encoding': 'gzip',
},
json: false,
}).status(200)
2017-04-19 21:15:28 +02:00
.response(function(res) {
assert.equal(res.headers['content-encoding'], 'gzip');
})
2017-04-19 21:15:28 +02:00
.then(function(body) {
assert.throws(function() {
2017-04-19 21:15:28 +02:00
JSON.parse(body.toString('utf8'));
});
2017-04-19 21:15:28 +02:00
return new Promise(function(resolve) {
require('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();
});
});
});
});
});
};
2014-03-30 23:05:42 +02:00