-
Notifications
You must be signed in to change notification settings - Fork 118
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
Disable following of redirects by default #454
Changes from 9 commits
55406ad
51cb83c
85a19eb
15b8013
83b1ac5
1b8a92b
ee3582b
28a8061
6fb8ead
6f424af
0c0d475
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ exclude_lines = | |
if getattr(typing, 'TYPE_CHECKING', False): | ||
pragma: no cover | ||
@bottle.(get|post|route) | ||
pytest.fail |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,10 @@ | |
from bravado.client import SwaggerClient | ||
from bravado.exception import BravadoConnectionError | ||
from bravado.exception import BravadoTimeoutError | ||
from bravado.exception import HTTPMovedPermanently | ||
from bravado.http_client import HttpClient | ||
from bravado.http_future import FutureAdapter | ||
from bravado.requests_client import RequestsClient | ||
from bravado.swagger_model import Loader | ||
|
||
|
||
|
@@ -159,6 +161,17 @@ def _class_fqn(clz): | |
}, | ||
}, | ||
}, | ||
'/redirect': { | ||
'get': { | ||
'operationId': 'redirect_test', | ||
'produces': ['application/json'], | ||
'responses': { | ||
'301': { | ||
'description': 'HTTP/301', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
|
@@ -234,6 +247,14 @@ def sleep_api(): | |
return sec_to_sleep | ||
|
||
|
||
@bottle.get('/redirect') | ||
def redirect_test(): | ||
return bottle.HTTPResponse( | ||
status=301, | ||
headers={'Location': '/json'}, | ||
) | ||
|
||
|
||
def run_bottle_server(port): | ||
"""Wrapper function for bottle.run so that the child Python interpreter finds the bottle routes on Windows.""" | ||
bottle.run(quiet=True, host='localhost', port=port) | ||
|
@@ -473,6 +494,34 @@ def test_msgpack_support(self, swagger_http_server): | |
assert response.headers['Content-Type'] == APP_MSGPACK | ||
assert unpackb(response.raw_bytes, encoding='utf-8') == API_RESPONSE | ||
|
||
def test_following_redirects(self, swagger_http_server): | ||
# the FidoClient doesn't have a way to turn off redirects being followed | ||
# so limit this test to the RequestsClient instead | ||
if not isinstance(self.http_client, RequestsClient): | ||
pytest.skip('Following redirects is only supported in the RequestsClient') | ||
|
||
response = self.http_client.request({ | ||
'method': 'GET', | ||
'url': '{server_address}/redirect'.format(server_address=swagger_http_server), | ||
'params': {}, | ||
'allow_redirects': True, | ||
}).result(timeout=1) | ||
|
||
assert isinstance(response, IncomingResponse) and response.status_code == 200 | ||
|
||
def test_redirects_are_not_followed(self, swagger_http_server): | ||
try: | ||
self.http_client.request({ | ||
'method': 'GET', | ||
'url': '{server_address}/redirect'.format(server_address=swagger_http_server), | ||
'params': {}, | ||
}).result(timeout=1) | ||
except HTTPMovedPermanently as exc: | ||
assert isinstance(exc.response, IncomingResponse) and exc.response.status_code == 301 | ||
assert isinstance(exc.response, IncomingResponse) and exc.response.headers['Location'] == '/json' | ||
else: | ||
pytest.fail("Expected exception was not raised") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend using with pytest.raises(HTTPMovedPermanently) as excinfo:
self.http_client.request({
'method': 'GET',
'url': '{server_address}/redirect'.format(server_address=swagger_http_server),
'params': {},
}).result(timeout=1)
exc = excinfo.value
assert isinstance(exc.response, IncomingResponse) and exc.response.status_code == 301
assert isinstance(exc.response, IncomingResponse) and exc.response.headers['Location'] == '/json' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hadn't realise that was support by Changed to that method instead and removed the coverage ignore. |
||
|
||
def test_timeout_errors_are_thrown_as_BravadoTimeoutError(self, swagger_http_server): | ||
if not self.http_future_adapter_type.timeout_errors: | ||
pytest.skip('{} does NOT defines timeout_errors'.format(self.http_future_adapter_type)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this a blacklist instead of a whitelist? bravado-asyncio does have a way to turn off following redirects, and I'd like to be able to run the test there too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, although it's somewhat clunky due to how coverage and test dependencies are loaded. If there's better suggestions on how to implement blacklisting without having to load the thing being blacklisted, I'm happy to change it.