-
Notifications
You must be signed in to change notification settings - Fork 1
/
requestBuilder.js
52 lines (45 loc) · 1.34 KB
/
requestBuilder.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
class RequestBuilder {
constructor(options) {
this.baseUrl = 'https://webhook.site/';
this.headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
};
// The test shouldn't fail if WebHook.site returns a 502
this.failOnStatusCode = false;
}
buildOptions(method, path) {
return {
method,
url: `${this.baseUrl}${path}`,
headers: this.headers,
};
}
request(method, path, parameters) {
const options = this.buildOptions(method, path);
if (parameters.hasOwnProperty('password') && !parameters.hasOwnProperty('apikey')) {
throw new Error('You must provide an apikey if a password is provided');
}
if (parameters.hasOwnProperty('apikey')) {
options.headers['Api-Key'] = parameters.apikey;
}
if (parameters.hasOwnProperty('password')) {
options.url = `${options.url}?password=${parameters.password}`;
}
//options.body = body || undefined;
return cy.request(options);
}
get(path, parameters) {
return this.request('GET', path, parameters);
}
post(path, parameters) {
return this.request('POST', path, parameters);
}
put(path, parameters) {
return this.request('PUT', path, parameters);
}
del(path, parameters) {
return this.request('DELETE', path, parameters);
}
}
module.exports = RequestBuilder;