Skip to content

Commit d2c92d8

Browse files
committed
tests: more unit tests
1 parent d394a88 commit d2c92d8

9 files changed

+168
-33
lines changed

test/fixtures/accept-a-charset-on-content-type.js.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,5 @@ module.exports = {
2525
},
2626
request: {
2727
body: {},
28-
headers: {
29-
'content-type': 'application/json',
30-
},
3128
},
3229
}

test/fixtures/accept-a-required-query-param.js.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ module.exports = {
2222
},
2323
},
2424
request: {
25-
path: '/foo/asdf?foo=asdf',
2625
route: 'foo/{path1}',
2726
query: {
2827
foo: 'asdf'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = {
2+
validateArgs: {
3+
paths: {
4+
'/foo': {
5+
get: {
6+
parameters: [
7+
{
8+
in: 'query',
9+
name: 'foo',
10+
type: 'string',
11+
required: true,
12+
},
13+
],
14+
}
15+
}
16+
},
17+
},
18+
request: {
19+
path: 'foo',
20+
query: {
21+
foo: 'asdf',
22+
additional1: 'bbbbb',
23+
}
24+
},
25+
requestOpts: {
26+
strictQueryParamValidation: false
27+
}
28+
};
29+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = {
2+
validateArgs: {
3+
paths: {
4+
'/foo': {
5+
get: {
6+
parameters: [
7+
{
8+
in: 'query',
9+
name: 'foo',
10+
type: 'string',
11+
required: false,
12+
},
13+
],
14+
}
15+
}
16+
},
17+
},
18+
request: {
19+
path: 'foo',
20+
query: { }
21+
}
22+
};
23+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module.exports = {
2+
validateArgs: {
3+
paths: {
4+
'/foo': {
5+
get: {
6+
parameters: [
7+
{
8+
in: 'query',
9+
name: 'startDate',
10+
schema: {
11+
type: 'string',
12+
format: 'date-time',
13+
},
14+
},
15+
],
16+
}
17+
}
18+
},
19+
},
20+
request: {
21+
path: 'foo',
22+
query: {
23+
startDate: '2024-02-01T01:34:03.001Z',
24+
}
25+
},
26+
};
27+
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
module.exports = {
22
validateArgs: {
33
paths: {
4-
'/foo/{path1}': {
4+
'/foo': {
55
get: {
66
parameters: [
7-
{
8-
in: 'path',
9-
name: 'path1',
10-
type: 'string',
11-
required: true,
12-
},
137
{
148
in: 'query',
159
name: 'foo',
@@ -22,16 +16,14 @@ module.exports = {
2216
},
2317
},
2418
request: {
25-
path: '/foo/asdf?',
26-
route: 'foo/{path1}',
27-
query: {}
28-
},
29-
expectedErrors: [
30-
{
31-
status: 400,
32-
code: 'Validation-required-query-parameter',
33-
source: { parameter: 'foo' },
34-
title: "The query parameter is required."
19+
path: 'foo',
20+
query: {
21+
foo: 'asdf',
22+
additional1: 'bbbbb',
3523
}
36-
],
24+
},
25+
requestOpts: {
26+
strictQueryParamValidation: false
27+
}
3728
};
29+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module.exports = {
2+
validateArgs: {
3+
paths: {
4+
'/foo': {
5+
get: {
6+
parameters: [
7+
{
8+
in: 'query',
9+
name: 'startDate',
10+
schema: {
11+
type: 'string',
12+
format: 'date-time',
13+
},
14+
},
15+
],
16+
}
17+
}
18+
},
19+
},
20+
request: {
21+
path: 'foo',
22+
query: {
23+
startDate: 'hello',
24+
}
25+
},
26+
expectedErrors: [
27+
{
28+
code: 'Validation-format',
29+
source: {
30+
pointer: '#/paths/foo/get/parameters/startDate/format'
31+
},
32+
status: 400,
33+
"title": 'must match format "date-time"'
34+
}
35+
]
36+
};
37+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = {
2+
validateArgs: {
3+
paths: {
4+
'/foo': {
5+
get: {
6+
parameters: [
7+
{
8+
in: 'query',
9+
name: 'foo',
10+
type: 'string',
11+
required: true,
12+
},
13+
],
14+
}
15+
}
16+
},
17+
},
18+
request: {
19+
path: 'foo',
20+
query: {
21+
foo: 'asdf',
22+
additional1: 'bbbbb',
23+
}
24+
},
25+
expectedErrors: [
26+
{
27+
status: 400,
28+
code: 'Validation-invalid-query-parameter',
29+
source: { parameter: 'additional1' },
30+
title: "The query parameter is not supported."
31+
}
32+
],
33+
};
34+

test/unit/ajv-validator-api.spec.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,11 @@ interface TestFixture {
3333
}
3434
request: {
3535
method?: string
36-
path?: string
36+
route?: string
3737
body?: unknown
3838
params?: {
3939
[key: string]: string
4040
}
41-
headers?: {
42-
[key: string]: string
43-
}
4441
query?: {
4542
[key: string]: string
4643
}
@@ -77,7 +74,7 @@ describe('The api validator', () => {
7774
} else {
7875
if (fixture.validateArgs.requestBody || fixture.validateArgs.parameters) {
7976
const operations: { [method in OpenAPIV3.HttpMethods]?: OpenAPIV3.OperationObject<any> } = {}
80-
spec.paths[fixture.request.path ?? '/test'] = operations
77+
spec.paths[`/${fixture.request.route}` ?? '/test'] = operations
8178
operations[(fixture.request.method as OpenAPIV3.HttpMethods) ?? 'post'] = {
8279
requestBody: fixture.validateArgs.requestBody,
8380
responses: {},
@@ -88,7 +85,11 @@ describe('The api validator', () => {
8885
const validator = new AjvOpenApiValidator(spec)
8986

9087
if (fixture.validateArgs.requestBody && fixture.request.body) {
91-
const result = validator.validateRequestBody(fixture.request.path ?? '/test', fixture.request.method ?? 'post', fixture.request.body)
88+
const result = validator.validateRequestBody(
89+
`/${fixture.request.route}` ?? '/test',
90+
fixture.request.method ?? 'post',
91+
fixture.request.body
92+
)
9293
if (fixture.expectedErrors) {
9394
expect(result).toEqual(fixture.expectedErrors)
9495
} else {
@@ -117,11 +118,7 @@ describe('The api validator', () => {
117118
}
118119
}
119120
if (operation.requestBody && fixture.request.body) {
120-
const result = validator.validateRequestBody(
121-
fixture.request.path ?? '/test',
122-
fixture.request.method ?? 'post',
123-
fixture.request.body
124-
)
121+
const result = validator.validateRequestBody(path, methodName, fixture.request.body)
125122
if (fixture.expectedErrors) {
126123
expect(result).toEqual(fixture.expectedErrors)
127124
} else {

0 commit comments

Comments
 (0)