Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 98a9e6a

Browse files
refactor: adds status code normalization to normalization layer
1 parent a0e3229 commit 98a9e6a

File tree

5 files changed

+54
-24
lines changed

5 files changed

+54
-24
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { assert } = require('chai');
2+
const {
3+
normalizeStatusCode
4+
} = require('../../../../units/normalize/normalizeStatusCode');
5+
6+
describe('normalizeStatusCode', () => {
7+
describe('when given a string', () => {
8+
const normalized = normalizeStatusCode(' 400 ');
9+
10+
it('returns a string', () => {
11+
assert.isString(normalized);
12+
});
13+
14+
it('trims the value', () => {
15+
assert.equal(normalized, '400');
16+
});
17+
});
18+
19+
describe('when given falsy value', () => {
20+
const values = [null, undefined];
21+
22+
values.forEach((value) => {
23+
const normalized = normalizeStatusCode(value);
24+
25+
it(`returns empty string when given ${value}`, () => {
26+
assert.equal(normalized, '');
27+
});
28+
});
29+
});
30+
});

lib/api/test/unit/units/validateStatusCode.test.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,66 @@ const { validateStatusCode } = require('../../../units/validateStatusCode');
33

44
describe('validateStatusCode', () => {
55
describe('given matching status codes', () => {
6-
const res = validateStatusCode(
6+
const result = validateStatusCode(
77
{
8-
statusCode: 200
8+
statusCode: '200'
99
},
1010
{
11-
statusCode: 200
11+
statusCode: '200'
1212
}
1313
);
1414

1515
it('has "TextDiff" validator', () => {
16-
assert.propertyVal(res, 'validator', 'TextDiff');
16+
assert.propertyVal(result, 'validator', 'TextDiff');
1717
});
1818

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

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

2727
it('has no errors', () => {
28-
assert.deepPropertyVal(res, 'results', []);
28+
assert.deepPropertyVal(result, 'results', []);
2929
});
3030
});
3131

3232
describe('given non-matching status codes', () => {
33-
const res = validateStatusCode(
33+
const result = validateStatusCode(
3434
{
35-
statusCode: 200
35+
statusCode: '200'
3636
},
3737
{
38-
statusCode: 400
38+
statusCode: '400'
3939
}
4040
);
4141

4242
it('has "TextDiff" validator', () => {
43-
assert.propertyVal(res, 'validator', 'TextDiff');
43+
assert.propertyVal(result, 'validator', 'TextDiff');
4444
});
4545

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

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

5454
describe('produces error', () => {
5555
it('exactly one error', () => {
56-
assert.lengthOf(res.results, 1);
56+
assert.lengthOf(result.results, 1);
5757
});
5858

5959
it('has "error" severity', () => {
60-
assert.propertyVal(res.results[0], 'severity', 'error');
60+
assert.propertyVal(result.results[0], 'severity', 'error');
6161
});
6262

6363
it('has explanatory message', () => {
6464
assert.propertyVal(
65-
res.results[0],
65+
result.results[0],
6666
'message',
6767
'Real and expected data does not match.'
6868
);

lib/api/units/normalize/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const evolve = require('../../utils/evolve');
2+
const { normalizeStatusCode } = require('./normalizeStatusCode');
23
const { normalizeHeaders } = require('./normalizeHeaders');
34

45
const normalize = evolve({
6+
statusCode: normalizeStatusCode,
57
headers: normalizeHeaders
68
});
79

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function normalizeStatusCode(value) {
2+
return value == null ? '' : String(value).trim();
3+
}
4+
5+
module.exports = { normalizeStatusCode };

lib/api/units/validateStatusCode.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@ const { TextDiff } = require('../../validators/text-diff');
22

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

5-
function normalizeStatusCode(statusCode) {
6-
return String(statusCode).trim();
7-
}
8-
95
/**
106
* Validates given real and expected status codes.
117
* @param {Object} real
128
* @param {number} expected
139
*/
1410
function validateStatusCode(real, expected) {
15-
const validator = new TextDiff(
16-
normalizeStatusCode(real.statusCode),
17-
normalizeStatusCode(expected.statusCode)
18-
);
11+
const validator = new TextDiff(real.statusCode, expected.statusCode);
1912
const rawData = validator.validate();
2013

2114
return {

0 commit comments

Comments
 (0)