Skip to content

Commit

Permalink
Added tests for issue #17
Browse files Browse the repository at this point in the history
  • Loading branch information
florentchauveau committed Jan 5, 2025
1 parent 396ed03 commit 58055c3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
49 changes: 49 additions & 0 deletions oapi_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ func TestOapiRequestValidator(t *testing.T) {
called = true
return nil
})
// add a Handler for an encoded path parameter
// this needs to be installed before calling the first doGet
// because of echo internals (maxParam)
e.GET("/resource/maxlength/:encoded", func(c echo.Context) error {
called = true
return c.NoContent(http.StatusNoContent)
})
e.GET("/resource/pattern/:encoded", func(c echo.Context) error {
called = true
return c.NoContent(http.StatusNoContent)
})

// Let's send the request to the wrong server, this should return 404
{
rec := doGet(t, e, "http://not.deepmap.ai/resource")
Expand Down Expand Up @@ -231,6 +243,43 @@ func TestOapiRequestValidator(t *testing.T) {
assert.False(t, called, "Handler should not have been called")
called = false
}

// Let's send a request with an encoded parameter
// It should pass validation even though the parameter is encoded
// to 3 chars and the parameter is limited to maxLength: 1
{
rec := doGet(t, e, "http://deepmap.ai/resource/maxlength/%2B")
assert.Equal(t, http.StatusNoContent, rec.Code)
assert.True(t, called, "Handler should have been called")
called = false
}

// Let's send a request with an unencoded parameter
// It should pass as well
{
rec := doGet(t, e, "http://deepmap.ai/resource/maxlength/+")
assert.Equal(t, http.StatusNoContent, rec.Code)
assert.True(t, called, "Handler should have been called")
called = false
}

// Let's send a request with an encoded parameter
// It should pass validation
{
rec := doGet(t, e, "http://deepmap.ai/resource/pattern/%2B1234")
assert.Equal(t, http.StatusNoContent, rec.Code)
assert.True(t, called, "Handler should have been called")
called = false
}

// Let's send a request with an unencoded parameter
// It should pass as well
{
rec := doGet(t, e, "http://deepmap.ai/resource/pattern/+1234")
assert.Equal(t, http.StatusNoContent, rec.Code)
assert.True(t, called, "Handler should have been called")
called = false
}
}

func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
Expand Down
38 changes: 32 additions & 6 deletions test_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ paths:
minimum: 10
maximum: 100
responses:
'200':
"200":
description: success
content:
application/json:
Expand All @@ -29,7 +29,7 @@ paths:
post:
operationId: createResource
responses:
'204':
"204":
description: No content
requestBody:
required: true
Expand All @@ -39,14 +39,40 @@ paths:
properties:
name:
type: string
/resource/maxlength/{param}:
get:
operationId: getMaxLengthResourceParameter
parameters:
- name: param
in: path
required: true
schema:
type: string
maxLength: 1
responses:
"204":
description: success
/resource/pattern/{param}:
get:
operationId: getPatternResourceParameter
parameters:
- name: param
in: path
required: true
schema:
type: string
pattern: '^\+[1-9]+$'
responses:
"204":
description: success
/protected_resource:
get:
operationId: getProtectedResource
security:
- BearerAuth:
- someScope
responses:
'204':
"204":
description: no content
/protected_resource2:
get:
Expand All @@ -55,7 +81,7 @@ paths:
- BearerAuth:
- otherScope
responses:
'204':
"204":
description: no content
/protected_resource_401:
get:
Expand All @@ -64,7 +90,7 @@ paths:
- BearerAuth:
- unauthorized
responses:
'401':
"401":
description: no content
/multiparamresource:
get:
Expand All @@ -85,7 +111,7 @@ paths:
minimum: 10
maximum: 100
responses:
'200':
"200":
description: success
content:
application/json:
Expand Down

0 comments on commit 58055c3

Please sign in to comment.