-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.ts
99 lines (87 loc) · 3.98 KB
/
index.spec.ts
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
import { ScenarioDescription } from './models/ScenarioDescription';
import { AuthenticationScenarioItem, BasicAuthenticationScenarioItem } from './models/ScenarioItem';
import { evalContentVariable, expectAssertAsJest, assertHeader, assertBody } from './utils';
import { evalPathParameters, buildPathWithParameters } from './pathUtils';
import { evalQueryParameters, buildPathWithQueryParams } from './queryParamsUtils';
import { evaluateBody } from './bodyUtils';
const jp = require('jsonpath');
const frisby = require('frisby');
const scenarioDescription: ScenarioDescription = require('../scenari/json-placeholder-post.json');
const extractedVariables: any = {};
let authenticationItem: AuthenticationScenarioItem;
const evalAuthHeaders = () => {
const authenticationHeaders: any = {};
if (authenticationItem && authenticationItem.authenticationType === 'basic') {
const basicAuthenticationItem: BasicAuthenticationScenarioItem = authenticationItem as BasicAuthenticationScenarioItem;
authenticationHeaders.authorization = 'Basic ' + new Buffer(`${basicAuthenticationItem.username}:${basicAuthenticationItem.password}`).toString('base64');
}
return authenticationHeaders;
}
const expectResponseToBeOfJSONFormat = (res: any, expect: any) => {
expect(res.headers.get('Content-Type')).toBeDefined();
expect(res.headers.get('Content-Type')).toContain('application/json');
expect(res.json).toBeDefined();
}
const createTestForScenarioItem = (scenarioItem) => {
const baseUrl = scenarioItem.url;
const path = scenarioItem.operation.path;
const operationId = scenarioItem.operation.operationId;
const summary = scenarioItem.operation.summary;
it(`${operationId} (${path}) should ${summary.toLowerCase()}`, done => {
const authenticationHeaders: any = evalAuthHeaders();
const pathParameters = evalPathParameters(scenarioItem, extractedVariables);
const pathWithParameters = buildPathWithParameters(path, pathParameters);
const queryParams = evalQueryParameters(scenarioItem, extractedVariables);
const pathWithQueryParams = buildPathWithQueryParams(queryParams, pathWithParameters);
const parameterHeaders: any = {};
const body = evaluateBody(scenarioItem, parameterHeaders, extractedVariables);
frisby
.fetch(`${baseUrl}${pathWithQueryParams}`, {
method: scenarioItem.operation.method,
headers: {
accept: scenarioItem.accept,
...authenticationHeaders,
...parameterHeaders
},
body
})
// .inspectRequest()
.then((res) => {
if (scenarioItem.accept !== 'application/json') {
throw new Error(`Content type ${scenarioItem.accept} is not yet supported`);
}
expectResponseToBeOfJSONFormat(res, expect);
scenarioItem.asserts.forEach(assertion => {
if (assertion.in === 'header') {
assertHeader(assertion, res, extractedVariables);
} else if (assertion.in === 'body') {
assertBody(assertion, res, extractedVariables);
}
});
if (scenarioItem.extracts) {
scenarioItem.extracts.forEach(extraction => {
if (extraction.in === 'header') {
extractedVariables[extraction.as] = res.headers.get(extraction.field);
} else if (extraction.in === 'body') {
const extractedValues = jp.query(res.json, extraction.field);
if (extractedValues.length > 0) {
extractedVariables[extraction.as] = extractedValues[0];
} else {
throw new Error(`Failed to extract variable ${extraction.as} with JSONPath ${extraction.field}`);
}
}
});
}
done();
});
});
}
describe(scenarioDescription.name, () => {
scenarioDescription.scenario.forEach(scenarioItem => {
if (scenarioItem.type === 'authentication') {
authenticationItem = scenarioItem;
} else if (scenarioItem.type === 'path') {
createTestForScenarioItem(scenarioItem);
}
})
});