|
| 1 | +// Copyright 2019 DeepMap, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package middleware |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + "net/http/httptest" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/getkin/kin-openapi/openapi3" |
| 23 | + "github.com/labstack/echo/v4" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + |
| 26 | + "github.com/deepmap/oapi-codegen/v2/pkg/testutil" |
| 27 | +) |
| 28 | + |
| 29 | +var testSchema = `openapi: "3.0.0" |
| 30 | +info: |
| 31 | + version: 1.0.0 |
| 32 | + title: TestServer |
| 33 | +servers: |
| 34 | + - url: http://deepmap.ai |
| 35 | +paths: |
| 36 | + /resource: |
| 37 | + get: |
| 38 | + operationId: getResource |
| 39 | + parameters: |
| 40 | + - name: id |
| 41 | + in: query |
| 42 | + schema: |
| 43 | + type: integer |
| 44 | + minimum: 10 |
| 45 | + maximum: 100 |
| 46 | + responses: |
| 47 | + '200': |
| 48 | + content: |
| 49 | + application/json: |
| 50 | + schema: |
| 51 | + properties: |
| 52 | + name: |
| 53 | + type: string |
| 54 | + id: |
| 55 | + type: integer |
| 56 | + post: |
| 57 | + operationId: createResource |
| 58 | + responses: |
| 59 | + '204': |
| 60 | + description: No content |
| 61 | + requestBody: |
| 62 | + required: true |
| 63 | + content: |
| 64 | + application/json: |
| 65 | + schema: |
| 66 | + properties: |
| 67 | + name: |
| 68 | + type: string |
| 69 | +` |
| 70 | + |
| 71 | +func doGet(t *testing.T, e *echo.Echo, url string) *httptest.ResponseRecorder { |
| 72 | + response := testutil.NewRequest().Get(url).WithAcceptJson().Go(t, e) |
| 73 | + return response.Recorder |
| 74 | +} |
| 75 | + |
| 76 | +func doPost(t *testing.T, e *echo.Echo, url string, jsonBody interface{}) *httptest.ResponseRecorder { |
| 77 | + response := testutil.NewRequest().Post(url).WithJsonBody(jsonBody).Go(t, e) |
| 78 | + return response.Recorder |
| 79 | +} |
| 80 | + |
| 81 | +func TestOapiRequestValidator(t *testing.T) { |
| 82 | + swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromYAMLData([]byte(testSchema)) |
| 83 | + assert.NoError(t, err, "Error initializing swagger") |
| 84 | + |
| 85 | + // Create a new echo router |
| 86 | + e := echo.New() |
| 87 | + |
| 88 | + // Install our OpenApi based request validator |
| 89 | + e.Use(OapiRequestValidator(swagger)) |
| 90 | + |
| 91 | + called := false |
| 92 | + |
| 93 | + // Install a request handler for /resource. We want to make sure it doesn't |
| 94 | + // get called. |
| 95 | + e.GET("/resource", func(c echo.Context) error { |
| 96 | + called = true |
| 97 | + return nil |
| 98 | + }) |
| 99 | + // Let's send the request to the wrong server, this should fail validation |
| 100 | + { |
| 101 | + rec := doGet(t, e, "http://not.deepmap.ai/resource") |
| 102 | + assert.Equal(t, http.StatusBadRequest, rec.Code) |
| 103 | + assert.False(t, called, "Handler should not have been called") |
| 104 | + } |
| 105 | + |
| 106 | + // Let's send a good request, it should pass |
| 107 | + { |
| 108 | + rec := doGet(t, e, "http://deepmap.ai/resource") |
| 109 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 110 | + assert.True(t, called, "Handler should have been called") |
| 111 | + called = false |
| 112 | + } |
| 113 | + |
| 114 | + // Send an out-of-spec parameter |
| 115 | + // TODO(marcin): limit checking for numbers is not yet working in |
| 116 | + // kin-openapi, they only check the string type. I either need to do |
| 117 | + // this myself, or patch their code. |
| 118 | + //{ |
| 119 | + // rec := doGet(t, e, "http://deepmap.ai/resource?id=500") |
| 120 | + // assert.Equal(t, http.StatusBadRequest, rec.Code) |
| 121 | + // assert.False(t, called, "Handler should not have been called") |
| 122 | + // called = false |
| 123 | + //} |
| 124 | + |
| 125 | + // Send a bad parameter type |
| 126 | + // TODO(marcin): type checking is currently not yet working in kin-openapi |
| 127 | + //{ |
| 128 | + // rec := doGet(t, e, "http://deepmap.ai/resource?id=foo") |
| 129 | + // assert.Equal(t, http.StatusBadRequest, rec.Code) |
| 130 | + // assert.False(t, called, "Handler should not have been called") |
| 131 | + // called = false |
| 132 | + //} |
| 133 | + |
| 134 | + // Add a handler for the POST message |
| 135 | + e.POST("/resource", func(c echo.Context) error { |
| 136 | + called = true |
| 137 | + return c.NoContent(http.StatusNoContent) |
| 138 | + }) |
| 139 | + |
| 140 | + called = false |
| 141 | + // Send a good request body |
| 142 | + { |
| 143 | + body := struct { |
| 144 | + Name string `json:"name"` |
| 145 | + }{ |
| 146 | + Name: "Marcin", |
| 147 | + } |
| 148 | + rec := doPost(t, e, "http://deepmap.ai/resource", body) |
| 149 | + assert.Equal(t, http.StatusNoContent, rec.Code) |
| 150 | + assert.True(t, called, "Handler should have been called") |
| 151 | + called = false |
| 152 | + } |
| 153 | + |
| 154 | + // Send a malformed body |
| 155 | + { |
| 156 | + body := struct { |
| 157 | + Name int `json:"name"` |
| 158 | + }{ |
| 159 | + Name: 7, |
| 160 | + } |
| 161 | + rec := doPost(t, e, "http://deepmap.ai/resource", body) |
| 162 | + assert.Equal(t, http.StatusBadRequest, rec.Code) |
| 163 | + assert.False(t, called, "Handler should not have been called") |
| 164 | + called = false |
| 165 | + } |
| 166 | +} |
0 commit comments