Files
connexion/tests/decorators/test_uri_parsing.py
Daniel Grossmann-Kavanagh 2f074998e3 URI parsing decorator (#613)
- array logic refactored into one place.
- validation.py and parameter.py no longer try to join the array, and the split it again.
- validation of defaults now works, because the validator is given the correct type.
- some additional classes that change the behavior of deduplicating query parameters that are defined multiple times
  - **AlwaysMultiURIParser** that is backwards compatible, warts and all (used by default)
  - **Swagger2URIParser** that adheres to the spec's definition of `collectionFormat: multi` and uses the last-defined query parameter value (ex. `query?a=1&a=2` => `a = 2`)
  - **FirstValueURIParser** that behaves like Swagger2URIParser, except that the first-defined value is used (ex. `query?a=1&a=2` => `a=1`)
2018-07-09 11:06:24 +02:00

66 lines
2.4 KiB
Python

import pytest
from connexion.decorators.uri_parsing import (AlwaysMultiURIParser,
FirstValueURIParser,
Swagger2URIParser)
QUERY1 = ["a", "b,c", "d,e,f"]
QUERY2 = ["a", "b|c", "d|e|f"]
PATH1 = "d,e,f"
PATH2 = "d|e|f"
CSV = "csv"
PIPES = "pipes"
MULTI = "multi"
@pytest.mark.parametrize("parser_class, expected, query_in, collection_format", [
(Swagger2URIParser, ['d', 'e', 'f'], QUERY1, CSV),
(FirstValueURIParser, ['a'], QUERY1, CSV),
(AlwaysMultiURIParser, ['a', 'b', 'c', 'd', 'e', 'f'], QUERY1, CSV),
(Swagger2URIParser, ['a', 'b', 'c', 'd', 'e', 'f'], QUERY1, MULTI),
(FirstValueURIParser, ['a', 'b', 'c', 'd', 'e', 'f'], QUERY1, MULTI),
(AlwaysMultiURIParser, ['a', 'b', 'c', 'd', 'e', 'f'], QUERY1, MULTI),
(Swagger2URIParser, ['d', 'e', 'f'], QUERY2, PIPES),
(FirstValueURIParser, ['a'], QUERY2, PIPES),
(AlwaysMultiURIParser, ['a', 'b', 'c', 'd', 'e', 'f'], QUERY2, PIPES)])
def test_uri_parser_query_params(parser_class, expected, query_in, collection_format):
class Request(object):
query = {"letters": query_in}
path_params = {}
request = Request()
parameters = [
{"name": "letters",
"in": "query",
"type": "array",
"items": {"type": "string"},
"collectionFormat": collection_format}
]
p = parser_class(parameters)
res = p(lambda x: x)(request)
assert res.query["letters"] == expected
@pytest.mark.parametrize("parser_class, expected, query_in, collection_format", [
(Swagger2URIParser, ['d', 'e', 'f'], PATH1, CSV),
(FirstValueURIParser, ['d', 'e', 'f'], PATH1, CSV),
(AlwaysMultiURIParser, ['d', 'e', 'f'], PATH1, CSV),
(Swagger2URIParser, ['d', 'e', 'f'], PATH2, PIPES),
(FirstValueURIParser, ['d', 'e', 'f'], PATH2, PIPES),
(AlwaysMultiURIParser, ['d', 'e', 'f'], PATH2, PIPES)])
def test_uri_parser_path_params(parser_class, expected, query_in, collection_format):
class Request(object):
query = {}
path_params = {"letters": query_in}
request = Request()
parameters = [
{"name": "letters",
"in": "path",
"type": "array",
"items": {"type": "string"},
"collectionFormat": collection_format}
]
p = parser_class(parameters)
res = p(lambda x: x)(request)
assert res.path_params["letters"] == expected