diff --git a/src/lib/constants.js b/src/lib/constants.js index 0b46d3fa7..c3407c00f 100644 --- a/src/lib/constants.js +++ b/src/lib/constants.js @@ -4,6 +4,12 @@ export const HEADERS = { JSON: 'application/json', JSON_CHARSET: 'application/json; charset=utf-8', OCTET_STREAM: 'application/octet-stream', + GZIP: 'gzip', +}; + +export const HEADER_TYPE = { + CONTENT_ENCODING: 'content-encoding', + ACCEPT_ENCODING: 'accept-encoding', }; export const ERROR_CODE = { @@ -26,3 +32,7 @@ export const HTTP_STATUS = { NOT_FOUND: 404, CONFLICT: 409, }; + +export const PORT_SERVER_1 = '55551'; +export const PORT_SERVER_2 = '55552'; +export const PORT_SERVER_3 = '55551'; diff --git a/test/functional/package/gzip.js b/test/functional/package/gzip.js index acc41a1bf..d647a9dd0 100644 --- a/test/functional/package/gzip.js +++ b/test/functional/package/gzip.js @@ -1,58 +1,60 @@ -import assert from 'assert'; import zlib from 'zlib'; import {readFile} from '../lib/test.utils'; +import {HEADER_TYPE, HEADERS, HTTP_STATUS} from "../../../src/lib/constants"; export default function(server, express) { + const PKG_NAME = 'testexp_gzip'; describe('test gzip support', () => { beforeAll(function() { - express.get('/testexp_gzip', function(req, res) { + express.get(`/${PKG_NAME}`, function(req, res) { + const version = '0.0.1'; const pkg = eval( '(' + readFile('../fixtures/publish.json5') .toString('utf8') - .replace(/__NAME__/g, 'testexp_gzip') - .replace(/__VERSION__/g, '0.0.1') + .replace(/__NAME__/g, PKG_NAME) + .replace(/__VERSION__/g, version) + ')' ); // overcoming compress threshold - 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']; + 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]; zlib.gzip(JSON.stringify(pkg), function(err, buf) { - assert.equal(err, null); - assert.equal(req.headers['accept-encoding'], 'gzip'); - res.header('content-encoding', 'gzip'); + expect(err).toBeNull(); + expect(req.headers[HEADER_TYPE.ACCEPT_ENCODING]).toBe(HEADERS.GZIP); + res.header(HEADER_TYPE.CONTENT_ENCODING, HEADERS.GZIP); res.send(buf); }); }); express.get('/testexp_baddata', function(req, res) { - assert.equal(req.headers['accept-encoding'], 'gzip'); - res.header('content-encoding', 'gzip'); + expect(req.headers[HEADER_TYPE.ACCEPT_ENCODING]).toBe(HEADERS.GZIP); + res.header(HEADER_TYPE.CONTENT_ENCODING, HEADERS.GZIP); res.send(new Buffer([1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1])); }); }); test('should not fail on bad gzip', () => { - return server.getPackage('testexp_baddata').status(404); + return server.getPackage('testexp_baddata').status(HTTP_STATUS.NOT_FOUND); }); test('should understand gzipped data from uplink', () => { - return server.getPackage('testexp_gzip') - .status(200) + return server.getPackage(PKG_NAME) + .status(HTTP_STATUS.OK) .response(function(res) { - assert.equal(res.headers['content-encoding'], undefined); + expect(res.headers[HEADER_TYPE.CONTENT_ENCODING]).toBeUndefined(); }) .then(function(body) { - assert.equal(body.name, 'testexp_gzip'); - assert.equal(Object.keys(body.versions).length, 9); + expect(body.name).toBe(PKG_NAME); + expect(Object.keys(body.versions)).toHaveLength(9); }); }); @@ -61,24 +63,24 @@ export default function(server, express) { uri: '/testexp_gzip', encoding: null, headers: { - 'Accept-encoding': 'gzip', + [HEADER_TYPE.ACCEPT_ENCODING]: HEADERS.GZIP, }, json: false, - }).status(200) + }).status(HTTP_STATUS.OK) .response(function(res) { - assert.equal(res.headers['content-encoding'], 'gzip'); + expect(res.headers[HEADER_TYPE.CONTENT_ENCODING]).toBe(HEADERS.GZIP); }) .then(function(body) { - assert.throws(function() { + expect(function() { JSON.parse(body.toString('utf8')); - }); + }).toThrow(/Unexpected/); return new Promise(function(resolve) { zlib.gunzip(body, function(err, buf) { - assert.equal(err, null); + expect(err).toBeNull(); body = JSON.parse(buf); - assert.equal(body.name, 'testexp_gzip'); - assert.equal(Object.keys(body.versions).length, 9); + expect(body.name).toBe(PKG_NAME); + expect(Object.keys(body.versions)).toHaveLength(9) resolve(); }); }); diff --git a/test/functional/package/scoped.js b/test/functional/package/scoped.js index d97668162..472e501b7 100644 --- a/test/functional/package/scoped.js +++ b/test/functional/package/scoped.js @@ -1,9 +1,11 @@ - -import assert from 'assert'; +import {HEADERS, HTTP_STATUS, PORT_SERVER_1, PORT_SERVER_2} from '../../../src/lib/constants'; import {generateSha} from '../lib/test.utils'; -import {HEADERS} from '../../../src/lib/constants'; +import {DIST_TAGS} from "../../../src/lib/utils"; export default function(server, server2) { + const SCOPE = '@test/scoped'; + const PKG_VERSION = '1.0.0'; + const PKG_NAME = 'scoped'; describe('test-scoped', () => { beforeAll(function() { @@ -14,19 +16,18 @@ export default function(server, server2) { }, method: 'PUT', json: require('./scoped.json'), - }).status(201); + }).status(HTTP_STATUS.CREATED); }); test('should publish scope package', () => {}); describe('should get scoped packages tarball', () => { const uploadScopedTarBall = (server) => { - return server.getTarball('@test/scoped', 'scoped-1.0.0.tgz') - .status(200) + return server.getTarball(SCOPE, `${PKG_NAME}-${PKG_VERSION}.tgz`) + .status(HTTP_STATUS.OK) .then(function(body) { // not real sha due to utf8 conversion - assert.strictEqual(generateSha(body), - '6e67b14e2c0e450b942e2bc8086b49e90f594790'); + expect(generateSha(body)).toEqual('6e67b14e2c0e450b942e2bc8086b49e90f594790'); }); }; @@ -41,22 +42,22 @@ export default function(server, server2) { }); describe('should retrieve scoped packages', () => { - const testScopePackage = (server, port) => server.getPackage('@test/scoped') - .status(200) + const testScopePackage = (server, port) => server.getPackage(SCOPE) + .status(HTTP_STATUS.OK) .then(function(body) { - assert.equal(body.name, '@test/scoped'); - assert.equal(body.versions['1.0.0'].name, '@test/scoped'); - assert.equal(body.versions['1.0.0'].dist.tarball, - `http://localhost:${port}/@test%2fscoped/-/scoped-1.0.0.tgz`); - assert.deepEqual(body['dist-tags'], {latest: '1.0.0'}); + expect(body.name).toBe(SCOPE); + expect(body.versions[PKG_VERSION].name).toBe(SCOPE); + expect(body.versions[PKG_VERSION].dist.tarball).toBe( + `http://localhost:${port}/@test%2fscoped/-/${PKG_NAME}-${PKG_VERSION}.tgz`); + expect(body[DIST_TAGS]).toEqual({latest: PKG_VERSION}); }); test('scoped package on server1', () => { - return testScopePackage(server, '55551'); + return testScopePackage(server, PORT_SERVER_1); }); test('scoped package on server2', () => { - return testScopePackage(server2, '55552'); + return testScopePackage(server2, PORT_SERVER_2); }); }); @@ -64,11 +65,11 @@ export default function(server, server2) { test('should work nginx workaround', () => { return server2.request({ uri: '/@test/scoped/1.0.0' - }).status(200) + }).status(HTTP_STATUS.OK) .then(function(body) { - assert.equal(body.name, '@test/scoped'); - assert.equal(body.dist.tarball, - 'http://localhost:55552/@test%2fscoped/-/scoped-1.0.0.tgz'); + expect(body.name).toEqual(SCOPE); + expect(body.dist.tarball).toEqual( + `http://localhost:${PORT_SERVER_2}/@test%2fscoped/-/${PKG_NAME}-${PKG_VERSION}.tgz`); }); }); });