-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixtures.js
215 lines (179 loc) · 4.86 KB
/
fixtures.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const { hash, sortObjectKeysRecurs, isObjectEmpty } = require('./utils')
const querystring = require('querystring')
// TODO: remove Joi with a lightweight composable validation library
const Joi = require('@hapi/joi')
const fixtureStorage = new Map()
exports.validateFixture = function validateFixture (
unsafeFixture,
configuration
) {
const schemaProperty = Joi.alternatives([
Joi.array().items(
Joi.custom((value, helpers) => {
const path = helpers.state.path
const property = path[path.length - 2]
if (!configuration[property][value]) {
throw new Error(`${value} not found in configuration`)
}
return value
}),
Joi.object()
),
Joi.object()
])
// TODO: move schema outside the func
const schema = Joi.object({
request: Joi.object({
body: Joi.any(),
path: Joi.string().required(),
method: Joi.string()
.regex(/^(head|delete|put|post|get|options|patch)$/i)
.required(),
headers: schemaProperty,
cookies: schemaProperty,
query: schemaProperty,
options: Joi.object({
headers: Joi.object({
strict: Joi.bool()
}),
cookies: Joi.object({
strict: Joi.bool()
}),
query: Joi.object({
strict: Joi.bool()
}),
body: Joi.object({
strict: Joi.bool()
})
})
}).required(),
response: Joi.object({
status: Joi.number()
.integer()
.min(200)
.max(600),
body: Joi.any(),
filepath: Joi.string(),
headers: schemaProperty,
cookies: schemaProperty,
options: Joi.object({
delay: Joi.number()
.integer()
.min(0)
})
})
.or('body', 'filepath')
.required(),
options: Joi.object({
lifetime: Joi.number()
.integer()
.min(0)
})
}).required()
const error = schema.validate(unsafeFixture).error
if (error) {
return error.message
}
return ''
}
function normalizeArrayMatcher (property, propertyValue, configuration) {
const result = {}
// Merge with configuration
for (const propertyItem of propertyValue) {
if (typeof propertyItem === 'string') {
Object.assign(result, configuration[property][propertyItem])
} else {
Object.assign(result, propertyItem)
}
}
return result
}
function normalizePath (request) {
// extract query from path is needed and move it in query property
const indexQueryString = request.path.indexOf('?')
if (indexQueryString >= 0) {
const path = request.path.substring(0, indexQueryString)
const query = querystring.parse(
request.path.substring(indexQueryString + 1)
)
request.path = path
if (request.query) {
Object.assign(request.query, query)
} else {
request.query = query
}
}
}
function normalizeFixture (fixture, configuration) {
const request = sortObjectKeysRecurs(fixture.request)
const response = sortObjectKeysRecurs(fixture.response)
const options = fixture.options
for (const property in request) {
if (property === 'body') {
continue
}
if (property === 'method') {
request.method = request.method.toUpperCase()
continue
}
if (property === 'path') {
normalizePath(request)
continue
}
const propertyValue = request[property]
if (Array.isArray(propertyValue)) {
request[property] = normalizeArrayMatcher(
property,
propertyValue,
configuration
)
}
if (typeof propertyValue === 'object' && isObjectEmpty(propertyValue)) {
delete request[property]
}
}
for (const property in response) {
if (property === 'body' || property === 'filepath') {
continue
}
const propertyValue = response[property]
if (Array.isArray(propertyValue)) {
response[property] = normalizeArrayMatcher(
property,
propertyValue,
configuration
)
}
}
if (options) {
return { request, response, options }
}
return { request, response }
}
function createFixtureId (fixture) {
return hash(JSON.stringify(fixture.request))
}
exports.registerFixture = function registerFixture (newFixture, configuration) {
const fixture = normalizeFixture(newFixture, configuration)
const fixtureId = createFixtureId(fixture)
if (fixtureStorage.has(fixtureId)) {
return {
conflictError: `Route ${fixture.request.method} ${fixture.request.path} is already registered`,
fixtureId
}
}
fixtureStorage.set(fixtureId, fixture)
return {
conflictError: '',
fixtureId
}
}
exports.getFixtureIterator = function getFixtureIterator () {
return fixtureStorage
}
exports.removeFixture = function removeFixture (fixtureId) {
return fixtureStorage.delete(fixtureId)
}
exports.removeFixtures = function removeFixtures () {
fixtureStorage.clear()
}