Skip to content

Commit

Permalink
fixes 'no redirects' configuration on redirect policy (#5790)
Browse files Browse the repository at this point in the history
* fixes no redirects configuration on redirect policy

* adds a no redirects test
  • Loading branch information
kristapratico authored Jun 11, 2019
1 parent 1e2f0ab commit 55c8295
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions sdk/core/azure-core/azure/core/pipeline/policies/redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core/examples/examples_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
18 changes: 17 additions & 1 deletion sdk/core/azure-core/examples/examples_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 55c8295

Please sign in to comment.