def assert3xx(response, expected_url, status_code=302, target_status_code=200):
 if hasattr(response, 'redirect_chain'):
  assert (len(response.redirect_chain) > 0), ("Response didn't redirect as expected: Response code was %d (expected %d)" % (response.status_code, status_code))
  (url, status_code) = response.redirect_chain[(-1)]
  assert (response.status_code == target_status_code), ("Response didn't redirect as expected: Final Response code was %d (expected %d)" % (response.status_code, target_status_code))
 else:
  assert (response.status_code == status_code), ("Response didn't redirect as expected: Response code was %d (expected %d)" % (response.status_code, status_code))
  url = response['Location']
 (scheme, netloc, path, query, fragment) = urlsplit(url)
 (e_scheme, e_netloc, e_path, e_query, e_fragment) = urlsplit(expected_url)
 if ((scheme and (not e_scheme)) and (netloc and (not e_netloc))):
  expected_url = urlunsplit(('http', 'testserver', e_path, e_query, e_fragment))
 msg = ("Response redirected to '%s', expected '%s'" % (url, expected_url))
 assert (url == expected_url), msg

def assert3xx ( response , expected_url , status_code = 302 , target_status_code = 200 ) : if hasattr ( response , 'redirect_chain' ) : assert ( len ( response . redirect_chain ) > 0 ) , ( "Response didn't redirect as expected: Response code was %d (expected %d)" % ( response . status_code , status_code ) ) ( url , status_code ) = response . redirect_chain [ ( - 1 ) ] assert ( response . status_code == target_status_code ) , ( "Response didn't redirect as expected: Final Response code was %d (expected %d)" % ( response . status_code , target_status_code ) ) else : assert ( response . status_code == status_code ) , ( "Response didn't redirect as expected: Response code was %d (expected %d)" % ( response . status_code , status_code ) ) url = response [ 'Location' ] ( scheme , netloc , path , query , fragment ) = urlsplit ( url ) ( e_scheme , e_netloc , e_path , e_query , e_fragment ) = urlsplit ( expected_url ) if ( ( scheme and ( not e_scheme ) ) and ( netloc and ( not e_netloc ) ) ) : expected_url = urlunsplit ( ( 'http' , 'testserver' , e_path , e_query , e_fragment ) ) msg = ( "Response redirected to '%s', expected '%s'" % ( url , expected_url ) ) assert ( url == expected_url ) , msg
asserts redirect and final redirect matches expected url .