Skip to content

Commit ef1f6b5

Browse files
committed
chore: test improvements
Signed-off-by: Norbert Biczo <[email protected]>
1 parent 885dbfb commit ef1f6b5

File tree

1 file changed

+82
-16
lines changed

1 file changed

+82
-16
lines changed

test/test_base_service.py

+82-16
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,9 @@ def test_retry_config_external():
789789

790790

791791
@responses.activate
792-
def test_redirect_ibm_to_ibm_success():
793-
url_from = 'http://region1.cloud.ibm.com/'
794-
url_to = 'http://region2.cloud.ibm.com/'
792+
def test_redirect_ibm_to_ibm():
793+
url_from = 'https://region1.cloud.ibm.com/'
794+
url_to = 'https://region2.cloud.ibm.com/'
795795

796796
safe_headers = {
797797
'Authorization': 'foo',
@@ -822,9 +822,9 @@ def test_redirect_ibm_to_ibm_success():
822822

823823

824824
@responses.activate
825-
def test_redirect_not_ibm_to_ibm_fail():
826-
url_from = 'http://region1.notcloud.ibm.com/'
827-
url_to = 'http://region2.cloud.ibm.com/'
825+
def test_redirect_not_ibm_to_ibm():
826+
url_from = 'https://region1.notcloud.ibm.com/'
827+
url_to = 'https://region2.cloud.ibm.com/'
828828

829829
safe_headers = {
830830
'Authorization': 'foo',
@@ -855,9 +855,9 @@ def test_redirect_not_ibm_to_ibm_fail():
855855

856856

857857
@responses.activate
858-
def test_redirect_ibm_to_not_ibm_fail():
859-
url_from = 'http://region1.cloud.ibm.com/'
860-
url_to = 'http://region2.notcloud.ibm.com/'
858+
def test_redirect_ibm_to_not_ibm():
859+
url_from = 'https://region1.cloud.ibm.com/'
860+
url_to = 'https://region2.notcloud.ibm.com/'
861861

862862
safe_headers = {
863863
'Authorization': 'foo',
@@ -888,9 +888,9 @@ def test_redirect_ibm_to_not_ibm_fail():
888888

889889

890890
@responses.activate
891-
def test_redirect_not_ibm_to_not_ibm_fail():
892-
url_from = 'http://region1.notcloud.ibm.com/'
893-
url_to = 'http://region2.notcloud.ibm.com/'
891+
def test_redirect_not_ibm_to_not_ibm():
892+
url_from = 'https://region1.notcloud.ibm.com/'
893+
url_to = 'https://region2.notcloud.ibm.com/'
894894

895895
safe_headers = {
896896
'Authorization': 'foo',
@@ -921,7 +921,73 @@ def test_redirect_not_ibm_to_not_ibm_fail():
921921

922922

923923
@responses.activate
924-
def test_redirect_ibm_to_ibm_exhausted_fail():
924+
def test_redirect_ibm_same_host():
925+
url_from = 'https://region1.cloud.ibm.com/'
926+
url_to = 'https://region1.cloud.ibm.com/'
927+
928+
safe_headers = {
929+
'Authorization': 'foo',
930+
'WWW-Authenticate': 'bar',
931+
'Cookie': 'baz',
932+
'Cookie2': 'baz2',
933+
}
934+
935+
responses.add(
936+
responses.GET, url_from, status=302, adding_headers={'Location': url_to}, body='just about to redirect'
937+
)
938+
responses.add(responses.GET, url_to, status=200, body='successfully redirected')
939+
940+
service = BaseService(service_url=url_from, authenticator=NoAuthAuthenticator())
941+
942+
prepped = service.prepare_request('GET', '', headers=safe_headers)
943+
response = service.send(prepped)
944+
result = response.get_result()
945+
946+
assert result.status_code == 200
947+
assert result.url == url_to
948+
assert result.text == 'successfully redirected'
949+
950+
# Make sure the headers have been excluded from the 2nd, redirected request.
951+
redirected_headers = responses.calls[1].request.headers
952+
for key in safe_headers:
953+
assert key in redirected_headers
954+
955+
956+
@responses.activate
957+
def test_redirect_not_ibm_same_host():
958+
url_from = 'https://region1.notcloud.ibm.com/'
959+
url_to = 'https://region1.notcloud.ibm.com/'
960+
961+
safe_headers = {
962+
'Authorization': 'foo',
963+
'WWW-Authenticate': 'bar',
964+
'Cookie': 'baz',
965+
'Cookie2': 'baz2',
966+
}
967+
968+
responses.add(
969+
responses.GET, url_from, status=302, adding_headers={'Location': url_to}, body='just about to redirect'
970+
)
971+
responses.add(responses.GET, url_to, status=200, body='successfully redirected')
972+
973+
service = BaseService(service_url=url_from, authenticator=NoAuthAuthenticator())
974+
975+
prepped = service.prepare_request('GET', '', headers=safe_headers)
976+
response = service.send(prepped)
977+
result = response.get_result()
978+
979+
assert result.status_code == 200
980+
assert result.url == url_to
981+
assert result.text == 'successfully redirected'
982+
983+
# Make sure the headers have been excluded from the 2nd, redirected request.
984+
redirected_headers = responses.calls[1].request.headers
985+
for key in safe_headers:
986+
assert key in redirected_headers
987+
988+
989+
@responses.activate
990+
def test_redirect_ibm_to_ibm_exhausted():
925991
redirects = 11
926992
safe_headers = {
927993
'Authorization': 'foo',
@@ -933,13 +999,13 @@ def test_redirect_ibm_to_ibm_exhausted_fail():
933999
for i in range(redirects):
9341000
responses.add(
9351001
responses.GET,
936-
f'http://region{i+1}.cloud.ibm.com/',
1002+
f'https://region{i+1}.cloud.ibm.com/',
9371003
status=302,
938-
adding_headers={'Location': f'http://region{i+2}.cloud.ibm.com/'},
1004+
adding_headers={'Location': f'https://region{i+2}.cloud.ibm.com/'},
9391005
body='just about to redirect',
9401006
)
9411007

942-
service = BaseService(service_url='http://region1.cloud.ibm.com/', authenticator=NoAuthAuthenticator())
1008+
service = BaseService(service_url='https://region1.cloud.ibm.com/', authenticator=NoAuthAuthenticator())
9431009

9441010
with pytest.raises(MaxRetryError) as ex:
9451011
prepped = service.prepare_request('GET', '', headers=safe_headers)

0 commit comments

Comments
 (0)