|
9 | 9 | from openapi_core.extensions.models.models import BaseModel
|
10 | 10 | from openapi_core.schema.operations.exceptions import InvalidOperation
|
11 | 11 | from openapi_core.schema.parameters.exceptions import MissingRequiredParameter
|
| 12 | +from openapi_core.schema.parameters.exceptions import InvalidParameterValue |
12 | 13 | from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
|
13 | 14 | from openapi_core.schema.responses.exceptions import (
|
14 | 15 | MissingResponseContent, InvalidResponse,
|
@@ -220,6 +221,74 @@ def test_get_pet(self, validator):
|
220 | 221 | }
|
221 | 222 |
|
222 | 223 |
|
| 224 | +class TestPathItemParamsValidator(object): |
| 225 | + |
| 226 | + @pytest.fixture |
| 227 | + def spec_dict(self, factory): |
| 228 | + return { |
| 229 | + "openapi": "3.0.0", |
| 230 | + "info": { |
| 231 | + "title": "Test path item parameter validation", |
| 232 | + "version": "0.1", |
| 233 | + }, |
| 234 | + "paths": { |
| 235 | + "/resource": { |
| 236 | + "parameters": [ |
| 237 | + { |
| 238 | + "name": "resId", |
| 239 | + "in": "query", |
| 240 | + "required": True, |
| 241 | + "schema": { |
| 242 | + "type": "integer", |
| 243 | + }, |
| 244 | + }, |
| 245 | + ], |
| 246 | + "get": { |
| 247 | + "responses": { |
| 248 | + "default": { |
| 249 | + "description": "Return the resource." |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + @pytest.fixture |
| 258 | + def spec(self, spec_dict): |
| 259 | + return create_spec(spec_dict) |
| 260 | + |
| 261 | + @pytest.fixture |
| 262 | + def validator(self, spec): |
| 263 | + return RequestValidator(spec) |
| 264 | + |
| 265 | + def test_request_missing_param(self, validator): |
| 266 | + request = MockRequest('http://example.com', 'get', '/resource') |
| 267 | + result = validator.validate(request) |
| 268 | + |
| 269 | + assert len(result.errors) == 1 |
| 270 | + assert type(result.errors[0]) == MissingRequiredParameter |
| 271 | + assert result.body is None |
| 272 | + assert result.parameters == {} |
| 273 | + |
| 274 | + def test_request_invalid_param(self, validator): |
| 275 | + request = MockRequest('http://example.com', 'get', '/resource', args={'resId': 'invalid'}) |
| 276 | + result = validator.validate(request) |
| 277 | + |
| 278 | + assert len(result.errors) == 1 |
| 279 | + assert type(result.errors[0]) == InvalidParameterValue |
| 280 | + assert result.body is None |
| 281 | + assert result.parameters == {} |
| 282 | + |
| 283 | + def test_request_valid_param(self, validator): |
| 284 | + request = MockRequest('http://example.com', 'get', '/resource', args={'resId': '10'}) |
| 285 | + result = validator.validate(request) |
| 286 | + |
| 287 | + assert len(result.errors) == 0 |
| 288 | + assert result.body is None |
| 289 | + assert result.parameters == {'query': {'resId': 10}} |
| 290 | + |
| 291 | + |
223 | 292 | class TestResponseValidator(object):
|
224 | 293 |
|
225 | 294 | host_url = 'http://petstore.swagger.io'
|
|
0 commit comments