This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: adds "validateStatusCode" unit
- Loading branch information
1 parent
69f9a93
commit f8a47c2
Showing
3 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const { assert } = require('chai'); | ||
const validateStatusCode = require('../../units/validateStatusCode'); | ||
|
||
describe('validateStatusCode', () => { | ||
describe('given matching status codes', () => { | ||
const res = validateStatusCode(200, 200); | ||
|
||
it('has proper validator', () => { | ||
assert.propertyVal(res, 'validator', 'TextDiff'); | ||
}); | ||
|
||
it('has proper expected type', () => { | ||
assert.propertyVal(res, 'expectedType', 'text/vnd.apiary.status-code'); | ||
}); | ||
|
||
it('has proper real type', () => { | ||
assert.propertyVal(res, 'realType', 'text/vnd.apiary.status-code'); | ||
}); | ||
|
||
it('has no errors', () => { | ||
assert.deepPropertyVal(res, 'results', []); | ||
}); | ||
}); | ||
|
||
describe('given unmatching status codes', () => { | ||
const res = validateStatusCode(200, 400); | ||
|
||
it('has proper validator', () => { | ||
assert.propertyVal(res, 'validator', 'TextDiff'); | ||
}); | ||
|
||
it('has proper expected type', () => { | ||
assert.propertyVal(res, 'expectedType', 'text/vnd.apiary.status-code'); | ||
}); | ||
|
||
it('has proper real type', () => { | ||
assert.propertyVal(res, 'realType', 'text/vnd.apiary.status-code'); | ||
}); | ||
|
||
it('has errors', () => { | ||
assert.deepPropertyVal(res, 'results', [ | ||
{ | ||
message: 'Real and expected data does not match.', | ||
severity: 'error' | ||
} | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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 {number} realStatusCode | ||
* @param {number} expectedStatusCode | ||
*/ | ||
function validateStatusCode(realStatusCode, expectedStatusCode) { | ||
const validator = new TextDiff( | ||
normalizeStatusCode(realStatusCode), | ||
normalizeStatusCode(expectedStatusCode) | ||
); | ||
const rawData = validator.validate(); | ||
|
||
return { | ||
validator: 'TextDiff', | ||
realType: APIARY_STATUS_CODE_TYPE, | ||
expectedType: APIARY_STATUS_CODE_TYPE, | ||
rawData, | ||
results: validator.evaluateOutputToResults() | ||
}; | ||
} | ||
|
||
module.exports = validateStatusCode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters