Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow http.HTTPStatus enums as response status codes. #504

Merged
merged 6 commits into from
Oct 5, 2017

Conversation

jkrukoff
Copy link
Contributor

@jkrukoff jkrukoff commented Aug 8, 2017

Python 3.5 introduced a new enumeration "http.HTTPStatus" for
representing HTTP response status codes. The default response validation
introduced in connexion 1.1.12 highlighted the fact that connexion does
not natively support this type and was previously silently ignoring
non-integer status code representations.

This modifies the response validation code to extract the value when
given an enum instead of an int. Somewhat hacky test code is added to
check for enum support on python versions that include
"http.HTTPStatus".

Fixes #503

Changes proposed in this pull request:

  • Convert enums to ints in response validation matching.

Python 3.5 introduced a new enumeration "http.HTTPStatus" for
representing HTTP response status codes. The default response validation
introduced in connexion 1.1.12 highlighted the fact that connexion does
not natively support this type and was previously silently ignoring
non-integer status code representations.

This modifies the response validation code to extract the value when
given an enum instead of an int. Somewhat hacky test code is added to
check for enum support on python versions that include
"http.HTTPStatus".
@coveralls
Copy link

coveralls commented Aug 8, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling d7d6b4b on jkrukoff:master into d51e9c8 on zalando:master.

@jmcs
Copy link
Contributor

jmcs commented Aug 15, 2017

I like it, but can you had a unit test for custom enum (this would cover the case where someone uses a backport of the enum).

@jkrukoff
Copy link
Contributor Author

Do you have a suggestion on a good way to only run the test in python versions where Enum is available, or should I use the same ImportError based approach?

@mvalkon
Copy link
Collaborator

mvalkon commented Aug 22, 2017

@jkrukoff there's a way to run the test with only specific python versions. First you need to indicate that the test should be skipped in python versions that do not support enums. You can use the @unittest.skipIf decorator:

@unittest.skipIf(sys.version_info < (3, 5), "Not supported in this version")
def test_your_test_name(...):

If you want to run your tests with multiple python versions, we're using tox for this. To verify that the above works you can run tox with -e command line parameter as follows:

$ tox -e py27,py34,py36  

You can install tox with pip:

$ pip install tox

tox is currently not in https://github.com/zalando/connexion/blob/master/requirements-devel.txt but I'll make a PR to add it.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 38ce0c8 on jkrukoff:master into f36c246 on zalando:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 38ce0c8 on jkrukoff:master into f36c246 on zalando:master.

This reverts to exception based python version checking for both tests,
due to the suggested unittest skipping alternative not being supported
in all python versions.

"unittest.case.SkipTest: Not supported in this version" is the error
reported.
@coveralls
Copy link

coveralls commented Sep 15, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 3e9da66 on jkrukoff:master into f36c246 on zalando:master.

@jkrukoff
Copy link
Contributor Author

Added the test for arbitrary enums, which exposed an assumption inside flask that appears to only work for ints/actual HTTPStatus Enums. Moved the enum value extraction deeper into the stack to compensate.

Tried to use @unittest.skipif in the test suite, but that failed with an unsupported error from unittest for python versions <3.5. Reverted back to the previous approach to get tests passing again.

@spec-first spec-first deleted a comment Sep 15, 2017
@mvalkon
Copy link
Collaborator

mvalkon commented Sep 22, 2017

@jkrukoff unittest.skipIf has been introduced in 3.1. Out of interest, what sort of an exception did you get?

The change looks good to me.

@jkrukoff
Copy link
Contributor Author

jkrukoff commented Sep 22, 2017

It was pretty close to "unittest.skipif is unsupported in this version". It it's been present since 3.1, would explain why the python 2.7 test suite was exploding with that error.

Though, honestly, that might have been my skip message being reflected back at me. Perhaps I was hitting some configuration in the test runner that labelled skips as errors?

Rebuilding f789831 will likely trip the error again.

@@ -1,4 +1,6 @@
#!/usr/bin/env python3
import sys
import unittest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This imports are not being used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah, forgot to remove those from the attempt to use uniftest.skipIf.

@@ -154,6 +154,12 @@ def _build_flask_response(cls, mimetype=None, content_type=None,
flask_response = flask.current_app.response_class(**kwargs) # type: flask.Response

if status_code is not None:
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use hasattr here since the most common use case is status_code being an integer.

hasattr is faster than try... except AtributeError for cases where the attribute doesn't exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and you get to repeat yourself while doing the check. It'd be even faster if just didn't support enums at all!

Will update.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 3e8596e on jkrukoff:master into f36c246 on zalando:master.

1 similar comment
@coveralls
Copy link

coveralls commented Oct 4, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 3e8596e on jkrukoff:master into f36c246 on zalando:master.

@hjacobs
Copy link
Contributor

hjacobs commented Oct 5, 2017

👍

@hjacobs hjacobs merged commit 94f64b8 into spec-first:master Oct 5, 2017
mvalkon added a commit to mvalkon/connexion that referenced this pull request Oct 5, 2017
A small bug was introduced in spec-first#500 when sanitizing the request query
parameters. Instead of fetching the arguments from `request.query`,
the parameters were sanitized from the `request.form`. This causes an
error log to be printed, as the parameters are not expected for the
query (for example in the case of a POST request).

Fixes spec-first#504
mvalkon added a commit to mvalkon/connexion that referenced this pull request Nov 14, 2017
A small bug was introduced in spec-first#500 when sanitizing the request query
parameters. Instead of fetching the arguments from `request.query`,
the parameters were sanitized from the `request.form`. This causes an
error log to be printed, as the parameters are not expected for the
query (for example in the case of a POST request).

Fixes spec-first#504
mvalkon added a commit to mvalkon/connexion that referenced this pull request Nov 15, 2017
A small bug was introduced in spec-first#500 when sanitizing the request query
parameters. Instead of fetching the arguments from `request.query`,
the parameters were sanitized from the `request.form`. This causes an
error log to be printed, as the parameters are not expected for the
query (for example in the case of a POST request).

Fixes spec-first#504
hjacobs pushed a commit that referenced this pull request Nov 16, 2017
* FIX a typo causing an ERROR log

A small bug was introduced in #500 when sanitizing the request query
parameters. Instead of fetching the arguments from `request.query`,
the parameters were sanitized from the `request.form`. This causes an
error log to be printed, as the parameters are not expected for the
query (for example in the case of a POST request).

Fixes #504

* adds a fixture for testing query param sanitazion

Adds a simple fixture to test form and query parameter sanitazion. This
is mostly related to #522, in which the `formData` parameters were
treated as query parameters.

* add a test to validate form data params

* introduce testfixtures library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants