From 55c8295f5484f5132a43c69a476ffff2dab08ee0 Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Tue, 11 Jun 2019 16:17:58 -0700 Subject: [PATCH] fixes 'no redirects' configuration on redirect policy (#5790) * fixes no redirects configuration on redirect policy * adds a no redirects test --- .../azure/core/pipeline/policies/redirect.py | 4 ++-- .../core/pipeline/policies/redirect_async.py | 2 +- sdk/core/azure-core/examples/examples_async.py | 2 +- sdk/core/azure-core/examples/examples_sync.py | 18 +++++++++++++++++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/redirect.py b/sdk/core/azure-core/azure/core/pipeline/policies/redirect.py index 0056f4974082..126ae4adb340 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/redirect.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/redirect.py @@ -141,7 +141,7 @@ def increment(self, settings, response, redirect_location): response.http_request.method = 'GET' for non_redirect_header in self._remove_headers_on_redirect: response.http_request.headers.pop(non_redirect_header, None) - return settings['redirects'] > 0 or not settings['allow'] + return settings['redirects'] > 0 def send(self, request): """Sends the PipelineRequest object to the next policy. @@ -158,7 +158,7 @@ def send(self, request): while retryable: response = self.next.send(request) redirect_location = self.get_redirect_location(response) - if redirect_location: + if redirect_location and redirect_settings['allow']: retryable = self.increment(redirect_settings, response, redirect_location) request.http_request = response.http_request continue diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/redirect_async.py b/sdk/core/azure-core/azure/core/pipeline/policies/redirect_async.py index 714e37c7e0a0..6bef60776ff7 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/redirect_async.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/redirect_async.py @@ -65,7 +65,7 @@ async def send(self, request): while redirects_remaining: response = await self.next.send(request) redirect_location = self.get_redirect_location(response) - if redirect_location: + if redirect_location and redirect_settings['allow']: redirects_remaining = self.increment(redirect_settings, response, redirect_location) request.http_request = response.http_request continue diff --git a/sdk/core/azure-core/examples/examples_async.py b/sdk/core/azure-core/examples/examples_async.py index 5a8d640a848c..18f3f4ceef3e 100644 --- a/sdk/core/azure-core/examples/examples_async.py +++ b/sdk/core/azure-core/examples/examples_async.py @@ -73,7 +73,7 @@ async def test_example_async_redirect_policy(): # It can also be overridden per operation. async with AsyncPipelineClient(base_url=url, config=config) as client: - response = await client._pipeline.run(request, redirect_max=5) + response = await client._pipeline.run(request, permit_redirects=True, redirect_max=5) # [END async_redirect_policy] diff --git a/sdk/core/azure-core/examples/examples_sync.py b/sdk/core/azure-core/examples/examples_sync.py index 0312c5839330..d935935411fb 100644 --- a/sdk/core/azure-core/examples/examples_sync.py +++ b/sdk/core/azure-core/examples/examples_sync.py @@ -69,12 +69,28 @@ def test_example_redirect_policy(): # It can also be overridden per operation. client = PipelineClient(base_url=url, config=config) request = client.get(url) - pipeline_response = client._pipeline.run(request, redirect_max=5) + pipeline_response = client._pipeline.run(request, permit_redirects=True, redirect_max=5) # [END redirect_policy] response = pipeline_response.http_response assert response.status_code == 200 + +def test_example_no_redirects(): + url = "https://bing.com" + + config = Configuration() + config.redirect_policy = RedirectPolicy.no_redirects() + + client = PipelineClient(base_url=url, config=config) + request = client.get(url) + pipeline_response = client._pipeline.run(request) + + response = pipeline_response.http_response + # bing returns 301 if not redirected + assert response.status_code == 301 + + def test_example_retry_policy(): url = "https://bing.com"