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

Commit f631116

Browse files
feat: swaps the order of "real" and "expected" arguments (validate)
BREAKING CHANGE: Gavel's call signature of the "validate" function is now the following: validate(expected, real)
1 parent 01503a0 commit f631116

13 files changed

+116
-67
lines changed

lib/next/test/integration/validateMessage.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ describe('validateMessage', () => {
7474

7575
describe('with non-matching requests', () => {
7676
const result = validateMessage(
77+
{
78+
method: 'PUT',
79+
headers: '',
80+
body: '2'
81+
},
7782
{
7883
method: 'POST',
7984
headers: {
8085
'Content-Type': 'application/json'
8186
},
8287
body: '{ "foo": "bar" }'
83-
},
84-
{
85-
method: 'PUT',
86-
headers: '',
87-
body: '2'
8888
}
8989
);
9090

@@ -217,7 +217,7 @@ describe('validateMessage', () => {
217217
'Accept-Language': 'en-US'
218218
}
219219
};
220-
const result = validateMessage(realResponse, expectedResponse);
220+
const result = validateMessage(expectedResponse, realResponse);
221221

222222
it('returns validation result object', () => {
223223
assert.isObject(result);
@@ -280,14 +280,14 @@ describe('validateMessage', () => {
280280

281281
describe('with non-matching headers', () => {
282282
const result = validateMessage(
283-
{
284-
statusCode: 404
285-
},
286283
{
287284
statusCode: 404,
288285
headers: {
289286
'Content-Type': 'text/plain'
290287
}
288+
},
289+
{
290+
statusCode: 404
291291
}
292292
);
293293

@@ -347,15 +347,15 @@ describe('validateMessage', () => {
347347

348348
describe('always validates expected properties', () => {
349349
const result = validateMessage(
350-
{
351-
body: 'doe'
352-
},
353350
{
354351
statusCode: 200,
355352
headers: {
356353
'Content-Type': 'application/json'
357354
},
358355
body: '{ "foo": "bar" }'
356+
},
357+
{
358+
body: 'doe'
359359
}
360360
);
361361

lib/next/test/unit/units/validateBody.test.js

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ describe('validateBody', () => {
2121
describe('when given supported body type', () => {
2222
describe('in a combination without related validator', () => {
2323
const result = validateBody(
24-
{
25-
headers: { 'content-type': 'application/json' },
26-
body: '{ "foo": "bar" }'
27-
},
2824
{
2925
headers: { 'content-type': 'text/plain' },
3026
body: ''
27+
},
28+
{
29+
headers: { 'content-type': 'application/json' },
30+
body: '{ "foo": "bar" }'
3131
}
3232
);
3333

@@ -63,11 +63,11 @@ describe('validateBody', () => {
6363
describe('with matching body type', () => {
6464
const res = validateBody(
6565
{
66-
body: '{ "foo": "bar" }',
67-
headers: { 'content-type': 'application/json' }
66+
body: '{ "foo": "bar" }'
6867
},
6968
{
70-
body: '{ "foo": "bar" }'
69+
body: '{ "foo": "bar" }',
70+
headers: { 'content-type': 'application/json' }
7171
}
7272
);
7373

@@ -90,11 +90,13 @@ describe('validateBody', () => {
9090

9191
describe('with non-matching body type', () => {
9292
const res = validateBody(
93+
{
94+
body: '{ "foo": "bar" }'
95+
},
9396
{
9497
body: 'foo',
9598
headers: { 'content-type': 'application/json' }
96-
},
97-
{ body: '{ "foo": "bar" }' }
99+
}
98100
);
99101

100102
it('has no validator', () => {
@@ -123,14 +125,14 @@ describe('validateBody', () => {
123125
describe('application/hal+json', () => {
124126
describe('with matching body type', () => {
125127
const res = validateBody(
128+
{
129+
body: '{ "foo": "bar" }'
130+
},
126131
{
127132
body: '{ "foo": "bar" }',
128133
headers: {
129134
'content-type': 'application/hal+json'
130135
}
131-
},
132-
{
133-
body: '{ "foo": "bar" }'
134136
}
135137
);
136138

@@ -153,14 +155,14 @@ describe('validateBody', () => {
153155

154156
describe('with non-matching body type', () => {
155157
const res = validateBody(
158+
{
159+
body: 'text'
160+
},
156161
{
157162
body: 'text',
158163
headers: {
159164
'content-type': 'application/hal+json'
160165
}
161-
},
162-
{
163-
body: 'text'
164166
}
165167
);
166168

@@ -191,7 +193,14 @@ describe('validateBody', () => {
191193
describe('without explicit "Content-Type" header', () => {
192194
describe('text/plain', () => {
193195
describe('with matching bodies', () => {
194-
const res = validateBody({ body: 'foo' }, { body: 'foo' });
196+
const res = validateBody(
197+
{
198+
body: 'foo'
199+
},
200+
{
201+
body: 'foo'
202+
}
203+
);
195204

196205
it('has "TextDiff" validator', () => {
197206
assert.propertyVal(res, 'validator', 'TextDiff');
@@ -211,7 +220,14 @@ describe('validateBody', () => {
211220
});
212221

213222
describe('with non-matching bodies', () => {
214-
const res = validateBody({ body: 'foo ' }, { body: 'bar' });
223+
const res = validateBody(
224+
{
225+
body: 'bar'
226+
},
227+
{
228+
body: 'foo '
229+
}
230+
);
215231

216232
it('has "TextDiff" validator', () => {
217233
assert.propertyVal(res, 'validator', 'TextDiff');
@@ -245,8 +261,12 @@ describe('validateBody', () => {
245261
describe('application/json', () => {
246262
describe('with matching bodies', () => {
247263
const res = validateBody(
248-
{ body: '{ "foo": "bar" }' },
249-
{ body: '{ "foo": "bar" }' }
264+
{
265+
body: '{ "foo": "bar" }'
266+
},
267+
{
268+
body: '{ "foo": "bar" }'
269+
}
250270
);
251271

252272
it('has "JsonExample" validator', () => {
@@ -268,8 +288,12 @@ describe('validateBody', () => {
268288

269289
describe('with non-matching bodies', () => {
270290
const res = validateBody(
271-
{ body: '{ "foo": "bar" }' },
272-
{ body: '{ "bar": null }' }
291+
{
292+
body: '{ "bar": null }'
293+
},
294+
{
295+
body: '{ "foo": "bar" }'
296+
}
273297
);
274298

275299
it('has "JsonExample" validator', () => {
@@ -303,11 +327,13 @@ describe('validateBody', () => {
303327
describe('application/schema+json', () => {
304328
describe('with matching bodies', () => {
305329
const res = validateBody(
306-
{ body: '{ "foo": "bar" }' },
307330
{
308331
bodySchema: {
309332
required: ['foo']
310333
}
334+
},
335+
{
336+
body: '{ "foo": "bar" }'
311337
}
312338
);
313339

@@ -330,11 +356,13 @@ describe('validateBody', () => {
330356

331357
describe('with non-matching bodies', () => {
332358
const res = validateBody(
333-
{ body: '{ "oneTwoThree": "bar" }' },
334359
{
335360
bodySchema: {
336361
required: ['doe']
337362
}
363+
},
364+
{
365+
body: '{ "oneTwoThree": "bar" }'
338366
}
339367
);
340368

lib/next/test/unit/units/validateHeaders.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ describe('validateHeaders', () => {
4747
const res = validateHeaders(
4848
{
4949
headers: {
50+
'accept-language': 'en-US,us',
51+
'content-type': 'application/json',
5052
connection: 'keep-alive'
5153
}
5254
},
5355
{
5456
headers: {
55-
'accept-language': 'en-US,us',
56-
'content-type': 'application/json',
5757
connection: 'keep-alive'
5858
}
5959
}
@@ -113,10 +113,10 @@ describe('validateHeaders', () => {
113113
describe('given non-json headers', () => {
114114
const res = validateHeaders(
115115
{
116-
headers: 'foo'
116+
headers: 'bar'
117117
},
118118
{
119-
headers: 'bar'
119+
headers: 'foo'
120120
}
121121
);
122122

lib/next/test/unit/units/validateMethod.test.js

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

44
describe('validateMethod', () => {
55
describe('given matching methods', () => {
6-
const result = validateMethod({ method: 'GET' }, { method: 'GET' });
6+
const result = validateMethod(
7+
{
8+
method: 'GET'
9+
},
10+
{
11+
method: 'GET'
12+
}
13+
);
714

815
it('has "isValid" as "true"', () => {
916
assert.propertyVal(result, 'isValid', true);
@@ -27,7 +34,14 @@ describe('validateMethod', () => {
2734
});
2835

2936
describe('given non-matching methods', () => {
30-
const result = validateMethod({ method: 'GET' }, { method: 'POST' });
37+
const result = validateMethod(
38+
{
39+
method: 'POST'
40+
},
41+
{
42+
method: 'GET'
43+
}
44+
);
3145

3246
it('returns "isValid" as "false"', () => {
3347
assert.propertyVal(result, 'isValid', false);
@@ -61,7 +75,14 @@ describe('validateMethod', () => {
6175
});
6276

6377
describe('given expected, but no real method', () => {
64-
const result = validateMethod({ method: '' }, { method: 'PATCH' });
78+
const result = validateMethod(
79+
{
80+
method: 'PATCH'
81+
},
82+
{
83+
method: ''
84+
}
85+
);
6586

6687
it('returns "isValid" as "false"', () => {
6788
assert.propertyVal(result, 'isValid', false);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ describe('validateStatusCode', () => {
3232
describe('given non-matching status codes', () => {
3333
const result = validateStatusCode(
3434
{
35-
statusCode: '200'
35+
statusCode: '400'
3636
},
3737
{
38-
statusCode: '400'
38+
statusCode: '200'
3939
}
4040
);
4141

lib/next/units/validateBody.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ function getBodyValidator(realType, expectedType) {
154154

155155
/**
156156
* Validates given bodies of transaction elements.
157-
* @param {Object} real
158-
* @param {Object} expected
157+
* @param {Object<string, any>} expected
158+
* @param {Object<string, any>} real
159159
*/
160-
function validateBody(real, expected) {
160+
function validateBody(expected, real) {
161161
const errors = [];
162162
const bodyType = typeof real.body;
163163

lib/next/units/validateHeaders.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ function getHeadersType(headers) {
1111

1212
/**
1313
* Validates given real and expected transaction elements.
14-
* @param {Object} real
15-
* @param {Object} expected
14+
* @param {Object<string, any>} expected
15+
* @param {Object<string, any>} real
1616
*/
17-
function validateHeaders(real, expected) {
18-
const realType = getHeadersType(real.headers);
17+
function validateHeaders(expected, real) {
1918
const expectedType = getHeadersType(expected.headers);
19+
const realType = getHeadersType(real.headers);
2020
const errors = [];
2121

2222
const hasJsonHeaders =

lib/next/units/validateMethod.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const APIARY_METHOD_TYPE = 'text/vnd.apiary.method';
22

3-
function validateMethod(real, expected) {
4-
const { method: realMethod } = real;
3+
function validateMethod(expected, real) {
54
const { method: expectedMethod } = expected;
6-
const errors = [];
5+
const { method: realMethod } = real;
76
const isValid = realMethod === expectedMethod;
7+
const errors = [];
88

99
if (!isValid) {
1010
errors.push({

lib/next/units/validateStatusCode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const APIARY_STATUS_CODE_TYPE = 'text/vnd.apiary.status-code';
55
* @param {Object} real
66
* @param {number} expected
77
*/
8-
function validateStatusCode(real, expected) {
9-
const errors = [];
8+
function validateStatusCode(expected, real) {
109
const isValid = real.statusCode === expected.statusCode;
10+
const errors = [];
1111

1212
if (!isValid) {
1313
errors.push({

0 commit comments

Comments
 (0)