Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/service request #24

Merged
merged 2 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. Dates are d

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

#### [v2.3.0](https://github.com/DEFRA/water-abstraction-helpers/compare/v2.2.0...v2.3.0)

> 22 March 2019

- Adds service request API from UI project [`2a6769e`](https://github.com/DEFRA/water-abstraction-helpers/commit/2a6769e8f499128e4b5cae9d37a2c6093e6dc439)

#### [v2.2.0](https://github.com/DEFRA/water-abstraction-helpers/compare/v2.1.0...v2.2.0)

> 1 March 2019
Expand Down
44 changes: 33 additions & 11 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@envage/water-abstraction-helpers",
"version": "2.2.0",
"version": "2.3.0",
"description": "Contains functions for processing water abstraction data which are shared across multiple projects",
"main": "./src/index.js",
"scripts": {
Expand Down Expand Up @@ -38,6 +38,7 @@
"cross-fetch": "^3.0.0",
"moment": "^2.23.0",
"request": "^2.88.0",
"request-promise-native": "^1.0.7",
"winston": "^2.4.4"
},
"publishConfig": {
Expand Down
6 changes: 6 additions & 0 deletions src/http/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const rp = require('request-promise-native').defaults({
proxy: null,
strictSSL: false
});

module.exports.request = rp;
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ exports.logger = require('./logger');
exports.nald = require('./nald');
exports.regions = require('./regions');
exports.returns = require('./returns');
exports.http = require('./http');
exports.serviceRequest = require('./service-request');
26 changes: 26 additions & 0 deletions src/service-request/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Abstraction over request-promise-native for making requests to
* downstream services that are always JSON content type and
* always pass a JWT auth header.
*/

const http = require('../http');
const { partial } = require('lodash');

const makeRequest = (method, url, additionalOptions = {}) => {
const options = Object.assign({
url,
method,
json: true,
headers: {
Authorization: process.env.JWT_TOKEN
}
}, additionalOptions);

return http.request(options);
};

exports.get = partial(makeRequest, 'GET');
exports.post = partial(makeRequest, 'POST');
exports.patch = partial(makeRequest, 'PATCH');
exports.delete = partial(makeRequest, 'DELETE');
62 changes: 62 additions & 0 deletions test/service-request/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { expect } = require('code');
const { experiment, test, beforeEach, afterEach } = exports.lab = require('lab').script();

const sinon = require('sinon');
const sandbox = sinon.createSandbox();

const http = require('../../src/http');
const serviceRequest = require('../../src/service-request');

const getExpectedOptions = (method, data) => {
return {
method,
url: data.url,
...data.options,
json: true,
headers: {
Authorization: data.token
}
};
};

experiment('serviceRequest', () => {
const data = {
url: 'url',
originalJwtToken: process.env.JWT_TOKEN,
options: {
foo: 'bar',
bar: 'foo'
},
token: 'token'
};

beforeEach(async () => {
process.env.JWT_TOKEN = data.token;
sandbox.stub(http, 'request');
});

afterEach(async () => {
process.env.JWT_TOKEN = data.originalJwtToken;
sandbox.restore();
});

test('get', async () => {
serviceRequest.get(data.url, data.options);
expect(http.request.lastCall.args[0]).to.equal(getExpectedOptions('GET', data));
});

test('post', async () => {
serviceRequest.post(data.url, data.options);
expect(http.request.lastCall.args[0]).to.equal(getExpectedOptions('POST', data));
});

test('patch', async () => {
serviceRequest.patch(data.url, data.options);
expect(http.request.lastCall.args[0]).to.equal(getExpectedOptions('PATCH', data));
});

test('delete', async () => {
serviceRequest.delete(data.url, data.options);
expect(http.request.lastCall.args[0]).to.equal(getExpectedOptions('DELETE', data));
});
});