|
18 | 18 |
|
19 | 19 | import gzip
|
20 | 20 | import io
|
| 21 | +import json |
21 | 22 | import os
|
22 | 23 | import re
|
23 | 24 | import ssl
|
@@ -866,3 +867,139 @@ def test_surrogatepass_into_bytes(self):
|
866 | 867 | con = self._get_mock_connection(response_body=buf)
|
867 | 868 | status, headers, data = con.perform_request("GET", "/")
|
868 | 869 | self.assertEqual(u"你好\uda6a", data)
|
| 870 | + |
| 871 | + |
| 872 | +class TestConnectionHttpbin: |
| 873 | + """Tests the HTTP connection implementations against a live server E2E""" |
| 874 | + |
| 875 | + def httpbin_anything(self, conn, **kwargs): |
| 876 | + status, headers, data = conn.perform_request("GET", "/anything", **kwargs) |
| 877 | + data = json.loads(data) |
| 878 | + data["headers"].pop( |
| 879 | + "X-Amzn-Trace-Id", None |
| 880 | + ) # Remove this header as it's put there by AWS. |
| 881 | + return (status, data) |
| 882 | + |
| 883 | + def test_urllib3_connection(self): |
| 884 | + # Defaults |
| 885 | + conn = Urllib3HttpConnection("httpbin.org", port=443, use_ssl=True) |
| 886 | + user_agent = conn._get_default_user_agent() |
| 887 | + status, data = self.httpbin_anything(conn) |
| 888 | + assert status == 200 |
| 889 | + assert data["method"] == "GET" |
| 890 | + assert data["headers"] == { |
| 891 | + "Accept-Encoding": "identity", |
| 892 | + "Content-Type": "application/json", |
| 893 | + "Host": "httpbin.org", |
| 894 | + "User-Agent": user_agent, |
| 895 | + } |
| 896 | + |
| 897 | + # http_compress=False |
| 898 | + conn = Urllib3HttpConnection( |
| 899 | + "httpbin.org", port=443, use_ssl=True, http_compress=False |
| 900 | + ) |
| 901 | + status, data = self.httpbin_anything(conn) |
| 902 | + assert status == 200 |
| 903 | + assert data["method"] == "GET" |
| 904 | + assert data["headers"] == { |
| 905 | + "Accept-Encoding": "identity", |
| 906 | + "Content-Type": "application/json", |
| 907 | + "Host": "httpbin.org", |
| 908 | + "User-Agent": user_agent, |
| 909 | + } |
| 910 | + |
| 911 | + # http_compress=True |
| 912 | + conn = Urllib3HttpConnection( |
| 913 | + "httpbin.org", port=443, use_ssl=True, http_compress=True |
| 914 | + ) |
| 915 | + status, data = self.httpbin_anything(conn) |
| 916 | + assert status == 200 |
| 917 | + assert data["headers"] == { |
| 918 | + "Accept-Encoding": "gzip,deflate", |
| 919 | + "Content-Type": "application/json", |
| 920 | + "Host": "httpbin.org", |
| 921 | + "User-Agent": user_agent, |
| 922 | + } |
| 923 | + |
| 924 | + # Headers |
| 925 | + conn = Urllib3HttpConnection( |
| 926 | + "httpbin.org", |
| 927 | + port=443, |
| 928 | + use_ssl=True, |
| 929 | + http_compress=True, |
| 930 | + headers={"header1": "value1"}, |
| 931 | + ) |
| 932 | + status, data = self.httpbin_anything( |
| 933 | + conn, headers={"header2": "value2", "header1": "override!"} |
| 934 | + ) |
| 935 | + assert status == 200 |
| 936 | + assert data["headers"] == { |
| 937 | + "Accept-Encoding": "gzip,deflate", |
| 938 | + "Content-Type": "application/json", |
| 939 | + "Host": "httpbin.org", |
| 940 | + "Header1": "override!", |
| 941 | + "Header2": "value2", |
| 942 | + "User-Agent": user_agent, |
| 943 | + } |
| 944 | + |
| 945 | + def test_requests_connection(self): |
| 946 | + # Defaults |
| 947 | + conn = RequestsHttpConnection("httpbin.org", port=443, use_ssl=True) |
| 948 | + user_agent = conn._get_default_user_agent() |
| 949 | + status, data = self.httpbin_anything(conn) |
| 950 | + assert status == 200 |
| 951 | + assert data["method"] == "GET" |
| 952 | + assert data["headers"] == { |
| 953 | + "Accept-Encoding": "identity", |
| 954 | + "Content-Type": "application/json", |
| 955 | + "Host": "httpbin.org", |
| 956 | + "User-Agent": user_agent, |
| 957 | + } |
| 958 | + |
| 959 | + # http_compress=False |
| 960 | + conn = RequestsHttpConnection( |
| 961 | + "httpbin.org", port=443, use_ssl=True, http_compress=False |
| 962 | + ) |
| 963 | + status, data = self.httpbin_anything(conn) |
| 964 | + assert status == 200 |
| 965 | + assert data["method"] == "GET" |
| 966 | + assert data["headers"] == { |
| 967 | + "Accept-Encoding": "identity", |
| 968 | + "Content-Type": "application/json", |
| 969 | + "Host": "httpbin.org", |
| 970 | + "User-Agent": user_agent, |
| 971 | + } |
| 972 | + |
| 973 | + # http_compress=True |
| 974 | + conn = RequestsHttpConnection( |
| 975 | + "httpbin.org", port=443, use_ssl=True, http_compress=True |
| 976 | + ) |
| 977 | + status, data = self.httpbin_anything(conn) |
| 978 | + assert status == 200 |
| 979 | + assert data["headers"] == { |
| 980 | + "Accept-Encoding": "gzip,deflate", |
| 981 | + "Content-Type": "application/json", |
| 982 | + "Host": "httpbin.org", |
| 983 | + "User-Agent": user_agent, |
| 984 | + } |
| 985 | + |
| 986 | + # Headers |
| 987 | + conn = RequestsHttpConnection( |
| 988 | + "httpbin.org", |
| 989 | + port=443, |
| 990 | + use_ssl=True, |
| 991 | + http_compress=True, |
| 992 | + headers={"header1": "value1"}, |
| 993 | + ) |
| 994 | + status, data = self.httpbin_anything( |
| 995 | + conn, headers={"header2": "value2", "header1": "override!"} |
| 996 | + ) |
| 997 | + assert status == 200 |
| 998 | + assert data["headers"] == { |
| 999 | + "Accept-Encoding": "gzip,deflate", |
| 1000 | + "Content-Type": "application/json", |
| 1001 | + "Host": "httpbin.org", |
| 1002 | + "Header1": "override!", |
| 1003 | + "Header2": "value2", |
| 1004 | + "User-Agent": user_agent, |
| 1005 | + } |
0 commit comments