|
1 | 1 | # coding=utf-8
|
2 |
| -# pylint: disable=missing-docstring,protected-access,too-few-public-methods |
| 2 | +# pylint: disable=missing-docstring,protected-access,too-few-public-methods,too-many-lines |
3 | 3 | import gzip
|
4 | 4 | import json
|
5 | 5 | import os
|
@@ -788,6 +788,166 @@ def test_retry_config_external():
|
788 | 788 | assert retry_err.value.reason == error
|
789 | 789 |
|
790 | 790 |
|
| 791 | +@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/' |
| 795 | + |
| 796 | + safe_headers = { |
| 797 | + 'Authorization': 'foo', |
| 798 | + 'WWW-Authenticate': 'bar', |
| 799 | + 'Cookie': 'baz', |
| 800 | + 'Cookie2': 'baz2', |
| 801 | + } |
| 802 | + |
| 803 | + responses.add( |
| 804 | + responses.GET, url_from, status=302, adding_headers={'Location': url_to}, body='just about to redirect' |
| 805 | + ) |
| 806 | + responses.add(responses.GET, url_to, status=200, body='successfully redirected') |
| 807 | + |
| 808 | + service = BaseService(service_url=url_from, authenticator=NoAuthAuthenticator()) |
| 809 | + |
| 810 | + prepped = service.prepare_request('GET', '', headers=safe_headers) |
| 811 | + response = service.send(prepped) |
| 812 | + result = response.get_result() |
| 813 | + |
| 814 | + assert result.status_code == 200 |
| 815 | + assert result.url == url_to |
| 816 | + assert result.text == 'successfully redirected' |
| 817 | + |
| 818 | + # Make sure the headers are included in the 2nd, redirected request. |
| 819 | + redirected_headers = responses.calls[1].request.headers |
| 820 | + for key in safe_headers: |
| 821 | + assert key in redirected_headers |
| 822 | + |
| 823 | + |
| 824 | +@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/' |
| 828 | + |
| 829 | + safe_headers = { |
| 830 | + 'Authorization': 'foo', |
| 831 | + 'WWW-Authenticate': 'bar', |
| 832 | + 'Cookie': 'baz', |
| 833 | + 'Cookie2': 'baz2', |
| 834 | + } |
| 835 | + |
| 836 | + responses.add( |
| 837 | + responses.GET, url_from, status=302, adding_headers={'Location': url_to}, body='just about to redirect' |
| 838 | + ) |
| 839 | + responses.add(responses.GET, url_to, status=200, body='successfully redirected') |
| 840 | + |
| 841 | + service = BaseService(service_url=url_from, authenticator=NoAuthAuthenticator()) |
| 842 | + |
| 843 | + prepped = service.prepare_request('GET', '', headers=safe_headers) |
| 844 | + response = service.send(prepped) |
| 845 | + result = response.get_result() |
| 846 | + |
| 847 | + assert result.status_code == 200 |
| 848 | + assert result.url == url_to |
| 849 | + assert result.text == 'successfully redirected' |
| 850 | + |
| 851 | + # Make sure the headers have been excluded from the 2nd, redirected request. |
| 852 | + redirected_headers = responses.calls[1].request.headers |
| 853 | + for key in safe_headers: |
| 854 | + assert key not in redirected_headers |
| 855 | + |
| 856 | + |
| 857 | +@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/' |
| 861 | + |
| 862 | + safe_headers = { |
| 863 | + 'Authorization': 'foo', |
| 864 | + 'WWW-Authenticate': 'bar', |
| 865 | + 'Cookie': 'baz', |
| 866 | + 'Cookie2': 'baz2', |
| 867 | + } |
| 868 | + |
| 869 | + responses.add( |
| 870 | + responses.GET, url_from, status=302, adding_headers={'Location': url_to}, body='just about to redirect' |
| 871 | + ) |
| 872 | + responses.add(responses.GET, url_to, status=200, body='successfully redirected') |
| 873 | + |
| 874 | + service = BaseService(service_url=url_from, authenticator=NoAuthAuthenticator()) |
| 875 | + |
| 876 | + prepped = service.prepare_request('GET', '', headers=safe_headers) |
| 877 | + response = service.send(prepped) |
| 878 | + result = response.get_result() |
| 879 | + |
| 880 | + assert result.status_code == 200 |
| 881 | + assert result.url == url_to |
| 882 | + assert result.text == 'successfully redirected' |
| 883 | + |
| 884 | + # Make sure the headers have been excluded from the 2nd, redirected request. |
| 885 | + redirected_headers = responses.calls[1].request.headers |
| 886 | + for key in safe_headers: |
| 887 | + assert key not in redirected_headers |
| 888 | + |
| 889 | + |
| 890 | +@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/' |
| 894 | + |
| 895 | + safe_headers = { |
| 896 | + 'Authorization': 'foo', |
| 897 | + 'WWW-Authenticate': 'bar', |
| 898 | + 'Cookie': 'baz', |
| 899 | + 'Cookie2': 'baz2', |
| 900 | + } |
| 901 | + |
| 902 | + responses.add( |
| 903 | + responses.GET, url_from, status=302, adding_headers={'Location': url_to}, body='just about to redirect' |
| 904 | + ) |
| 905 | + responses.add(responses.GET, url_to, status=200, body='successfully redirected') |
| 906 | + |
| 907 | + service = BaseService(service_url=url_from, authenticator=NoAuthAuthenticator()) |
| 908 | + |
| 909 | + prepped = service.prepare_request('GET', '', headers=safe_headers) |
| 910 | + response = service.send(prepped) |
| 911 | + result = response.get_result() |
| 912 | + |
| 913 | + assert result.status_code == 200 |
| 914 | + assert result.url == url_to |
| 915 | + assert result.text == 'successfully redirected' |
| 916 | + |
| 917 | + # Make sure the headers have been excluded from the 2nd, redirected request. |
| 918 | + redirected_headers = responses.calls[1].request.headers |
| 919 | + for key in safe_headers: |
| 920 | + assert key not in redirected_headers |
| 921 | + |
| 922 | + |
| 923 | +@responses.activate |
| 924 | +def test_redirect_ibm_to_ibm_exhausted_fail(): |
| 925 | + redirects = 11 |
| 926 | + safe_headers = { |
| 927 | + 'Authorization': 'foo', |
| 928 | + 'WWW-Authenticate': 'bar', |
| 929 | + 'Cookie': 'baz', |
| 930 | + 'Cookie2': 'baz2', |
| 931 | + } |
| 932 | + |
| 933 | + for i in range(redirects): |
| 934 | + responses.add( |
| 935 | + responses.GET, |
| 936 | + f'http://region{i+1}.cloud.ibm.com/', |
| 937 | + status=302, |
| 938 | + adding_headers={'Location': f'http://region{i+2}.cloud.ibm.com/'}, |
| 939 | + body='just about to redirect', |
| 940 | + ) |
| 941 | + |
| 942 | + service = BaseService(service_url='http://region1.cloud.ibm.com/', authenticator=NoAuthAuthenticator()) |
| 943 | + |
| 944 | + with pytest.raises(MaxRetryError) as ex: |
| 945 | + prepped = service.prepare_request('GET', '', headers=safe_headers) |
| 946 | + service.send(prepped) |
| 947 | + |
| 948 | + assert ex.value.reason == 'reached the maximum number of redirects: 10' |
| 949 | + |
| 950 | + |
791 | 951 | @responses.activate
|
792 | 952 | def test_user_agent_header():
|
793 | 953 | service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
|
|
0 commit comments