Skip to content

Commit 91b2a73

Browse files
Added tests for issue #17
1 parent bd401ff commit 91b2a73

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

oapi_validate_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ func TestOapiRequestValidator(t *testing.T) {
129129
called = true
130130
return nil
131131
})
132+
// add a Handler for an encoded path parameter
133+
// this needs to be installed before calling the first doGet
134+
// because of echo internals (maxParam)
135+
e.GET("/resource/minlength/:encoded", func(c echo.Context) error {
136+
called = true
137+
return c.NoContent(http.StatusNoContent)
138+
})
139+
e.GET("/resource/pattern/:encoded", func(c echo.Context) error {
140+
called = true
141+
return c.NoContent(http.StatusNoContent)
142+
})
143+
132144
// Let's send the request to the wrong server, this should return 404
133145
{
134146
rec := doGet(t, e, "http://not.deepmap.ai/resource")
@@ -231,6 +243,42 @@ func TestOapiRequestValidator(t *testing.T) {
231243
assert.False(t, called, "Handler should not have been called")
232244
called = false
233245
}
246+
247+
// Let's send a request with an encoded parameter
248+
// It should pass validation
249+
{
250+
rec := doGet(t, e, "http://deepmap.ai/resource/minlength/%2B")
251+
assert.Equal(t, http.StatusNoContent, rec.Code)
252+
assert.True(t, called, "Handler should have been called")
253+
called = false
254+
}
255+
256+
// Let's send a request with an unencoded parameter
257+
// It should pass as well
258+
{
259+
rec := doGet(t, e, "http://deepmap.ai/resource/minlength/+")
260+
assert.Equal(t, http.StatusNoContent, rec.Code)
261+
assert.True(t, called, "Handler should have been called")
262+
called = false
263+
}
264+
265+
// Let's send a request with an encoded parameter
266+
// It should pass validation
267+
{
268+
rec := doGet(t, e, "http://deepmap.ai/resource/pattern/%2B1234")
269+
assert.Equal(t, http.StatusNoContent, rec.Code)
270+
assert.True(t, called, "Handler should have been called")
271+
called = false
272+
}
273+
274+
// Let's send a request with an unencoded parameter
275+
// It should pass as well
276+
{
277+
rec := doGet(t, e, "http://deepmap.ai/resource/pattern/+1234")
278+
assert.Equal(t, http.StatusNoContent, rec.Code)
279+
assert.True(t, called, "Handler should have been called")
280+
called = false
281+
}
234282
}
235283

236284
func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {

test_spec.yaml

+32-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ paths:
1616
minimum: 10
1717
maximum: 100
1818
responses:
19-
'200':
19+
"200":
2020
description: success
2121
content:
2222
application/json:
@@ -29,7 +29,7 @@ paths:
2929
post:
3030
operationId: createResource
3131
responses:
32-
'204':
32+
"204":
3333
description: No content
3434
requestBody:
3535
required: true
@@ -39,14 +39,40 @@ paths:
3939
properties:
4040
name:
4141
type: string
42+
/resource/minlength/{param}:
43+
get:
44+
operationId: getMinLengthResourceParameter
45+
parameters:
46+
- name: param
47+
in: path
48+
required: true
49+
schema:
50+
type: string
51+
maxLength: 1
52+
responses:
53+
"204":
54+
description: success
55+
/resource/pattern/{param}:
56+
get:
57+
operationId: getPatternResourceParameter
58+
parameters:
59+
- name: param
60+
in: path
61+
required: true
62+
schema:
63+
type: string
64+
pattern: '^\+[1-9]+$'
65+
responses:
66+
"204":
67+
description: success
4268
/protected_resource:
4369
get:
4470
operationId: getProtectedResource
4571
security:
4672
- BearerAuth:
4773
- someScope
4874
responses:
49-
'204':
75+
"204":
5076
description: no content
5177
/protected_resource2:
5278
get:
@@ -55,7 +81,7 @@ paths:
5581
- BearerAuth:
5682
- otherScope
5783
responses:
58-
'204':
84+
"204":
5985
description: no content
6086
/protected_resource_401:
6187
get:
@@ -64,7 +90,7 @@ paths:
6490
- BearerAuth:
6591
- unauthorized
6692
responses:
67-
'401':
93+
"401":
6894
description: no content
6995
/multiparamresource:
7096
get:
@@ -85,7 +111,7 @@ paths:
85111
minimum: 10
86112
maximum: 100
87113
responses:
88-
'200':
114+
"200":
89115
description: success
90116
content:
91117
application/json:

0 commit comments

Comments
 (0)