Files
connexion/tests/api/test_headers.py
Miguel G. Flores 005d046a8d Allow "Required: False" on Response headers - Fix #1261 (#1293)
* Add failing test for optional header

* Required: False headers don't give error if missing

* Use dict.get

* Required keys are when they are not present, or present with "True"

* Update connexion/decorators/response.py

Co-authored-by: Ruwann <ruwan.lambrichts@ml6.eu>

* Update tests for required and optional headers

Swagger2 response headers are optional, and have no 'required'
attribute. OpenAPI3 response headers are optional by default,
but can be specified to be required by setting 'required: true'.

* Make headers required for tests that rely on it

Co-authored-by: Ruwann <ruwan.lambrichts@ml6.eu>
2021-07-16 08:50:00 +02:00

52 lines
1.9 KiB
Python

import json
def test_headers_jsonifier(simple_app):
app_client = simple_app.app.test_client()
response = app_client.post('/v1.0/goodday/dan', data={}) # type: flask.Response
assert response.status_code == 201
assert response.headers["Location"] == "http://localhost/my/uri"
def test_headers_produces(simple_app):
app_client = simple_app.app.test_client()
response = app_client.post('/v1.0/goodevening/dan', data={}) # type: flask.Response
assert response.status_code == 201
assert response.headers["Location"] == "http://localhost/my/uri"
def test_header_not_returned(simple_openapi_app):
app_client = simple_openapi_app.app.test_client()
response = app_client.post('/v1.0/goodday/noheader', data={}) # type: flask.Response
assert response.status_code == 500 # view_func has not returned what was promised in spec
assert response.content_type == 'application/problem+json'
data = json.loads(response.data.decode('utf-8', 'replace'))
assert data['type'] == 'about:blank'
assert data['title'] == 'Response headers do not conform to specification'
assert data['detail'] == "Keys in header don't match response specification. Difference: Location"
assert data['status'] == 500
def test_no_content_response_have_headers(simple_app):
app_client = simple_app.app.test_client()
resp = app_client.get('/v1.0/test-204-with-headers')
assert resp.status_code == 204
assert 'X-Something' in resp.headers
def test_no_content_object_and_have_headers(simple_app):
app_client = simple_app.app.test_client()
resp = app_client.get('/v1.0/test-204-with-headers-nocontent-obj')
assert resp.status_code == 204
assert 'X-Something' in resp.headers
def test_optional_header(simple_openapi_app):
app_client = simple_openapi_app.app.test_client()
resp = app_client.get('/v1.0/test-optional-headers')
assert resp.status_code == 200
assert 'X-Optional-Header' not in resp.headers