-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-base.js
More file actions
330 lines (290 loc) · 12.6 KB
/
api-base.js
File metadata and controls
330 lines (290 loc) · 12.6 KB
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
// Importando o Puppeteer
/**
* @typedef {import('puppeteer-core').Page} Page
*/
import { Base } from './base.js';
import { readFileSync } from 'fs';
const jsonPath = new URL('./package.json', import.meta.url);
const packageJson = JSON.parse(readFileSync(jsonPath, 'utf-8'));
export class ApiBase extends Base {
constructor(page) {
super(page);
this.page = page;
this.page.setDefaultTimeout(this.defaultTimeout);
}
/** @type {Page} */
page;
defaultTimeout = 10000;
customHeaders = {};
async request(api) {
const startTime = Date.now();
let duration = 0;
const description = `Requisição ${api.method} para ${api.url}`;
let success = null;
let screenshot = null;
let error;
let extractedValues;
try {
await this.page.setRequestInterception(true);
this.requestData(api);
const response = await this.page.goto(api.url);
if (response) {
if (response.status() !== api.expectStatusCode) {
throw new Error(
`Falha na requisição ${api.method} para ${response.url()}. Status ${response.status()} mas esperado ${api.expectStatusCode}. Response ${await response.text()}`
);
}
console.log('Resposta da requisição:');
console.log(await this.getLog(response));
if (api.properties) {
const responseBody = await response.json();
if (api.properties) {
api.properties = api.properties.map(({ key, value, disabled }) => {
if (key === '$' && typeof value === 'string') {
return { key, value: [value], disabled };
}
return { key, value, disabled };
});
}
this.validateProperties(responseBody, api.properties, api.validateFieldNotExists);
if (api.extractList && api.extractList.length > 0) {
extractedValues = this.extractFields(responseBody, api.extractList);
console.log('Campos extraídos:', extractedValues);
}
}
success = `Sucesso na requisição ${api.method}`;
console.log(success);
} else {
throw new Error(`Não foi possível obter resposta da requisição ${api.method}`);
}
} catch (e) {
error = `Erro durante a execução da requisição ${api.method}: ${e}`;
console.log(error);
} finally {
const endTime = Date.now();
duration = (endTime - startTime) / 1000;
await this.page.setRequestInterception(false);
screenshot = await this.getScreenshot();
}
return { duration, error, success, description, screenshot, extractedValues };
}
resolvePath(obj, path) {
const segments = path
.replace(/\[(\d+)\]/g, '.$1')
.replace(/^\$/, '')
.replace(/^\./, '')
.split('.');
return segments.reduce((acc, segment) => {
if (acc && typeof acc === 'object' && segment in acc) {
return acc[segment];
}
return undefined;
}, obj);
}
validateProperties(responseData, properties, validateFieldNotExists) {
if (!properties || !properties.length) return;
const validatedKeys = [];
for (const { key, value: expectedValue, disabled } of properties) {
if (disabled) continue;
console.log(`Verificando propriedade: ${key}, valor esperado:`, expectedValue);
let actualValue;
try {
if (key === '$') {
// Se root for array, usa direto
if (Array.isArray(responseData)) {
actualValue = responseData;
} else if (Array.isArray(responseData.data)) {
// Caso comum: pegar a propriedade "data" que é array
actualValue = responseData.data;
} else if (Array.isArray(responseData.json)) {
// Alternativa: "json" pode ter o array
actualValue = responseData.json;
} else {
throw new Error(`Validação com "$" exige que o root ou 'data' ou 'json' seja um array.`);
}
} else if (key.startsWith('$')) {
// Exemplo $.data.something
const path = key.slice(2); // remove "$."
actualValue = this.resolvePath(responseData, path);
} else if (key.startsWith('.')) {
const path = key.slice(1);
actualValue = responseData?.[path];
} else {
actualValue = key.split('.').reduce((acc, part) => {
if (part.includes('[')) {
const [arr, index] = part.split(/\[|\]/).filter(Boolean);
return acc?.[arr]?.[parseInt(index)];
}
return acc?.[part];
}, responseData);
}
} catch (e) {
throw new Error(`Erro ao acessar a propriedade '${key}': ${e.message}`);
}
if (actualValue === undefined) {
throw new Error(`A propriedade '${key}' não está presente na resposta da API.`);
}
const actualStr = JSON.stringify(actualValue);
const expectedStr = JSON.stringify(expectedValue);
if (actualStr !== expectedStr) {
throw new Error(`Valor da propriedade '${key}' é '${actualStr}', mas esperado '${expectedStr}'.`);
}
}
if (validateFieldNotExists) {
const flattenKeys = (obj, prefix = '') =>
Object.keys(obj).flatMap(key => {
const path = prefix ? `${prefix}.${key}` : key;
if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
return flattenKeys(obj[key], path);
}
return path;
});
const allKeys = flattenKeys(responseData);
const normalizedValidatedKeys = validatedKeys
.filter(k => k !== '$')
.map(k => k.replace(/^\$?\./, ''));
const extraKeys = allKeys.filter(k => !normalizedValidatedKeys.includes(k));
if (extraKeys.length) {
throw new Error(`A resposta da API contém propriedades não esperadas: ${extraKeys.join(', ')}`);
}
}
console.log('Propriedades validadas com sucesso');
}
async requestData(api) {
const headers = {
'User-Agent': `TestTask/${packageJson.version}`,
...this.customHeaders,
};
if (api.contentType === 'multipart/form-data') {
headers['Content-Type'] = `${api.contentType}; boundary=${this.getBoundary()}`;
} else {
headers['Content-Type'] = api.contentType;
}
this.page.once('request', interceptedRequest => {
const requestData = {
url: interceptedRequest.url(),
method: api.method,
headers: {
...interceptedRequest.headers(),
...headers,
},
body: api.bodyFields ? this.getData(api) : null,
};
console.log('Enviando requisição:');
console.log(JSON.stringify(requestData, null, 2));
interceptedRequest.continue({
method: requestData.method,
postData: requestData.body,
headers: requestData.headers,
});
});
}
addHeader(key, value) {
this.customHeaders[key] = value;
}
getData(api) {
const buildNestedObject = (target, path, value) => {
const keys = path
.replace(/\[(\d+)\]/g, '.$1')
.replace(/^\$/, '')
.replace(/^\./, '')
.split('.');
let current = target;
keys.forEach((key, index) => {
const isLast = index === keys.length - 1;
const nextKey = keys[index + 1];
if (!isLast) {
const isArray = /^\d+$/.test(nextKey);
if (!(key in current)) {
current[key] = isArray ? [] : {};
}
current = current[key];
} else {
current[key] = value;
}
});
};
switch (api.contentType) {
case 'application/json':
if (api.bodyFields.length === 1 && api.bodyFields[0].key === '$') {
const val = api.bodyFields[0].value;
if (typeof val === 'string') {
return JSON.stringify([val]);
}
return JSON.stringify(val);
}
const jsonBody = {};
api.bodyFields.filter(field => !field.disabled).forEach(field => {
buildNestedObject(jsonBody, field.key, field.value);
});
return JSON.stringify(jsonBody);
case 'application/x-www-form-urlencoded':
const urlParams = new URLSearchParams();
api.bodyFields.filter(field => !field.disabled).forEach(field => {
urlParams.append(field.key, field.value);
});
return urlParams.toString();
case 'multipart/form-data':
const form = new FormData();
api.bodyFields.filter(field => !field.disabled).forEach(field => {
form.append(field.key, field.value);
});
return this.formDataToMultipartString(form);
default:
return '';
}
}
formDataToMultipartString(formData) {
const boundary = `----WebKitFormBoundary${Math.random().toString().substring(2)}`;
let multipartString = '';
for (const [key, value] of formData.entries()) {
multipartString += `--${boundary}\r\n`;
multipartString += `Content-Disposition: form-data; name="${key}"\r\n\r\n`;
multipartString += `${value}\r\n`;
}
multipartString += `--${boundary}--`;
return multipartString;
}
getBoundary() {
return `----WebKitFormBoundary${Math.random().toString().substring(2)}`;
}
async getLog(response) {
return {
url: response.url(),
statusCode: response.status(),
bodyText: await response.text(),
bodyJson: await response.json(),
};
}
extractFields(responseData, extractList) {
const result = {};
for (const { key, alias } of extractList) {
try {
let value;
if (key === '$') {
value = Array.isArray(responseData)
? responseData
: Array.isArray(responseData.data)
? responseData.data
: Array.isArray(responseData.json)
? responseData.json
: null;
if (!value) throw new Error(`Não é possível extrair array usando '$'.`);
} else {
const normalizedPath = key
.replace(/^\$/, '')
.replace(/^\./, '')
.replace(/\[(\d+)\]/g, '.$1');
value = normalizedPath
.split('.')
.reduce((acc, part) => acc?.[part], responseData);
}
result[alias] = value;
} catch (err) {
console.warn(`Erro ao extrair "${key}" como "${alias}": ${err.message}`);
result[alias] = undefined;
}
}
return result;
}
}