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

Commit e8a6406

Browse files
refactor: parameterize http message origin of "getBodyType"
1 parent e9dc2bf commit e8a6406

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('validateBody', () => {
7272
it('has explanatory message', () => {
7373
assert.match(
7474
res.results[0].message,
75-
/^Real body 'Content-Type' header is 'application\/json' but body is not a parseable JSON:/
75+
/^Can't validate: real body 'Content-Type' header is 'application\/json' but body is not a parseable JSON:/
7676
);
7777
});
7878
});
@@ -142,7 +142,7 @@ describe('validateBody', () => {
142142
it('has explanatory message', () => {
143143
assert.match(
144144
res.results[0].message,
145-
/^Real body 'Content-Type' header is 'application\/hal\+json' but body is not a parseable JSON:/
145+
/^Can't validate: real body 'Content-Type' header is 'application\/hal\+json' but body is not a parseable JSON:/
146146
);
147147
});
148148
});

lib/api/test/unit/units/validateBody/getBodySchemaType.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('getBodySchemaType', () => {
3939
it('returns parsing error', () => {
4040
assert.match(
4141
res[0],
42-
/^Can't validate. Expected body JSON Schema is not a parseable JSON:/
42+
/^Can't validate: expected body JSON Schema is not a parseable JSON:/
4343
);
4444
});
4545

lib/api/test/unit/units/validateBody/getBodyType.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('getBodyType', () => {
1010
jsonTypes.forEach((jsonType) => {
1111
describe(`when given "${jsonType}" content type`, () => {
1212
describe('and parsable json body', () => {
13-
const res = getBodyType('{ "foo": "bar" }', jsonType);
13+
const res = getBodyType('{ "foo": "bar" }', jsonType, 'real');
1414

1515
it('returns no errors', () => {
1616
assert.isNull(res[0]);
@@ -22,14 +22,14 @@ describe('getBodyType', () => {
2222
});
2323

2424
describe('and non-parsable json body', () => {
25-
const res = getBodyType('abc', jsonType);
25+
const res = getBodyType('abc', jsonType, 'real');
2626

2727
it('returns parsing error', () => {
2828
assert.match(
2929
res[0],
3030
new RegExp(
3131
/* eslint-disable no-useless-escape */
32-
`^Real body 'Content-Type' header is \'${jsonType.replace(
32+
`^Can't validate: real body 'Content-Type' header is \'${jsonType.replace(
3333
/(\/|\+)/g,
3434
'\\$1'
3535
)}\' but body is not a parseable JSON:`
@@ -50,7 +50,7 @@ describe('getBodyType', () => {
5050
nonJsonTypes.forEach((nonJsonType) => {
5151
describe(`when given "${nonJsonType}" content type`, () => {
5252
describe('and parsable json body', () => {
53-
const res = getBodyType('{ "foo": "bar" }', nonJsonType);
53+
const res = getBodyType('{ "foo": "bar" }', nonJsonType, 'real');
5454

5555
it('returns no errors', () => {
5656
assert.isNull(res[0]);
@@ -62,7 +62,7 @@ describe('getBodyType', () => {
6262
});
6363

6464
describe('and a non-json body', () => {
65-
const res = getBodyType('abc', nonJsonType);
65+
const res = getBodyType('abc', nonJsonType, 'real');
6666

6767
it('returns no errors', () => {
6868
assert.isNull(res[0]);

lib/api/units/validateBody.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ function isJsonContentType(contentType) {
6262
* on the given body and normalized headers.
6363
* @param {string} body
6464
* @param {Object} headers
65+
* @param {'real'|'expected'} bodyType
6566
* @returns {[error, bodyType]}
6667
*/
67-
function getBodyType(body, contentType) {
68+
function getBodyType(body, contentType, httpMessageOrigin) {
6869
const hasJsonContentType = isJsonContentType(contentType);
6970

7071
try {
@@ -77,16 +78,9 @@ function getBodyType(body, contentType) {
7778
} catch (parsingError) {
7879
const fallbackMediaType = mediaTyper.parse('text/plain');
7980
const error = hasJsonContentType
80-
? /**
81-
* @TODO The same message for real/expected
82-
* body assertion. Real/expected must be reflected
83-
* in the error message.
84-
*/
85-
`\
86-
Real body 'Content-Type' header is '${contentType}' \
81+
? `Can't validate: ${httpMessageOrigin} body 'Content-Type' header is '${contentType}' \
8782
but body is not a parseable JSON:
88-
${parsingError.message}\
89-
`
83+
${parsingError.message}`
9084
: null;
9185

9286
return [error, fallbackMediaType];
@@ -110,7 +104,7 @@ function getBodySchemaType(bodySchema) {
110104
jph.parse(bodySchema);
111105
return [null, jsonSchemaType];
112106
} catch (exception) {
113-
const error = `Can't validate. Expected body JSON Schema is not a parseable JSON:\n${
107+
const error = `Can't validate: expected body JSON Schema is not a parseable JSON:\n${
114108
exception.message
115109
}`;
116110

@@ -165,13 +159,15 @@ function validateBody(real, expected) {
165159

166160
const [realTypeError, realType] = getBodyType(
167161
real.body,
168-
real.headers && real.headers['content-type']
162+
real.headers && real.headers['content-type'],
163+
'real'
169164
);
170165
const [expectedTypeError, expectedType] = expected.bodySchema
171166
? getBodySchemaType(expected.bodySchema)
172167
: getBodyType(
173168
expected.body,
174-
expected.headers && expected.headers['content-type']
169+
expected.headers && expected.headers['content-type'],
170+
'expected'
175171
);
176172

177173
if (realTypeError) {

0 commit comments

Comments
 (0)