Skip to content

Commit d603236

Browse files
Added tests for issue oapi-codegen#17
1 parent bd401ff commit d603236

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

oapi_validate_test.go

+49
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/maxlength/: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,43 @@ 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 even though the parameter is encoded
249+
// to 3 chars and the parameter is limited to maxLength: 1
250+
{
251+
rec := doGet(t, e, "http://deepmap.ai/resource/maxlength/%2B")
252+
assert.Equal(t, http.StatusNoContent, rec.Code)
253+
assert.True(t, called, "Handler should have been called")
254+
called = false
255+
}
256+
257+
// Let's send a request with an unencoded parameter
258+
// It should pass as well
259+
{
260+
rec := doGet(t, e, "http://deepmap.ai/resource/maxlength/+")
261+
assert.Equal(t, http.StatusNoContent, rec.Code)
262+
assert.True(t, called, "Handler should have been called")
263+
called = false
264+
}
265+
266+
// Let's send a request with an encoded parameter
267+
// It should pass validation
268+
{
269+
rec := doGet(t, e, "http://deepmap.ai/resource/pattern/%2B1234")
270+
assert.Equal(t, http.StatusNoContent, rec.Code)
271+
assert.True(t, called, "Handler should have been called")
272+
called = false
273+
}
274+
275+
// Let's send a request with an unencoded parameter
276+
// It should pass as well
277+
{
278+
rec := doGet(t, e, "http://deepmap.ai/resource/pattern/+1234")
279+
assert.Equal(t, http.StatusNoContent, rec.Code)
280+
assert.True(t, called, "Handler should have been called")
281+
called = false
282+
}
234283
}
235284

236285
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/maxlength/{param}:
43+
get:
44+
operationId: getMaxLengthResourceParameter
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)