-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
156 lines (135 loc) · 4.03 KB
/
index.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
const request = require('then-request');
const urljoin = require('url-join');
const { cropLongData } = require('@kronoslive/codeceptjs-utils');
const { FormData } = require('then-request');
let mochawesome;
let utils;
/**
* HTTP codeceptjs helper
*/
class HTTP extends Helper {
/**
*
* @param {object} config
*/
constructor(config) {
super(config);
this._validateConfig(config);
}
/**
*
* @param {object} config
* @private
*/
_validateConfig(config) {
this.options = {};
this.currentResponse = '';
// Override defaults with config
Object.assign(this.options, config);
if (!this.options.endpoint) {
throw new Error(`
HTTP requires at endpoint to be set.
Check your codeceptjs config file to ensure this is set properly
{
"helpers": {
"HTTP": {
"endpoint": "YOUR_HTTP_ENDPOINT"
}
}
}
`);
}
}
/**
*
* @returns {boolean}
* @private
*/
_beforeSuite(test, mochawesomeHelper) {
mochawesome = mochawesomeHelper || this.helpers.Mochawesome;
utils = this.helpers.Utils;
return true;
}
/**
*
* @private
*/
_after() {
this.currentResponse = '';
}
/**
* Send HTTP request, response will be availible as return value and in this.currentResponse.
* @param {string} requestPath - endpoint path. Can be relative or absolute
* @param {string} method - (optional). Default GET
* @param {object} options - (optional). Headers, body, etc. see https://www.npmjs.com/package/then-request
* @param {string} domain
* @returns {*}
*
* ```js
* let response = yield I.sendRequest('/orders');
* ```
*/
async sendRequest(requestPath, method = 'GET', options = {}, domain) {
domain = domain || this.options.endpoint;
if (requestPath.indexOf('http') !== 0) {
requestPath = urljoin(domain, requestPath);
}
let form;
if (options.form) {
form = new FormData();
Object.keys(options.form).forEach((k) => {
form.append(k, options.form[k].toString());
});
}
mochawesome.addMochawesomeContext({
title: 'Send HTTP request',
value: {
method,
url: requestPath,
options,
},
});
const res = await request(method, requestPath, { ...options, form });
let body = res.body.length === 0 ? null : res.body.toString('utf8');
try {
body = JSON.parse(body);
} catch (e) {
// ignore then
}
const result = {
status: res.statusCode,
headers: res.headers,
body,
};
this.currentResponse = result;
mochawesome.addMochawesomeContext({
title: 'Get HTTP response',
value: cropLongData(result),
});
return result;
}
/**
* Validate that latest https response has specified JSON schema.
* @param {string} schema - path to JSON schema file. is relative to schemas folder
* @param {*} params - (optional) if schema file is js file, that export function, then you can specify here params in array,
* that will be applied to this function. Function should return JSON schema.
* @param {*} data - (optional) - specify data that should be validated (by default first response message for latest
* request)
* @returns {*}
*
* ```
* I.seeHttpResponseHasValidJsonSchema('authPassed.json');
* ```
*/
seeHttpResponseHasValidJsonSchema(schema, params, data) {
if (utils === undefined) {
throw new Error('To use JSON schema validation please add codeceptjs-utils-helper https://www.npmjs.com/package/@kronoslive/codeceptjs-utils-helper');
}
data = data || this.currentResponse.body;
return utils.seeDataHasValidJsonSchema(schema, params, data);
}
sendRequestUntil(requestPath, method = 'GET', options = {}, predicate, domain, timeout = 2000) {
return utils.waitUntil(() => this.sendRequest(requestPath, method, options, domain).then(predicate), timeout, `http request wait ${requestPath} with ${predicate}`, 250);
}
}
module.exports = HTTP;