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

Commit f08d0a6

Browse files
feat: validates "uri" field of HTTP message
1 parent 4478590 commit f08d0a6

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const { assert } = require('chai');
2+
const { validateURI } = require('../../../units/validateURI');
3+
4+
describe('validateURI', () => {
5+
describe('given matching URI', () => {
6+
const result = validateURI(
7+
{
8+
uri: 'https://apiary.io/dashboard'
9+
},
10+
{
11+
uri: 'https://apiary.io/dashboard'
12+
}
13+
);
14+
15+
it('marks field as valid', () => {
16+
assert.propertyVal(result, 'isValid', true);
17+
});
18+
19+
it('has "null" validator', () => {
20+
assert.propertyVal(result, 'validator', null);
21+
});
22+
23+
it('has "text/vnd.apiary.uri" real type', () => {
24+
assert.propertyVal(result, 'realType', 'text/vnd.apiary.uri');
25+
});
26+
27+
it('has "text/vnd.apiary.uri" expected type', () => {
28+
assert.propertyVal(result, 'expectedType', 'text/vnd.apiary.uri');
29+
});
30+
31+
it('has no errors', () => {
32+
assert.lengthOf(result.errors, 0);
33+
});
34+
});
35+
36+
describe('given non-matching URI', () => {
37+
const result = validateURI(
38+
{
39+
uri: 'https://apiary.io/dashboard'
40+
},
41+
{
42+
uri: 'http://domain.com/dashboard'
43+
}
44+
);
45+
46+
it('marks field as invalid', () => {
47+
assert.propertyVal(result, 'isValid', false);
48+
});
49+
50+
it('has "null" validator', () => {
51+
assert.propertyVal(result, 'validator', null);
52+
});
53+
54+
it('has "text/vnd.apiary.uri" real type', () => {
55+
assert.propertyVal(result, 'realType', 'text/vnd.apiary.uri');
56+
});
57+
58+
it('has "text/vnd.apiary.uri" expected type', () => {
59+
assert.propertyVal(result, 'expectedType', 'text/vnd.apiary.uri');
60+
});
61+
62+
describe('produces an error', () => {
63+
it('exactly one error', () => {
64+
assert.lengthOf(result.errors, 1);
65+
});
66+
67+
it('has explanatory message', () => {
68+
assert.propertyVal(
69+
result.errors[0],
70+
'message',
71+
'Expected "uri" field to equal "https://apiary.io/dashboard", but got "http://domain.com/dashboard".'
72+
);
73+
});
74+
});
75+
});
76+
});

lib/next/units/coerce/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const otherwise = require('../../utils/otherwise');
33

44
const coercionMap = {
55
method: otherwise(''),
6+
uri: otherwise(''),
67
headers: otherwise({})
78
};
89

lib/next/units/validateStatusCode.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ function validateStatusCode(expected, real) {
2222
validator: 'TextDiff',
2323
realType: APIARY_STATUS_CODE_TYPE,
2424
expectedType: APIARY_STATUS_CODE_TYPE,
25-
rawData: '',
2625
errors
2726
};
2827
}

lib/next/units/validateURI.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const APIARY_URI_TYPE = 'text/vnd.apiary.uri';
2+
3+
const validateURI = (expected, real) => {
4+
const { uri: expectedURI } = expected;
5+
const { uri: realURI } = real;
6+
const errors = [];
7+
const isValid = expectedURI === realURI;
8+
9+
if (!isValid) {
10+
errors.push({
11+
message: `Expected "uri" field to equal "${expectedURI}", but got "${realURI}".`
12+
});
13+
}
14+
15+
return {
16+
isValid,
17+
validator: null,
18+
expectedType: APIARY_URI_TYPE,
19+
realType: APIARY_URI_TYPE,
20+
errors
21+
};
22+
};
23+
24+
module.exports = { validateURI };

lib/next/validate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { coerce, coerceWeak } = require('./units/coerce');
33
const { normalize } = require('./units/normalize');
44
const { isValid } = require('./units/isValid');
55
const { validateMethod } = require('./units/validateMethod');
6+
const { validateURI } = require('./units/validateURI');
67
const { validateStatusCode } = require('./units/validateStatusCode');
78
const { validateHeaders } = require('./units/validateHeaders');
89
const { validateBody } = require('./units/validateBody');
@@ -27,6 +28,10 @@ function validate(expectedMessage, realMessage) {
2728
result.fields.method = validateMethod(expected, real);
2829
}
2930

31+
if (expected.uri) {
32+
result.fields.uri = validateURI(expected, real);
33+
}
34+
3035
if (expected.statusCode) {
3136
result.fields.statusCode = validateStatusCode(expected, real);
3237
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = function() {
2+
this.Given(/^you expect HTTP message URI "([^"]*)"$/, (uri, callback) => {
3+
this.expected.uri = uri;
4+
return callback();
5+
});
6+
7+
return this.When(/^real HTTP message URI is "([^"]*)"$/, (uri, callback) => {
8+
this.real.uri = uri;
9+
return callback();
10+
});
11+
};

0 commit comments

Comments
 (0)