|
| 1 | +import io |
| 2 | +import sys |
| 3 | + |
| 4 | + |
| 5 | +class Socket: |
| 6 | + def __init__(self): |
| 7 | + self._write_buffer = io.BytesIO() |
| 8 | + self._read_buffer = io.BytesIO(b"HTTP/1.0 200 OK\r\n\r\n") |
| 9 | + |
| 10 | + def connect(self, address): |
| 11 | + pass |
| 12 | + |
| 13 | + def write(self, buf): |
| 14 | + self._write_buffer.write(buf) |
| 15 | + |
| 16 | + def readline(self): |
| 17 | + return self._read_buffer.readline() |
| 18 | + |
| 19 | + |
| 20 | +class usocket: |
| 21 | + AF_INET = 2 |
| 22 | + SOCK_STREAM = 1 |
| 23 | + IPPROTO_TCP = 6 |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def getaddrinfo(host, port, af=0, type=0, flags=0): |
| 27 | + return [(usocket.AF_INET, usocket.SOCK_STREAM, usocket.IPPROTO_TCP, "", ("127.0.0.1", 80))] |
| 28 | + |
| 29 | + def socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP): |
| 30 | + return Socket() |
| 31 | + |
| 32 | + |
| 33 | +sys.modules["usocket"] = usocket |
| 34 | +# ruff: noqa: E402 |
| 35 | +import requests |
| 36 | + |
| 37 | + |
| 38 | +def format_message(response): |
| 39 | + return response.raw._write_buffer.getvalue().decode("utf8") |
| 40 | + |
| 41 | + |
| 42 | +def test_simple_get(): |
| 43 | + response = requests.request("GET", "http://example.com") |
| 44 | + |
| 45 | + assert response.raw._write_buffer.getvalue() == ( |
| 46 | + b"GET / HTTP/1.0\r\n" + b"Connection: close\r\n" + b"Host: example.com\r\n\r\n" |
| 47 | + ), format_message(response) |
| 48 | + |
| 49 | + |
| 50 | +def test_get_auth(): |
| 51 | + response = requests.request( |
| 52 | + "GET", "http://example.com", auth=("test-username", "test-password") |
| 53 | + ) |
| 54 | + |
| 55 | + assert response.raw._write_buffer.getvalue() == ( |
| 56 | + b"GET / HTTP/1.0\r\n" |
| 57 | + + b"Host: example.com\r\n" |
| 58 | + + b"Authorization: Basic dGVzdC11c2VybmFtZTp0ZXN0LXBhc3N3b3Jk\r\n" |
| 59 | + + b"Connection: close\r\n\r\n" |
| 60 | + ), format_message(response) |
| 61 | + |
| 62 | + |
| 63 | +def test_get_custom_header(): |
| 64 | + response = requests.request("GET", "http://example.com", headers={"User-Agent": "test-agent"}) |
| 65 | + |
| 66 | + assert response.raw._write_buffer.getvalue() == ( |
| 67 | + b"GET / HTTP/1.0\r\n" |
| 68 | + + b"User-Agent: test-agent\r\n" |
| 69 | + + b"Host: example.com\r\n" |
| 70 | + + b"Connection: close\r\n\r\n" |
| 71 | + ), format_message(response) |
| 72 | + |
| 73 | + |
| 74 | +def test_post_json(): |
| 75 | + response = requests.request("GET", "http://example.com", json="test") |
| 76 | + |
| 77 | + assert response.raw._write_buffer.getvalue() == ( |
| 78 | + b"GET / HTTP/1.0\r\n" |
| 79 | + + b"Connection: close\r\n" |
| 80 | + + b"Content-Type: application/json\r\n" |
| 81 | + + b"Host: example.com\r\n" |
| 82 | + + b"Content-Length: 6\r\n\r\n" |
| 83 | + + b'"test"' |
| 84 | + ), format_message(response) |
| 85 | + |
| 86 | + |
| 87 | +def test_post_chunked_data(): |
| 88 | + def chunks(): |
| 89 | + yield "test" |
| 90 | + |
| 91 | + response = requests.request("GET", "http://example.com", data=chunks()) |
| 92 | + |
| 93 | + assert response.raw._write_buffer.getvalue() == ( |
| 94 | + b"GET / HTTP/1.0\r\n" |
| 95 | + + b"Transfer-Encoding: chunked\r\n" |
| 96 | + + b"Host: example.com\r\n" |
| 97 | + + b"Connection: close\r\n\r\n" |
| 98 | + + b"4\r\ntest\r\n" |
| 99 | + + b"0\r\n\r\n" |
| 100 | + ), format_message(response) |
| 101 | + |
| 102 | + |
| 103 | +def test_overwrite_get_headers(): |
| 104 | + response = requests.request( |
| 105 | + "GET", "http://example.com", headers={"Connection": "keep-alive", "Host": "test.com"} |
| 106 | + ) |
| 107 | + |
| 108 | + assert response.raw._write_buffer.getvalue() == ( |
| 109 | + b"GET / HTTP/1.0\r\n" + b"Host: test.com\r\n" + b"Connection: keep-alive\r\n\r\n" |
| 110 | + ), format_message(response) |
| 111 | + |
| 112 | + |
| 113 | +def test_overwrite_post_json_headers(): |
| 114 | + response = requests.request( |
| 115 | + "GET", |
| 116 | + "http://example.com", |
| 117 | + json="test", |
| 118 | + headers={"Content-Type": "text/plain", "Content-Length": "10"}, |
| 119 | + ) |
| 120 | + |
| 121 | + assert response.raw._write_buffer.getvalue() == ( |
| 122 | + b"GET / HTTP/1.0\r\n" |
| 123 | + + b"Connection: close\r\n" |
| 124 | + + b"Content-Length: 10\r\n" |
| 125 | + + b"Content-Type: text/plain\r\n" |
| 126 | + + b"Host: example.com\r\n\r\n" |
| 127 | + + b'"test"' |
| 128 | + ), format_message(response) |
| 129 | + |
| 130 | + |
| 131 | +def test_overwrite_post_chunked_data_headers(): |
| 132 | + def chunks(): |
| 133 | + yield "test" |
| 134 | + |
| 135 | + response = requests.request( |
| 136 | + "GET", "http://example.com", data=chunks(), headers={"Content-Length": "4"} |
| 137 | + ) |
| 138 | + |
| 139 | + assert response.raw._write_buffer.getvalue() == ( |
| 140 | + b"GET / HTTP/1.0\r\n" |
| 141 | + + b"Host: example.com\r\n" |
| 142 | + + b"Content-Length: 4\r\n" |
| 143 | + + b"Connection: close\r\n\r\n" |
| 144 | + + b"test" |
| 145 | + ), format_message(response) |
| 146 | + |
| 147 | + |
| 148 | +test_simple_get() |
| 149 | +test_get_auth() |
| 150 | +test_get_custom_header() |
| 151 | +test_post_json() |
| 152 | +test_post_chunked_data() |
| 153 | +test_overwrite_get_headers() |
| 154 | +test_overwrite_post_json_headers() |
| 155 | +test_overwrite_post_chunked_data_headers() |
0 commit comments