Skip to content

Commit

Permalink
Fix getting header in redirect processing
Browse files Browse the repository at this point in the history
The code for processing an HTTP redirect included an incorrect method of
getting the Location header from an HTTPResponse.  It needs to use the
getheader() method on the response, instead.  This patch fixes that and
includes a unit test that covers this code path.

Change-Id: I0952fabad581b020dee07bdc4007b55b47c906aa
Closes-Bug: #1231524
  • Loading branch information
russellb committed Sep 26, 2013
1 parent 4718635 commit 50266ee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion glanceclient/common/http.py
Expand Up @@ -233,7 +233,8 @@ def _http_request(self, url, method, **kwargs):
raise exc.from_response(resp, body_str)
elif resp.status in (301, 302, 305):
# Redirected. Reissue the request to the new location.
return self._http_request(resp['location'], method, **kwargs)
return self._http_request(resp.getheader('location', None), method,
**kwargs)
elif resp.status == 300:
raise exc.from_response(resp)

Expand Down
24 changes: 24 additions & 0 deletions tests/test_http.py
Expand Up @@ -100,6 +100,30 @@ def test_connection_refused(self):
(comm_err.message, self.endpoint))
self.assertTrue(self.endpoint in comm_err.message, fail_msg)

def test_request_redirected(self):
resp = utils.FakeResponse({'location': 'http://www.example.com'},
status=302, body=StringIO.StringIO())
httplib.HTTPConnection.request(
mox.IgnoreArg(),
mox.IgnoreArg(),
headers=mox.IgnoreArg(),
)
httplib.HTTPConnection.getresponse().AndReturn(resp)

# The second request should be to the redirected location
expected_response = 'Ok'
resp2 = utils.FakeResponse({}, StringIO.StringIO(expected_response))
httplib.HTTPConnection.request(
'GET',
'http://www.example.com',
headers=mox.IgnoreArg(),
)
httplib.HTTPConnection.getresponse().AndReturn(resp2)

self.mock.ReplayAll()

self.client.json_request('GET', '/v1/images/detail')

def test_http_encoding(self):
httplib.HTTPConnection.request(
mox.IgnoreArg(),
Expand Down

0 comments on commit 50266ee

Please sign in to comment.