Skip to content

Commit

Permalink
Merge pull request #5 from olipratt/contenttypeparameters
Browse files Browse the repository at this point in the history
Support content-type parameters with parameters
  • Loading branch information
olipratt authored Apr 17, 2018
2 parents 6513f88 + 5d2ad2c commit e163718
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
8 changes: 5 additions & 3 deletions swaggerconformance/_basictests.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ def single_operation_test(client, operation, params):
assert result.status in operation.response_codes, \
"Response code {} not in {}".format(result.status,
operation.response_codes)
assert 'application/json' in result.headers['Content-Type'], \
"application/json not in {}".format(result.headers['Content-Type'])
assert any(entry.strip().startswith('application/json') \
for entry in result.headers['Content-Type']), \
"'application/json' not in 'Content-Type' header: {}" \
.format(result.headers['Content-Type'])

# Run the test, which takes one less parameter than expected due to the
# hypothesis decorator providing the last one.
single_operation_test(client, operation) # pylint: disable=E1120
single_operation_test(client, operation) # pylint: disable=E1120
46 changes: 36 additions & 10 deletions tests/test_swaggerconformance.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,34 @@
CONTENT_TYPE_JSON = 'application/json'


def _respond_to_method(method, path, response_json=None, status=200):
def _respond_to_method(method, path, response_json, status, content_type):
url_re = re.compile(SCHEMA_URL_BASE + path + '$')
responses.add(method, url_re, json=response_json, status=status,
content_type=CONTENT_TYPE_JSON)
content_type=content_type)

def respond_to_get(path, response_json=None, status=200):
def respond_to_get(path, response_json=None, status=200,
content_type=CONTENT_TYPE_JSON):
"""Respond to a GET request to the provided path."""
_respond_to_method(responses.GET, path, response_json, status)
_respond_to_method(responses.GET, path, response_json, status,
content_type)

def respond_to_post(path, response_json=None, status=200):
def respond_to_post(path, response_json=None, status=200,
content_type=CONTENT_TYPE_JSON):
"""Respond to a POST request to the provided path."""
_respond_to_method(responses.POST, path, response_json, status)
_respond_to_method(responses.POST, path, response_json, status,
content_type)

def respond_to_put(path, response_json=None, status=200):
def respond_to_put(path, response_json=None, status=200,
content_type=CONTENT_TYPE_JSON):
"""Respond to a PUT request to the provided path."""
_respond_to_method(responses.PUT, path, response_json, status)
_respond_to_method(responses.PUT, path, response_json, status,
content_type)

def respond_to_delete(path, response_json=None, status=200):
def respond_to_delete(path, response_json=None, status=200,
content_type=CONTENT_TYPE_JSON):
"""Respond to a DELETE request to the provided path."""
_respond_to_method(responses.DELETE, path, response_json, status)
_respond_to_method(responses.DELETE, path, response_json, status,
content_type)


class APITemplateTestCase(unittest.TestCase):
Expand Down Expand Up @@ -160,6 +168,24 @@ def test_running_as_module(self):
dunder_main,
[TEST_SCHEMA_PATH])

@responses.activate
def test_content_type_header_with_parameters(self):
"""Content type header parameters should be allowed."""
content_type_extra = 'application/json; charset=utf-8'
respond_to_get('/schema')
respond_to_get('/apps',
response_json=[{'name': 'test'}],
content_type=content_type_extra)
respond_to_get(r'/apps/.+', status=404,
content_type=content_type_extra)
respond_to_put(r'/apps/.+', status=204,
content_type=content_type_extra)
respond_to_delete(r'/apps/.+', status=204,
content_type=content_type_extra)

swaggerconformance.api_conformance_test(TEST_SCHEMA_PATH,
cont_on_err=False)


class ParameterTypesTestCase(unittest.TestCase):
"""Tests to cover all the options/constraints on parameters."""
Expand Down

0 comments on commit e163718

Please sign in to comment.