Skip to content

Commit

Permalink
Merge b5ba8d4 into c3c2a6b
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-koval committed Aug 23, 2018
2 parents c3c2a6b + b5ba8d4 commit 57bbc93
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 41 deletions.
19 changes: 17 additions & 2 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ const validateCustomVersion = options => propSatisfies(isValidVersion, 'version'
const getCustomName = options => prop('name', options);
const getCustomVersion = options => prop('version', options);

const getValidHeaderValue = (options, validator, getter, valueFromPackage) => {
if (validator(options)) {
return getter(options);
}
return valueFromPackage;
};

const setVersionHeaders = (options) => {
options = options || {};
return async (ctx, next) => {
Expand All @@ -31,8 +38,16 @@ const setVersionHeaders = (options) => {
const serviceVersion = getPackageVersion(packageFile);
assert(isValidVersion(serviceVersion), 'Version is not semver');

ctx.set('X-Service-Name', validateCustomName(options) ? getCustomName(options) : serviceName);
ctx.set('X-Service-Version', validateCustomVersion(options) ? getCustomVersion(options) : serviceVersion);
const name = getValidHeaderValue(options, validateCustomName, getCustomName, serviceName);
const version = getValidHeaderValue(
options,
validateCustomVersion,
getCustomVersion,
serviceVersion
);

ctx.set('X-Service-Name', name);
ctx.set('X-Service-Version', version);
};
};

Expand Down
63 changes: 44 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
},
"homepage": "https://github.com/oleg-koval/koa-verto#readme",
"devDependencies": {
"axios": "^0.18.0",
"chai": "^4.1.2",
"coveralls": "^3.0.2",
"eslint": "^5.4.0",
Expand All @@ -44,6 +43,7 @@
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"semantic-release": "^15.9.9",
"superagent": "^3.8.3",
"travis-deploy-once": "^5.0.2"
},
"dependencies": {
Expand Down
91 changes: 72 additions & 19 deletions test/koaIntegration.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const Koa = require('koa');
const axios = require('axios');
const setVersionHeaders = require('..');

const { name, version } = require('../package.json');

const verify = (headers) => {
expect(headers['x-service-name']).to.be.equal(name);
expect(headers['x-service-version']).to.be.equal(version);
const verify = (headers, options) => {
expect(headers['x-service-name']).to.be.equal(options.name);
expect(headers['x-service-version']).to.be.equal(options.version);
};

describe('Should work with Koa as middleware', () => {
describe('Should work with Koa as middleware with values from package.json', () => {
let app;
const url = 'http://localhost:3000/';

Expand All @@ -24,37 +23,91 @@ describe('Should work with Koa as middleware', () => {
});

it('has headers for GET request', async () => {
const response = await axios.get(url);
verify(response.headers);
const response = await request.get(url);
verify(response.headers, { name, version });
});

it('has headers for POST request', async () => {
const response = await axios.get(url);
verify(response.headers);
const response = await request.get(url);
verify(response.headers, { name, version });
});

it('has headers for PUT request', async () => {
const response = await axios.put(url);
verify(response.headers);
const response = await request.put(url);
verify(response.headers, { name, version });
});

it('has headers for PATCH request', async () => {
const response = await axios.patch(url);
verify(response.headers);
const response = await request.patch(url);
verify(response.headers, { name, version });
});

it('has headers for HEAD request', async () => {
const response = await axios.head(url);
verify(response.headers);
const response = await request.head(url);
verify(response.headers, { name, version });
});

it('has headers for OPTIONS request', async () => {
const response = await axios.options(url);
verify(response.headers);
const response = await request.options(url);
verify(response.headers, { name, version });
});

it('has headers for DELETE request', async () => {
const response = await axios.delete(url);
verify(response.headers);
const response = await request.delete(url);
verify(response.headers, { name, version });
});
});

describe('Should work with Koa as middleware with custom values', () => {
let app;
const url = 'http://localhost:3001/';
const options = {
name: 'app',
version: 'v0.1.1'
};

before((done) => {
app = new Koa();
app.use(setVersionHeaders(options));
app.use((ctx) => {
ctx.body = 'ok';
});
app.listen(3001);
done();
});

it('has headers for GET request', async () => {
const response = await request.get(url);
verify(response.headers, options);
});

it('has headers for POST request', async () => {
const response = await request.get(url);
verify(response.headers, options);
});

it('has headers for PUT request', async () => {
const response = await request.put(url);
verify(response.headers, options);
});

it('has headers for PATCH request', async () => {
const response = await request.patch(url);
verify(response.headers, options);
});

it('has headers for HEAD request', async () => {
const response = await request.head(url);
verify(response.headers, options);
});

it('has headers for OPTIONS request', async () => {
const response = await request.options(url);
verify(response.headers, options);
});

it('has headers for DELETE request', async () => {
const response = await request.delete(url);
verify(response.headers, options);
});
});
2 changes: 2 additions & 0 deletions test/support/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// setup global chai methods
const chai = require('chai');
const request = require('superagent');

chai.config.includeStack = true;
chai.config.showDiff = true;

global.chai = chai;
global.expect = chai.expect;
global.assert = chai.assert;
global.request = request;

0 comments on commit 57bbc93

Please sign in to comment.