Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion 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
@@ -1,6 +1,6 @@
{
"name": "yoti",
"version": "3.7.1",
"version": "3.7.2",
"description": "Yoti NodeJS SDK for back-end integration",
"author": "Yoti LTD <[email protected]> (https://www.yoti.com/developers)",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sonar.projectKey = yoti-web-sdk:node
sonar.projectName = node-sdk
sonar.projectVersion = 3.7.1
sonar.projectVersion = 3.7.2
sonar.exclusions=tests/**,examples/**,node_modules/**,coverage/**
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.verbose = true
5 changes: 3 additions & 2 deletions src/request/request.handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ module.exports.execute = yotiRequest => new Promise((resolve, reject) => {
request
.then((response) => {
try {
const parsedResponse = JSON.parse(response.text);
const receipt = parsedResponse.receipt || null;
const parsedResponse = response.text !== '' ? JSON.parse(response.text) : null;
const receipt = parsedResponse !== null ? parsedResponse.receipt : null;

return resolve(new YotiResponse(parsedResponse, response.statusCode, receipt));
} catch (err) {
return reject(err);
Expand Down
112 changes: 112 additions & 0 deletions tests/request/request.handler.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const nock = require('nock');
const fs = require('fs');
const yotiRequestHandler = require('../../src/request/request.handler');
const { RequestBuilder } = require('../../src/request/request.builder');
const { Payload } = require('../../src/request/payload');

const SOME_BASE_URL = 'https://someapi.yoti.com';
const SOME_ENDPOINT = '/some/endpoint';
const SOME_ENDPOINT_REG_EXP = new RegExp(`^${SOME_ENDPOINT}`);
const SOME_PEM_STRING = fs.readFileSync('./tests/sample-data/keys/node-sdk-test.pem', 'utf8');
const ALLOWED_METHODS = ['POST', 'PUT', 'PATCH', 'GET', 'DELETE'];
const SOME_JSON_DATA = { some: 'json' };
const SOME_JSON_RECEIPT_DATA = { receipt: 'some receipt' };

/**
* @param {string} method
* @param {string} uri
* @param {integer} responseCode
* @param {string} body
*/
const mockResponse = (method, uri, responseCode, body) => {
const scope = nock(SOME_BASE_URL);
const interceptor = scope[method.toLowerCase()](uri);
interceptor.reply(responseCode, body);
};

describe('yotiRequest', () => {
afterEach((done) => {
nock.cleanAll();
done();
});

ALLOWED_METHODS.forEach((ALLOWED_METHOD) => {
describe(`when empty response is returned for ${ALLOWED_METHOD} method`, () => {
beforeEach((done) => {
mockResponse(ALLOWED_METHOD, SOME_ENDPOINT_REG_EXP, 200, '');
done();
});

it('should return YotiResponse', (done) => {
const request = new RequestBuilder()
.withBaseUrl(SOME_BASE_URL)
.withEndpoint(SOME_ENDPOINT)
.withMethod(ALLOWED_METHOD)
.withPayload(new Payload(''))
.withPemString(SOME_PEM_STRING)
.build();

yotiRequestHandler
.execute(request)
.then((response) => {
expect(response.getParsedResponse()).toBeNull();
done();
})
.catch(done);
});
});
describe(`when JSON response is returned for ${ALLOWED_METHOD} method`, () => {
beforeEach((done) => {
mockResponse(ALLOWED_METHOD, SOME_ENDPOINT_REG_EXP, 200, JSON.stringify(SOME_JSON_DATA));
done();
});

it('should return YotiResponse', (done) => {
const request = new RequestBuilder()
.withBaseUrl(SOME_BASE_URL)
.withEndpoint(SOME_ENDPOINT)
.withMethod(ALLOWED_METHOD)
.withPayload(new Payload(''))
.withPemString(SOME_PEM_STRING)
.build();

yotiRequestHandler
.execute(request)
.then((response) => {
expect(response.getParsedResponse())
.toStrictEqual(SOME_JSON_DATA);
expect(response.getReceipt())
.toBeNull();
done();
})
.catch(done);
});
});
});
describe('when receipt is returned', () => {
beforeEach((done) => {
mockResponse('GET', SOME_ENDPOINT_REG_EXP, 200, JSON.stringify(SOME_JSON_RECEIPT_DATA));
done();
});

it('should return YotiResponse', (done) => {
const request = new RequestBuilder()
.withBaseUrl(SOME_BASE_URL)
.withEndpoint(SOME_ENDPOINT)
.withMethod('GET')
.withPemString(SOME_PEM_STRING)
.build();

yotiRequestHandler
.execute(request)
.then((response) => {
expect(response.getParsedResponse())
.toStrictEqual(SOME_JSON_RECEIPT_DATA);
expect(response.getReceipt())
.toStrictEqual(SOME_JSON_RECEIPT_DATA.receipt);
done();
})
.catch(done);
});
});
});