/
match_body.js
77 lines (64 loc) · 2.02 KB
/
match_body.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict'
const _ = require('lodash')
const querystring = require('querystring')
const common = require('./common')
module.exports = function matchBody(options, spec, body) {
if (spec instanceof RegExp) {
return spec.test(body)
}
if (Buffer.isBuffer(spec)) {
const encoding = common.isUtf8Representable(spec) ? 'utf8' : 'hex'
spec = spec.toString(encoding)
}
const contentType = (
(options.headers &&
(options.headers['Content-Type'] || options.headers['content-type'])) ||
''
).toString()
const isMultipart = contentType.includes('multipart')
const isUrlencoded = contentType.includes('application/x-www-form-urlencoded')
// try to transform body to json
let json
if (typeof spec === 'object' || spec instanceof Function) {
try {
json = JSON.parse(body)
} catch (err) {
// not a valid JSON string
}
if (json !== undefined) {
body = json
} else if (isUrlencoded) {
body = querystring.parse(body)
}
}
if (spec instanceof Function) {
return spec.call(options, body)
}
// strip line endings from both so that we get a match no matter what OS we are running on
// if Content-Type does not contains 'multipart'
if (!isMultipart && typeof body === 'string') {
body = body.replace(/\r?\n|\r/g, '')
}
if (!isMultipart && typeof spec === 'string') {
spec = spec.replace(/\r?\n|\r/g, '')
}
// Because the nature of URL encoding, all the values in the body have been cast to strings.
// dataEqual does strict checking so we we have to cast the non-regexp values in the spec too.
if (isUrlencoded) {
spec = mapValuesDeep(spec, val => (val instanceof RegExp ? val : `${val}`))
}
return common.dataEqual(spec, body)
}
/**
* Based on lodash issue discussion
* https://github.com/lodash/lodash/issues/1244
*/
function mapValuesDeep(obj, cb) {
if (Array.isArray(obj)) {
return obj.map(v => mapValuesDeep(v, cb))
}
if (_.isPlainObject(obj)) {
return _.mapValues(obj, v => mapValuesDeep(v, cb))
}
return cb(obj)
}