-
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
common handler to validate operation handler response
Documented in AbstractApi._response_from_handler. This method is implemented by subclasses. Body and tuples are validated by connexion.operations.validation.validate_operation_output fixes #578
- Loading branch information
Showing
13 changed files
with
294 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import six | ||
|
||
from connexion.utils import normalize_tuple | ||
|
||
BODY_TYPES = (six.text_type, six.binary_type, dict, list) | ||
|
||
|
||
def validate_operation_output(response): | ||
"""Will validate the format returned by a handler.""" | ||
if isinstance(response, BODY_TYPES): | ||
response = (response, ) | ||
body, status, headers = normalize_tuple(response, 3) | ||
if not isinstance(body, BODY_TYPES): | ||
raise ValueError( | ||
"first returned value has to be {}, got {}".format( | ||
BODY_TYPES, type(body) | ||
) | ||
) | ||
status = status or 200 | ||
if headers is not None and not isinstance(headers, dict): | ||
raise ValueError( | ||
"Type of 3rd return value is dict, got {}".format( | ||
type(headers) | ||
) | ||
) | ||
return body, status, headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import json | ||
|
||
import pytest | ||
|
||
from connexion.apis import AbstractAPI | ||
from connexion.utils import Jsonifier | ||
|
||
|
||
@pytest.mark.parametrize("body,mimetype,expected", [ | ||
(None, None, b"null\n"), | ||
# is returned as it is with mimetype text/plain | ||
("test", "text/plain", b"test"), | ||
(b"test", "text/plain", b"test"), | ||
("test", "application/json", b'"test"\n'), | ||
(b"test", "application/json", b'"test"\n'), | ||
]) | ||
def test_encode_body(body, mimetype, expected): | ||
"""Test the body encoding. | ||
Jsonifier adds a `\n` after the serialized string. | ||
""" | ||
assert AbstractAPI.encode_body(body, mimetype) == expected | ||
|
||
|
||
@pytest.mark.parametrize("body", [ | ||
None, | ||
{"test": 1}, | ||
["test"], | ||
]) | ||
def test_encode_body_objects(body): | ||
encoded = AbstractAPI.encode_body(body, mimetype="application/json") | ||
serde = Jsonifier(json) | ||
assert encoded == serde.dumps(body).encode("UTF-8") | ||
assert encoded.decode("UTF-8")[-1] == "\n" | ||
assert serde.loads(encoded) == body |
Oops, something went wrong.