-
Notifications
You must be signed in to change notification settings - Fork 333
/
functional.spec.ts
164 lines (153 loc) · 4.54 KB
/
functional.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
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
import { DiagnosticSeverity, HttpParamStyles, IHttpOperation } from '@stoplight/types';
import { httpInputs, httpOperations, httpOutputs } from '../../__tests__/fixtures';
import { validateInput, validateOutput } from '../index';
import { assertRight, assertLeft } from '@stoplight/prism-core/src/__tests__/utils';
const BAD_INPUT = Object.assign({}, httpInputs[2], {
body: { name: 'Shopping', completed: 'yes' },
url: Object.assign({}, httpInputs[2].url, { query: { overwrite: 'true' } }),
headers: { 'x-todos-publish': 'yesterday' },
});
const GOOD_INPUT = Object.assign({}, httpInputs[2], {
url: Object.assign({}, httpInputs[0].url, { query: { completed: true } }),
});
const BAD_OUTPUT = Object.assign({}, httpOutputs[1], {
body: { name: 'Shopping', completed: 'yes' },
headers: { 'x-todos-publish': 'yesterday', 'content-type': 'application/something' },
});
describe('HttpValidator', () => {
describe('validateInput()', () => {
describe('all validations are turned on', () => {
it('returns validation errors for whole request structure', () => {
expect(validateInput({ resource: httpOperations[2], element: BAD_INPUT })).toMatchSnapshot();
});
describe('when all required params are provided', () => {
it('returns no validation errors', () => {
assertRight(validateInput({ resource: httpOperations[0], element: GOOD_INPUT }));
});
});
});
describe('deprecated keyword validation', () => {
const resource: IHttpOperation = {
id: 'abc',
method: 'get',
path: '/test',
responses: [
{
code: '200',
},
],
request: {
query: [
{
style: HttpParamStyles.Form,
deprecated: true,
name: 'productId',
},
],
},
};
it('returns warnings', () => {
assertLeft(
validateInput({
resource,
element: {
method: 'get',
url: {
path: '/test',
query: {
productId: 'abc',
},
},
},
}),
error =>
expect(error).toEqual([
{
code: 'deprecated',
message: 'Query param productId is deprecated',
path: ['query', 'productId'],
severity: DiagnosticSeverity.Warning,
},
])
);
});
it('does not return warnings', () => {
assertRight(
validateInput({
resource,
element: {
method: 'get',
url: {
path: '/test',
query: {},
},
},
})
);
});
});
describe('headers validation', () => {
it('is case insensitive', () => {
assertRight(
validateInput({
resource: {
method: 'GET',
path: '/hey',
responses: [
{
code: '200',
},
],
id: 'hey',
request: {
headers: [
{
name: 'API_KEY',
style: HttpParamStyles.Simple,
schema: {
type: 'string',
},
required: true,
},
],
},
},
element: {
method: 'get',
url: {
path: '/hey',
},
headers: {
api_Key: 'ha',
},
},
})
);
});
});
describe('query validation', () => {
it('returns only query validation errors', () => {
assertLeft(
validateInput({
resource: httpOperations[2],
element: BAD_INPUT,
}),
error =>
expect(error).toContainEqual({
code: 'pattern',
message: 'should match pattern "^(yes|no)$"',
path: ['query', 'overwrite'],
severity: DiagnosticSeverity.Error,
})
);
});
});
});
describe('validateOutput()', () => {
describe('all validations are turned on', () => {
it('returns validation errors for whole request structure', () => {
expect(validateOutput({ resource: httpOperations[1], element: BAD_OUTPUT })).toMatchSnapshot();
});
});
});
});