-
Notifications
You must be signed in to change notification settings - Fork 19
/
definitions.js
366 lines (313 loc) · 11.2 KB
/
definitions.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
'use strict'
const { inspect } = require('util')
const { expect } = require('chai')
const _ = require('lodash')
const Cast = require('../../core/cast')
const { assertObjectMatchSpec } = require('../../core/assertions')
const { STATUS_CODES } = require('http')
const STATUS_MESSAGES = _.values(STATUS_CODES).map(_.lowerCase)
/**
* Ensures there's a response available and returns it.
*
* @param {Object} client
*/
const mustGetResponse = client => {
const response = client.getResponse()
expect(response, 'No response available').to.not.be.empty
return response
}
module.exports = ({ baseUrl = '' } = {}) => ({ Given, When, Then }) => {
/**
* Setting http headers
*/
Given(/^(?:I )?set request headers$/, function(step) {
this.httpApiClient.setHeaders(Cast.object(this.state.populateObject(step.rowsHash())))
})
/**
* Setting http option followRedirect to false
*/
Given(/^(?:I )?do not follow redirect$/, function() {
this.httpApiClient.setFollowRedirect(false)
})
/**
* Setting http option followRedirect to true
*/
Given(/^(?:I )?follow redirect$/, function() {
this.httpApiClient.setFollowRedirect(true)
})
/**
* Assign http headers
* The difference from "set request headers" is that "set" set the whole headers object
* "assign" replace or set the given headers, keeping untouched the ones already set
*/
Given(/^(?:I )?assign request headers$/, function(step) {
const headers = Cast.object(this.state.populateObject(step.rowsHash()))
_.each(headers, (value, key) => this.httpApiClient.setHeader(key, value))
})
/**
* Setting a single http header
*/
Given(/^(?:I )?set ([a-zA-Z0-9-]+) request header to (.+)$/, function(key, value) {
this.httpApiClient.setHeader(key, Cast.value(this.state.populate(value)))
})
/**
* Clearing headers
*/
Given(/^(?:I )?clear request headers/, function() {
this.httpApiClient.clearHeaders()
})
/**
* Setting json payload
*/
Given(/^(?:I )?set request json body$/, function(step) {
this.httpApiClient.setJsonBody(Cast.object(this.state.populateObject(step.rowsHash())))
})
/**
* Setting json payload from fixture file
*/
Given(/^(?:I )?set request json body from (.+)$/, function(fixture) {
return this.fixtures.load(fixture).then(data => {
this.httpApiClient.setJsonBody(data)
})
})
/**
* Setting form data
*/
Given(/^(?:I )?set request form body$/, function(step) {
this.httpApiClient.setFormBody(Cast.object(this.state.populateObject(step.rowsHash())))
})
/**
* Setting form data from fixture file
*/
Given(/^(?:I )?set request form body from (.+)$/, function(fixture) {
return this.fixtures.load(fixture).then(data => {
this.httpApiClient.setFormBody(data)
})
})
/**
* Setting multipart data from fixture file
*/
Given(/^(?:I )?set request multipart body from (.+)$/, function(fixture) {
return this.fixtures.load(fixture).then(data => {
this.httpApiClient.setMultipartBody(data)
})
})
/**
* Clearing body
*/
Given(/^(?:I )?clear request body$/, function() {
this.httpApiClient.clearBody()
})
/**
* Setting query parameters
*/
Given(/^(?:I )?set request query$/, function(step) {
this.httpApiClient.setQuery(Cast.object(this.state.populateObject(step.rowsHash())))
})
Given(/^(?:I )?pick response json (.+) as (.+)$/, function(path, key) {
const response = this.httpApiClient.getResponse()
const body = response.body
this.state.set(key, _.get(body, path))
})
/**
* Enabling cookies
*/
Given(/^(?:I )?enable cookies$/, function() {
this.httpApiClient.enableCookies()
})
/**
* Disabling cookies
*/
Given(/^(?:I )?disable cookies$/, function() {
this.httpApiClient.disableCookies()
})
/**
* Setting a cookie from fixture file
*/
Given(/^(?:I )?set cookie from (.+)$/, function(fixture) {
return this.fixtures.load(fixture).then(cookie => {
this.httpApiClient.setCookie(cookie)
})
})
/**
* Clearing client request cookies
*/
Given(/^(?:I )?clear request cookies$/, function() {
this.httpApiClient.clearRequestCookies()
})
/**
* Resetting the client's state
*/
When(/^(?:I )?reset http client$/, function() {
this.httpApiClient.reset()
})
/**
* Performing a request
*/
When(/^(?:I )?(GET|POST|PUT|DELETE) (.+)$/, function(method, path) {
return this.httpApiClient.makeRequest(method, this.state.populate(path), baseUrl)
})
/**
* Dumping response body
*/
When(/^(?:I )?dump response body$/, function() {
const response = mustGetResponse(this.httpApiClient)
console.log(inspect(response.body, { colors: true, depth: null })) // eslint-disable-line no-console
})
/**
* Dumping response headers
*/
When(/^(?:I )?dump response headers$/, function() {
const response = mustGetResponse(this.httpApiClient)
console.log(response.headers) // eslint-disable-line no-console
})
/**
* Dumping response cookies
*/
When(/^(?:I )?dump response cookies$/, function() {
mustGetResponse(this.httpApiClient)
console.log(this.httpApiClient.getCookies()) // eslint-disable-line no-console
})
/**
* Checking response status code
*/
Then(/^response status code should be ([1-5][0-9][0-9])$/, function(statusCode) {
const response = mustGetResponse(this.httpApiClient)
expect(
response.statusCode,
`Expected status code to be: ${statusCode}, but found: ${response.statusCode}`
).to.equal(Number(statusCode))
})
/**
* Checking response status by message
*/
Then(/^response status should be (.+)$/, function(statusMessage) {
if (!STATUS_MESSAGES.includes(_.lowerCase(statusMessage))) {
throw new TypeError(`'${statusMessage}' is not a valid status message`)
}
const response = mustGetResponse(this.httpApiClient)
const statusCode = _.findKey(STATUS_CODES, msg => _.lowerCase(msg) === statusMessage)
const currentStatusMessage = STATUS_CODES[`${response.statusCode}`] || response.statusCode
expect(
response.statusCode,
`Expected status to be: '${statusMessage}', but found: '${_.lowerCase(
currentStatusMessage
)}'`
).to.equal(Number(statusCode))
})
/**
* Checking response cookie is present|absent
*/
Then(/^response should (not )?have an? (.+) cookie$/, function(flag, key) {
const cookie = this.httpApiClient.getCookie(key)
if (flag === undefined) {
expect(cookie, `No cookie found for key '${key}'`).to.not.be.null
} else {
expect(cookie, `A cookie exists for key '${key}'`).to.be.null
}
})
/**
* Checking response cookie is|isn't secure
*/
Then(/^response (.+) cookie should (not )?be secure$/, function(key, flag) {
const cookie = this.httpApiClient.getCookie(key)
expect(cookie, `No cookie found for key '${key}'`).to.not.be.null
if (flag === undefined) {
expect(cookie.secure, `Cookie '${key}' is not secure`).to.be.true
} else {
expect(cookie.secure, `Cookie '${key}' is secure`).to.be.false
}
})
/**
* Checking response cookie httpOnly
*/
Then(/^response (.+) cookie should (not )?be http only$/, function(key, flag) {
const cookie = this.httpApiClient.getCookie(key)
expect(cookie, `No cookie found for key '${key}'`).to.not.be.null
if (flag === undefined) {
expect(cookie.httpOnly, `Cookie '${key}' is not http only`).to.be.true
} else {
expect(cookie.httpOnly, `Cookie '${key}' is http only`).to.be.false
}
})
/**
* Checking response cookie domain
*/
Then(/^response (.+) cookie domain should (not )?be (.+)$/, function(key, flag, domain) {
const cookie = this.httpApiClient.getCookie(key)
expect(cookie, `No cookie found for key '${key}'`).to.not.be.null
if (flag === undefined) {
expect(
cookie.domain,
`Expected cookie '${key}' domain to be '${domain}', found '${cookie.domain}'`
).to.equal(domain)
} else {
expect(cookie.domain, `Cookie '${key}' domain is '${domain}'`).to.not.equal(domain)
}
})
/**
* This definition can be used for checking an object response.
* It check that the properties of this object match with the expected properties
* The columns header are | field | matcher | value |
* @see Assertions.assertObjectMatchSpec
*/
Then(/^(?:I )?json response should (fully )?match$/, function(fully, table) {
const response = mustGetResponse(this.httpApiClient)
const { body } = response
// We check the response has json content-type
expect(response.headers['content-type']).to.contain('application/json')
// First we populate spec values if it contains some placeholder
const spec = table.hashes().map(fieldSpec =>
_.assign({}, fieldSpec, {
value: this.state.populate(fieldSpec.value)
})
)
assertObjectMatchSpec(body, spec, !!fully)
})
/**
* This definition verify that an array for a given path has the expected length
*/
Then(/^(?:I )?should receive a collection of ([0-9]+) items?(?: for path )?(.+)?$/, function(
size,
path
) {
const response = mustGetResponse(this.httpApiClient)
const { body } = response
const array = path !== undefined ? _.get(body, path) : body
expect(array.length).to.be.equal(Number(size))
})
/**
* Verifies that response matches a fixture.
**/
Then(/^response should match fixture (.+)$/, function(fixtureId) {
const response = mustGetResponse(this.httpApiClient)
return this.fixtures.load(fixtureId).then(snapshot => {
expect(response.body).to.deep.equal(snapshot)
})
})
/**
* Checking response header.
*/
Then(/^response header (.+) should (not )?(equal|contain|match) (.+)$/, function(
key,
flag,
comparator,
expectedValue
) {
const response = mustGetResponse(this.httpApiClient)
const header = response.headers[key.toLowerCase()]
expect(header, `Header '${key}' does not exist`).to.not.be.undefined
let expectFn = expect(
header,
`Expected header '${key}' to ${
flag ? flag : ''
}${comparator} '${expectedValue}', but found '${header}' which does${
flag ? '' : ' not'
}`
).to
if (flag !== undefined) {
expectFn = expectFn.not
}
expectFn[comparator](comparator === 'match' ? new RegExp(expectedValue) : expectedValue)
})
}