Skip to content

Commit

Permalink
refactor: adds status code normalization to normalization layer
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed May 20, 2019
1 parent a0e3229 commit 98a9e6a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
30 changes: 30 additions & 0 deletions lib/api/test/unit/units/normalize/normalizeStatusCode.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { assert } = require('chai');
const {
normalizeStatusCode
} = require('../../../../units/normalize/normalizeStatusCode');

describe('normalizeStatusCode', () => {
describe('when given a string', () => {
const normalized = normalizeStatusCode(' 400 ');

it('returns a string', () => {
assert.isString(normalized);
});

it('trims the value', () => {
assert.equal(normalized, '400');
});
});

describe('when given falsy value', () => {
const values = [null, undefined];

values.forEach((value) => {
const normalized = normalizeStatusCode(value);

it(`returns empty string when given ${value}`, () => {
assert.equal(normalized, '');
});
});
});
});
32 changes: 16 additions & 16 deletions lib/api/test/unit/units/validateStatusCode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,66 @@ const { validateStatusCode } = require('../../../units/validateStatusCode');

describe('validateStatusCode', () => {
describe('given matching status codes', () => {
const res = validateStatusCode(
const result = validateStatusCode(
{
statusCode: 200
statusCode: '200'
},
{
statusCode: 200
statusCode: '200'
}
);

it('has "TextDiff" validator', () => {
assert.propertyVal(res, 'validator', 'TextDiff');
assert.propertyVal(result, 'validator', 'TextDiff');
});

it('has "text/vnd.apiary.status-code" expected type', () => {
assert.propertyVal(res, 'expectedType', 'text/vnd.apiary.status-code');
assert.propertyVal(result, 'expectedType', 'text/vnd.apiary.status-code');
});

it('has "text/vnd.apiary.status-code" real type', () => {
assert.propertyVal(res, 'realType', 'text/vnd.apiary.status-code');
assert.propertyVal(result, 'realType', 'text/vnd.apiary.status-code');
});

it('has no errors', () => {
assert.deepPropertyVal(res, 'results', []);
assert.deepPropertyVal(result, 'results', []);
});
});

describe('given non-matching status codes', () => {
const res = validateStatusCode(
const result = validateStatusCode(
{
statusCode: 200
statusCode: '200'
},
{
statusCode: 400
statusCode: '400'
}
);

it('has "TextDiff" validator', () => {
assert.propertyVal(res, 'validator', 'TextDiff');
assert.propertyVal(result, 'validator', 'TextDiff');
});

it('has "text/vnd.apiary.status-code" expected type', () => {
assert.propertyVal(res, 'expectedType', 'text/vnd.apiary.status-code');
assert.propertyVal(result, 'expectedType', 'text/vnd.apiary.status-code');
});

it('has "text/vnd.apiary.status-code" real type', () => {
assert.propertyVal(res, 'realType', 'text/vnd.apiary.status-code');
assert.propertyVal(result, 'realType', 'text/vnd.apiary.status-code');
});

describe('produces error', () => {
it('exactly one error', () => {
assert.lengthOf(res.results, 1);
assert.lengthOf(result.results, 1);
});

it('has "error" severity', () => {
assert.propertyVal(res.results[0], 'severity', 'error');
assert.propertyVal(result.results[0], 'severity', 'error');
});

it('has explanatory message', () => {
assert.propertyVal(
res.results[0],
result.results[0],
'message',
'Real and expected data does not match.'
);
Expand Down
2 changes: 2 additions & 0 deletions lib/api/units/normalize/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const evolve = require('../../utils/evolve');
const { normalizeStatusCode } = require('./normalizeStatusCode');
const { normalizeHeaders } = require('./normalizeHeaders');

const normalize = evolve({
statusCode: normalizeStatusCode,
headers: normalizeHeaders
});

Expand Down
5 changes: 5 additions & 0 deletions lib/api/units/normalize/normalizeStatusCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function normalizeStatusCode(value) {
return value == null ? '' : String(value).trim();
}

module.exports = { normalizeStatusCode };
9 changes: 1 addition & 8 deletions lib/api/units/validateStatusCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@ const { TextDiff } = require('../../validators/text-diff');

const APIARY_STATUS_CODE_TYPE = 'text/vnd.apiary.status-code';

function normalizeStatusCode(statusCode) {
return String(statusCode).trim();
}

/**
* Validates given real and expected status codes.
* @param {Object} real
* @param {number} expected
*/
function validateStatusCode(real, expected) {
const validator = new TextDiff(
normalizeStatusCode(real.statusCode),
normalizeStatusCode(expected.statusCode)
);
const validator = new TextDiff(real.statusCode, expected.statusCode);
const rawData = validator.validate();

return {
Expand Down

0 comments on commit 98a9e6a

Please sign in to comment.