| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
import typing |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from urllib3.fields import _TYPE_FIELD_VALUE_TUPLE |
| 9 |
|
| 10 |
from ...port_helpers import find_unused_port |
| 11 |
|
| 12 |
if sys.version_info < (3, 11): |
| 13 |
# pyodide only works on 3.11+ |
| 14 |
pytest.skip(allow_module_level=True) |
| 15 |
|
| 16 |
# only run these tests if pytest_pyodide is installed |
| 17 |
# so we don't break non-emscripten pytest running |
| 18 |
pytest_pyodide = pytest.importorskip("pytest_pyodide") |
| 19 |
|
| 20 |
from pytest_pyodide import run_in_pyodide # type: ignore[import-not-found] # noqa: E402 |
| 21 |
|
| 22 |
from .conftest import PyodideServerInfo, ServerRunnerInfo # noqa: E402 |
| 23 |
|
| 24 |
# make our ssl certificates work in chrome |
| 25 |
pyodide_config = pytest_pyodide.config.get_global_config() |
| 26 |
pyodide_config.set_flags( |
| 27 |
"chrome", ["ignore-certificate-errors"] + pyodide_config.get_flags("chrome") |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
def test_index( |
| 32 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo, prefer_jspi: bool |
| 33 |
) -> None: |
| 34 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 35 |
def pyodide_test(selenium_coverage, host: str, port: int, prefer_jspi: bool) -> None: # type: ignore[no-untyped-def] |
| 36 |
import urllib3.contrib.emscripten.fetch |
| 37 |
from urllib3.connection import HTTPConnection |
| 38 |
from urllib3.response import BaseHTTPResponse |
| 39 |
|
| 40 |
assert urllib3.contrib.emscripten.fetch.has_jspi() == prefer_jspi |
| 41 |
conn = HTTPConnection(host, port) |
| 42 |
url = f"http://{host}:{port}/" |
| 43 |
conn.request("GET", url) |
| 44 |
response = conn.getresponse() |
| 45 |
# check methods of response |
| 46 |
assert isinstance(response, BaseHTTPResponse) |
| 47 |
assert response.url == url |
| 48 |
response.url = "http://woo" |
| 49 |
assert response.url == "http://woo" |
| 50 |
assert response.connection == conn |
| 51 |
assert response.retries is None |
| 52 |
data1 = response.data |
| 53 |
decoded1 = data1.decode("utf-8") |
| 54 |
data2 = response.data # check that getting data twice works |
| 55 |
decoded2 = data2.decode("utf-8") |
| 56 |
assert decoded1 == decoded2 == "Dummy server!" |
| 57 |
|
| 58 |
pyodide_test( |
| 59 |
selenium_coverage, |
| 60 |
testserver_http.http_host, |
| 61 |
testserver_http.http_port, |
| 62 |
prefer_jspi, |
| 63 |
) |
| 64 |
|
| 65 |
|
| 66 |
def test_pool_requests( |
| 67 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo, prefer_jspi: bool |
| 68 |
) -> None: |
| 69 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 70 |
def pyodide_test(selenium_coverage, host: str, port: int, https_port: int, prefer_jspi: bool) -> None: # type: ignore[no-untyped-def] |
| 71 |
# first with PoolManager |
| 72 |
import urllib3 |
| 73 |
import urllib3.contrib.emscripten.fetch |
| 74 |
|
| 75 |
assert urllib3.contrib.emscripten.fetch.has_jspi() == prefer_jspi |
| 76 |
|
| 77 |
http = urllib3.PoolManager() |
| 78 |
resp = http.request("GET", f"http://{host}:{port}/") |
| 79 |
# ensure that the response is cached and can be read multiple times |
| 80 |
for _ in range(2): |
| 81 |
assert resp.data.decode("utf-8") == "Dummy server!" |
| 82 |
|
| 83 |
resp2 = http.request("GET", f"http://{host}:{port}/index") |
| 84 |
assert resp2.data.decode("utf-8") == "Dummy server!" |
| 85 |
|
| 86 |
# should all have come from one pool |
| 87 |
assert len(http.pools) == 1 |
| 88 |
|
| 89 |
resp3 = http.request("GET", f"https://{host}:{https_port}/") |
| 90 |
assert resp3.data.decode("utf-8") == "Dummy server!" |
| 91 |
|
| 92 |
# one http pool + one https pool |
| 93 |
assert len(http.pools) == 2 |
| 94 |
|
| 95 |
# now with ConnectionPool |
| 96 |
# because block == True, this will fail if the connection isn't |
| 97 |
# returned to the pool correctly after the first request |
| 98 |
pool = urllib3.HTTPConnectionPool(host, port, maxsize=1, block=True) |
| 99 |
resp3 = pool.urlopen("GET", "/index") |
| 100 |
assert resp3.data.decode("utf-8") == "Dummy server!" |
| 101 |
|
| 102 |
resp4 = pool.urlopen("GET", "/") |
| 103 |
assert resp4.data.decode("utf-8") == "Dummy server!" |
| 104 |
|
| 105 |
# now with manual release of connection |
| 106 |
# first - connection should be released once all |
| 107 |
# data is read |
| 108 |
pool2 = urllib3.HTTPConnectionPool(host, port, maxsize=1, block=True) |
| 109 |
|
| 110 |
resp5 = pool2.urlopen("GET", "/index", preload_content=False) |
| 111 |
assert pool2.pool is not None |
| 112 |
# at this point, the connection should not be in the pool |
| 113 |
assert pool2.pool.qsize() == 0 |
| 114 |
assert resp5.data.decode("utf-8") == "Dummy server!" |
| 115 |
# now we've read all the data, connection should be back to the pool |
| 116 |
assert pool2.pool.qsize() == 1 |
| 117 |
resp6 = pool2.urlopen("GET", "/index", preload_content=False) |
| 118 |
assert pool2.pool.qsize() == 0 |
| 119 |
# force it back to the pool |
| 120 |
resp6.release_conn() |
| 121 |
assert pool2.pool.qsize() == 1 |
| 122 |
read_str = resp6.read() |
| 123 |
# for consistency with urllib3, this still returns the correct data even though |
| 124 |
# we are in theory not using the connection any more |
| 125 |
assert read_str.decode("utf-8") == "Dummy server!" |
| 126 |
|
| 127 |
pyodide_test( |
| 128 |
selenium_coverage, |
| 129 |
testserver_http.http_host, |
| 130 |
testserver_http.http_port, |
| 131 |
testserver_http.https_port, |
| 132 |
prefer_jspi, |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
# wrong protocol / protocol error etc. should raise an exception of http.client.HTTPException |
| 137 |
def test_wrong_protocol( |
| 138 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 139 |
) -> None: |
| 140 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 141 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 142 |
import http.client |
| 143 |
|
| 144 |
import pytest |
| 145 |
|
| 146 |
from urllib3.connection import HTTPConnection |
| 147 |
|
| 148 |
conn = HTTPConnection(host, port) |
| 149 |
with pytest.raises(http.client.HTTPException): |
| 150 |
conn.request("GET", f"http://{host}:{port}/") |
| 151 |
|
| 152 |
pyodide_test( |
| 153 |
selenium_coverage, testserver_http.http_host, testserver_http.https_port |
| 154 |
) |
| 155 |
|
| 156 |
|
| 157 |
# wrong protocol / protocol error etc. should raise an exception of http.client.HTTPException |
| 158 |
def test_bad_method( |
| 159 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 160 |
) -> None: |
| 161 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 162 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 163 |
import http.client |
| 164 |
|
| 165 |
import pytest |
| 166 |
|
| 167 |
from urllib3.connection import HTTPConnection |
| 168 |
|
| 169 |
conn = HTTPConnection(host, port) |
| 170 |
with pytest.raises(http.client.HTTPException): |
| 171 |
conn.request("TRACE", f"http://{host}:{port}/") |
| 172 |
|
| 173 |
pyodide_test( |
| 174 |
selenium_coverage, testserver_http.http_host, testserver_http.https_port |
| 175 |
) |
| 176 |
|
| 177 |
|
| 178 |
# no connection - should raise |
| 179 |
def test_no_response( |
| 180 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 181 |
) -> None: |
| 182 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 183 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 184 |
import http.client |
| 185 |
|
| 186 |
import pytest |
| 187 |
|
| 188 |
from urllib3.connection import HTTPConnection |
| 189 |
|
| 190 |
conn = HTTPConnection(host, port) |
| 191 |
with pytest.raises(http.client.HTTPException): |
| 192 |
conn.request("GET", f"http://{host}:{port}/") |
| 193 |
_ = conn.getresponse() |
| 194 |
|
| 195 |
pyodide_test(selenium_coverage, testserver_http.http_host, find_unused_port()) |
| 196 |
|
| 197 |
|
| 198 |
def test_404(selenium_coverage: typing.Any, testserver_http: PyodideServerInfo) -> None: |
| 199 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 200 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 201 |
from urllib3.connection import HTTPConnection |
| 202 |
from urllib3.response import BaseHTTPResponse |
| 203 |
|
| 204 |
conn = HTTPConnection(host, port) |
| 205 |
conn.request("GET", f"http://{host}:{port}/status?status=404 NOT FOUND") |
| 206 |
response = conn.getresponse() |
| 207 |
assert isinstance(response, BaseHTTPResponse) |
| 208 |
assert response.status == 404 |
| 209 |
|
| 210 |
pyodide_test( |
| 211 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 212 |
) |
| 213 |
|
| 214 |
|
| 215 |
# setting timeout should show a warning to js console |
| 216 |
# if we're on the ui thread, because XMLHttpRequest doesn't |
| 217 |
# support timeout in async mode if globalThis == Window |
| 218 |
@pytest.mark.without_jspi |
| 219 |
def test_timeout_warning( |
| 220 |
selenium_coverage: typing.Any, |
| 221 |
testserver_http: PyodideServerInfo, |
| 222 |
) -> None: |
| 223 |
@run_in_pyodide() # type: ignore[untyped-decorator] |
| 224 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 225 |
import js # type: ignore[import-not-found] |
| 226 |
|
| 227 |
import urllib3.contrib.emscripten.fetch |
| 228 |
from urllib3.connection import HTTPConnection |
| 229 |
|
| 230 |
old_log = js.console.warn |
| 231 |
log_msgs = [] |
| 232 |
|
| 233 |
def capture_log(*args): # type: ignore[no-untyped-def] |
| 234 |
log_msgs.append(str(args)) |
| 235 |
old_log(*args) |
| 236 |
|
| 237 |
js.console.warn = capture_log |
| 238 |
|
| 239 |
conn = HTTPConnection(host, port, timeout=1.0) |
| 240 |
conn.request("GET", f"http://{host}:{port}/") |
| 241 |
conn.getresponse() |
| 242 |
js.console.warn = old_log |
| 243 |
# should have shown timeout warning exactly once by now |
| 244 |
assert len([x for x in log_msgs if x.find("Warning: Timeout") != -1]) == 1 |
| 245 |
assert urllib3.contrib.emscripten.fetch._SHOWN_TIMEOUT_WARNING |
| 246 |
|
| 247 |
pyodide_test( |
| 248 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 249 |
) |
| 250 |
|
| 251 |
|
| 252 |
@pytest.mark.webworkers |
| 253 |
def test_timeout_in_worker_non_streaming( |
| 254 |
testserver_http: PyodideServerInfo, |
| 255 |
run_from_server: ServerRunnerInfo, |
| 256 |
) -> None: |
| 257 |
worker_code = f""" |
| 258 |
from urllib3.exceptions import TimeoutError |
| 259 |
from urllib3.connection import HTTPConnection |
| 260 |
from pyodide.ffi import JsException |
| 261 |
from http.client import HTTPException |
| 262 |
conn = HTTPConnection("{testserver_http.http_host}", {testserver_http.http_port},timeout=1.0) |
| 263 |
result=-1 |
| 264 |
try: |
| 265 |
conn.request("GET","/slow",preload_content = True) |
| 266 |
_response = conn.getresponse() |
| 267 |
result=-3 |
| 268 |
except TimeoutError as e: |
| 269 |
result=1 # we've got the correct exception |
| 270 |
except HTTPException as e: |
| 271 |
result=-3 |
| 272 |
except BaseException as e: |
| 273 |
result=-2 |
| 274 |
raise BaseException(str(result)+":"+str(type(e))+str(e.args) ) |
| 275 |
except JsException as e: |
| 276 |
result=-4 |
| 277 |
assert result == 1 |
| 278 |
""" |
| 279 |
run_from_server.run_webworker(worker_code) |
| 280 |
|
| 281 |
|
| 282 |
@pytest.mark.webworkers |
| 283 |
def test_timeout_in_worker_streaming( |
| 284 |
testserver_http: PyodideServerInfo, |
| 285 |
run_from_server: ServerRunnerInfo, |
| 286 |
) -> None: |
| 287 |
worker_code = f""" |
| 288 |
import urllib3.contrib.emscripten.fetch |
| 289 |
await urllib3.contrib.emscripten.fetch.wait_for_streaming_ready() |
| 290 |
from urllib3.exceptions import TimeoutError |
| 291 |
from urllib3.connection import HTTPConnection |
| 292 |
conn = HTTPConnection("{testserver_http.http_host}", {testserver_http.http_port},timeout=1.0) |
| 293 |
result=-1 |
| 294 |
try: |
| 295 |
conn.request("GET","/slow",preload_content=False) |
| 296 |
_response = conn.getresponse() |
| 297 |
result=-3 |
| 298 |
except TimeoutError as e: |
| 299 |
result=1 # we've got the correct exception |
| 300 |
except BaseException as e: |
| 301 |
result=-2 |
| 302 |
assert result == 1 |
| 303 |
""" |
| 304 |
run_from_server.run_webworker(worker_code) |
| 305 |
|
| 306 |
|
| 307 |
@pytest.mark.parametrize( |
| 308 |
"connection_cls", ["HTTPSConnection", "VerifiedHTTPSConnection"] |
| 309 |
) |
| 310 |
def test_index_https( |
| 311 |
selenium_coverage: typing.Any, |
| 312 |
testserver_http: PyodideServerInfo, |
| 313 |
connection_cls: str, |
| 314 |
) -> None: |
| 315 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 316 |
def pyodide_test(selenium_coverage, host: str, port: int, connection_cls: str) -> None: # type: ignore[no-untyped-def] |
| 317 |
from urllib3 import connection |
| 318 |
from urllib3.response import BaseHTTPResponse |
| 319 |
|
| 320 |
conn = getattr(connection, connection_cls)(host, port) |
| 321 |
conn.request("GET", f"https://{host}:{port}/") |
| 322 |
response = conn.getresponse() |
| 323 |
assert isinstance(response, BaseHTTPResponse) |
| 324 |
data = response.data |
| 325 |
assert data.decode("utf-8") == "Dummy server!" |
| 326 |
|
| 327 |
pyodide_test( |
| 328 |
selenium_coverage, |
| 329 |
testserver_http.http_host, |
| 330 |
testserver_http.https_port, |
| 331 |
connection_cls, |
| 332 |
) |
| 333 |
|
| 334 |
|
| 335 |
@pytest.mark.without_jspi |
| 336 |
def test_non_streaming_no_fallback_warning( |
| 337 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 338 |
) -> None: |
| 339 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 340 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 341 |
import js |
| 342 |
|
| 343 |
import urllib3.contrib.emscripten.fetch |
| 344 |
from urllib3.connection import HTTPSConnection |
| 345 |
from urllib3.response import BaseHTTPResponse |
| 346 |
|
| 347 |
log_msgs = [] |
| 348 |
old_log = js.console.warn |
| 349 |
|
| 350 |
def capture_log(*args): # type: ignore[no-untyped-def] |
| 351 |
log_msgs.append(str(args)) |
| 352 |
old_log(*args) |
| 353 |
|
| 354 |
js.console.warn = capture_log |
| 355 |
conn = HTTPSConnection(host, port) |
| 356 |
conn.request("GET", f"https://{host}:{port}/", preload_content=True) |
| 357 |
response = conn.getresponse() |
| 358 |
js.console.warn = old_log |
| 359 |
assert isinstance(response, BaseHTTPResponse) |
| 360 |
data = response.data |
| 361 |
assert data.decode("utf-8") == "Dummy server!" |
| 362 |
# no console warnings because we didn't ask it to stream the response |
| 363 |
# check no log messages |
| 364 |
assert ( |
| 365 |
len([x for x in log_msgs if x.find("Can't stream HTTP requests") != -1]) |
| 366 |
== 0 |
| 367 |
) |
| 368 |
assert not urllib3.contrib.emscripten.fetch._SHOWN_STREAMING_WARNING |
| 369 |
|
| 370 |
pyodide_test( |
| 371 |
selenium_coverage, testserver_http.http_host, testserver_http.https_port |
| 372 |
) |
| 373 |
|
| 374 |
|
| 375 |
@pytest.mark.without_jspi |
| 376 |
def test_streaming_fallback_warning( |
| 377 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 378 |
) -> None: |
| 379 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 380 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 381 |
import js |
| 382 |
|
| 383 |
import urllib3.contrib.emscripten.fetch |
| 384 |
from urllib3.connection import HTTPSConnection |
| 385 |
from urllib3.response import BaseHTTPResponse |
| 386 |
|
| 387 |
# monkeypatch is_cross_origin_isolated so that it warns about that |
| 388 |
# even if we're serving it so it is fine |
| 389 |
urllib3.contrib.emscripten.fetch.is_cross_origin_isolated = lambda: False |
| 390 |
|
| 391 |
log_msgs = [] |
| 392 |
old_log = js.console.warn |
| 393 |
|
| 394 |
def capture_log(*args): # type: ignore[no-untyped-def] |
| 395 |
log_msgs.append(str(args)) |
| 396 |
old_log(*args) |
| 397 |
|
| 398 |
js.console.warn = capture_log |
| 399 |
|
| 400 |
conn = HTTPSConnection(host, port) |
| 401 |
conn.request("GET", f"https://{host}:{port}/", preload_content=False) |
| 402 |
response = conn.getresponse() |
| 403 |
js.console.warn = old_log |
| 404 |
assert isinstance(response, BaseHTTPResponse) |
| 405 |
data = response.data |
| 406 |
assert data.decode("utf-8") == "Dummy server!" |
| 407 |
# check that it has warned about falling back to non-streaming fetch exactly once |
| 408 |
assert ( |
| 409 |
len([x for x in log_msgs if x.find("Can't stream HTTP requests") != -1]) |
| 410 |
== 1 |
| 411 |
) |
| 412 |
assert urllib3.contrib.emscripten.fetch._SHOWN_STREAMING_WARNING |
| 413 |
|
| 414 |
pyodide_test( |
| 415 |
selenium_coverage, testserver_http.http_host, testserver_http.https_port |
| 416 |
) |
| 417 |
|
| 418 |
|
| 419 |
def test_specific_method( |
| 420 |
selenium_coverage: typing.Any, |
| 421 |
testserver_http: PyodideServerInfo, |
| 422 |
) -> None: |
| 423 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 424 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 425 |
from urllib3 import HTTPSConnectionPool |
| 426 |
|
| 427 |
with HTTPSConnectionPool(host, port) as pool: |
| 428 |
path = "/specific_method?method=POST" |
| 429 |
response = pool.request("POST", path) |
| 430 |
assert response.status == 200 |
| 431 |
|
| 432 |
response = pool.request("PUT", path) |
| 433 |
assert response.status == 400 |
| 434 |
|
| 435 |
pyodide_test( |
| 436 |
selenium_coverage, testserver_http.http_host, testserver_http.https_port |
| 437 |
) |
| 438 |
|
| 439 |
|
| 440 |
@pytest.mark.webworkers |
| 441 |
def test_streaming_download( |
| 442 |
testserver_http: PyodideServerInfo, |
| 443 |
run_from_server: ServerRunnerInfo, |
| 444 |
prefer_jspi: bool, |
| 445 |
) -> None: |
| 446 |
# test streaming download, which must be in a webworker |
| 447 |
# as you can't do it on main thread |
| 448 |
|
| 449 |
# this should return the 17mb big file, and |
| 450 |
# should not log any warning about falling back |
| 451 |
bigfile_url = ( |
| 452 |
f"http://{testserver_http.http_host}:{testserver_http.http_port}/bigfile" |
| 453 |
) |
| 454 |
worker_code = f""" |
| 455 |
import urllib3.contrib.emscripten.fetch |
| 456 |
await urllib3.contrib.emscripten.fetch.wait_for_streaming_ready() |
| 457 |
from urllib3.response import BaseHTTPResponse |
| 458 |
from urllib3.connection import HTTPConnection |
| 459 |
|
| 460 |
conn = HTTPConnection("{testserver_http.http_host}", {testserver_http.http_port}) |
| 461 |
conn.request("GET", "{bigfile_url}",preload_content=False) |
| 462 |
response = conn.getresponse() |
| 463 |
assert isinstance(response, BaseHTTPResponse) |
| 464 |
assert urllib3.contrib.emscripten.fetch._SHOWN_STREAMING_WARNING==False |
| 465 |
assert(urllib3.contrib.emscripten.fetch.has_jspi() == {prefer_jspi}) |
| 466 |
data=response.data.decode('utf-8') |
| 467 |
assert len(data) == 17825792 |
| 468 |
""" |
| 469 |
run_from_server.run_webworker(worker_code) |
| 470 |
|
| 471 |
|
| 472 |
@pytest.mark.webworkers |
| 473 |
def test_streaming_close( |
| 474 |
testserver_http: PyodideServerInfo, |
| 475 |
run_from_server: ServerRunnerInfo, |
| 476 |
prefer_jspi: bool, |
| 477 |
) -> None: |
| 478 |
# test streaming download, which must be in a webworker |
| 479 |
# as you can't do it on main thread |
| 480 |
|
| 481 |
# this should return the 17mb big file, and |
| 482 |
# should not log any warning about falling back |
| 483 |
url = f"http://{testserver_http.http_host}:{testserver_http.http_port}/" |
| 484 |
worker_code = f""" |
| 485 |
import urllib3.contrib.emscripten.fetch |
| 486 |
await urllib3.contrib.emscripten.fetch.wait_for_streaming_ready() |
| 487 |
from urllib3.response import BaseHTTPResponse |
| 488 |
from urllib3.connection import HTTPConnection |
| 489 |
from io import RawIOBase |
| 490 |
|
| 491 |
conn = HTTPConnection("{testserver_http.http_host}", {testserver_http.http_port}) |
| 492 |
conn.request("GET", "{url}",preload_content=False) |
| 493 |
response = conn.getresponse() |
| 494 |
# check body is a RawIOBase stream and isn't seekable, writeable |
| 495 |
body_internal = response._response.body |
| 496 |
assert(isinstance(body_internal,RawIOBase)) |
| 497 |
assert(body_internal.writable() is False) |
| 498 |
assert(body_internal.seekable() is False) |
| 499 |
assert(body_internal.readable() is True) |
| 500 |
assert(urllib3.contrib.emscripten.fetch.has_jspi() == {prefer_jspi}) |
| 501 |
|
| 502 |
response.drain_conn() |
| 503 |
x=response.read() |
| 504 |
assert(not x) |
| 505 |
response.close() |
| 506 |
conn.close() |
| 507 |
# try and make destructor be covered |
| 508 |
# by killing everything |
| 509 |
del response |
| 510 |
del body_internal |
| 511 |
del conn |
| 512 |
""" |
| 513 |
run_from_server.run_webworker(worker_code) |
| 514 |
|
| 515 |
|
| 516 |
@pytest.mark.webworkers |
| 517 |
def test_streaming_bad_url( |
| 518 |
testserver_http: PyodideServerInfo, |
| 519 |
run_from_server: ServerRunnerInfo, |
| 520 |
) -> None: |
| 521 |
# this should cause an error |
| 522 |
# because the protocol is bad |
| 523 |
bad_url = f"hsffsdft://{testserver_http.http_host}:{testserver_http.http_port}/" |
| 524 |
# this must be in a webworker |
| 525 |
# as you can't do it on main thread |
| 526 |
worker_code = f""" |
| 527 |
import pytest |
| 528 |
import http.client |
| 529 |
import urllib3.contrib.emscripten.fetch |
| 530 |
await urllib3.contrib.emscripten.fetch.wait_for_streaming_ready() |
| 531 |
from urllib3.response import BaseHTTPResponse |
| 532 |
from urllib3.connection import HTTPConnection |
| 533 |
|
| 534 |
conn = HTTPConnection("{testserver_http.http_host}", {testserver_http.http_port}) |
| 535 |
with pytest.raises(http.client.HTTPException): |
| 536 |
conn.request("GET", "{bad_url}",preload_content=False) |
| 537 |
""" |
| 538 |
run_from_server.run_webworker(worker_code) |
| 539 |
|
| 540 |
|
| 541 |
@pytest.mark.webworkers |
| 542 |
def test_streaming_bad_method( |
| 543 |
testserver_http: PyodideServerInfo, |
| 544 |
run_from_server: ServerRunnerInfo, |
| 545 |
) -> None: |
| 546 |
# this should cause an error |
| 547 |
# because the protocol is bad |
| 548 |
bad_url = f"http://{testserver_http.http_host}:{testserver_http.http_port}/" |
| 549 |
# this must be in a webworker |
| 550 |
# as you can't do it on main thread |
| 551 |
worker_code = f""" |
| 552 |
import pytest |
| 553 |
import http.client |
| 554 |
|
| 555 |
import urllib3.contrib.emscripten.fetch |
| 556 |
await urllib3.contrib.emscripten.fetch.wait_for_streaming_ready() |
| 557 |
from urllib3.response import BaseHTTPResponse |
| 558 |
from urllib3.connection import HTTPConnection |
| 559 |
|
| 560 |
conn = HTTPConnection("{testserver_http.http_host}", {testserver_http.http_port}) |
| 561 |
with pytest.raises(http.client.HTTPException): |
| 562 |
# TRACE method should throw SecurityError in Javascript |
| 563 |
conn.request("TRACE", "{bad_url}",preload_content=False) |
| 564 |
""" |
| 565 |
run_from_server.run_webworker(worker_code) |
| 566 |
|
| 567 |
|
| 568 |
@pytest.mark.webworkers |
| 569 |
@pytest.mark.without_jspi |
| 570 |
def test_streaming_notready_warning( |
| 571 |
testserver_http: PyodideServerInfo, |
| 572 |
run_from_server: ServerRunnerInfo, |
| 573 |
) -> None: |
| 574 |
# test streaming download but don't wait for |
| 575 |
# worker to be ready - should fallback to non-streaming |
| 576 |
# and log a warning |
| 577 |
file_url = f"http://{testserver_http.http_host}:{testserver_http.http_port}/" |
| 578 |
worker_code = f""" |
| 579 |
import js |
| 580 |
import urllib3.contrib.emscripten.fetch |
| 581 |
from urllib3.response import BaseHTTPResponse |
| 582 |
from urllib3.connection import HTTPConnection |
| 583 |
|
| 584 |
urllib3.contrib.emscripten.fetch.streaming_ready = lambda :False |
| 585 |
log_msgs=[] |
| 586 |
old_log=js.console.warn |
| 587 |
def capture_log(*args): |
| 588 |
log_msgs.append(str(args)) |
| 589 |
old_log(*args) |
| 590 |
js.console.warn=capture_log |
| 591 |
|
| 592 |
conn = HTTPConnection("{testserver_http.http_host}", {testserver_http.http_port}) |
| 593 |
conn.request("GET", "{file_url}",preload_content=False) |
| 594 |
js.console.warn=old_log |
| 595 |
response = conn.getresponse() |
| 596 |
assert isinstance(response, BaseHTTPResponse) |
| 597 |
data=response.data.decode('utf-8') |
| 598 |
#assert len([x for x in log_msgs if x.find("Can't stream HTTP requests")!=-1])==1 |
| 599 |
#assert urllib3.contrib.emscripten.fetch._SHOWN_STREAMING_WARNING==True |
| 600 |
""" |
| 601 |
run_from_server.run_webworker(worker_code) |
| 602 |
|
| 603 |
|
| 604 |
def test_post_receive_json( |
| 605 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 606 |
) -> None: |
| 607 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 608 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 609 |
import json |
| 610 |
|
| 611 |
from urllib3.connection import HTTPConnection |
| 612 |
from urllib3.response import BaseHTTPResponse |
| 613 |
|
| 614 |
json_data = { |
| 615 |
"Bears": "like", |
| 616 |
"to": {"eat": "buns", "with": ["marmalade", "and custard"]}, |
| 617 |
} |
| 618 |
conn = HTTPConnection(host, port) |
| 619 |
conn.request( |
| 620 |
"POST", |
| 621 |
f"http://{host}:{port}/echo_json", |
| 622 |
body=json.dumps(json_data).encode("utf-8"), |
| 623 |
headers={"Content-type": "application/json"}, |
| 624 |
) |
| 625 |
response = conn.getresponse() |
| 626 |
assert isinstance(response, BaseHTTPResponse) |
| 627 |
data = response.json() |
| 628 |
assert data == json_data |
| 629 |
|
| 630 |
pyodide_test( |
| 631 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 632 |
) |
| 633 |
|
| 634 |
|
| 635 |
def test_upload( |
| 636 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 637 |
) -> None: |
| 638 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 639 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 640 |
from urllib3 import HTTPConnectionPool |
| 641 |
|
| 642 |
data = "I'm in ur multipart form-data, hazing a cheezburgr" |
| 643 |
fields: dict[str, _TYPE_FIELD_VALUE_TUPLE] = { |
| 644 |
"upload_param": "filefield", |
| 645 |
"upload_filename": "lolcat.txt", |
| 646 |
"filefield": ("lolcat.txt", data), |
| 647 |
} |
| 648 |
fields["upload_size"] = str(len(data)) |
| 649 |
with HTTPConnectionPool(host, port) as pool: |
| 650 |
r = pool.request("POST", "/upload", fields=fields) |
| 651 |
assert r.status == 200 |
| 652 |
|
| 653 |
pyodide_test( |
| 654 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 655 |
) |
| 656 |
|
| 657 |
|
| 658 |
@pytest.mark.without_jspi |
| 659 |
@pytest.mark.in_webbrowser |
| 660 |
def test_streaming_not_ready_in_browser( |
| 661 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 662 |
) -> None: |
| 663 |
# streaming ready should always be false |
| 664 |
# if we're in the main browser thread |
| 665 |
selenium_coverage.run_async( |
| 666 |
""" |
| 667 |
import urllib3.contrib.emscripten.fetch |
| 668 |
result=await urllib3.contrib.emscripten.fetch.wait_for_streaming_ready() |
| 669 |
assert(result is False) |
| 670 |
assert(urllib3.contrib.emscripten.fetch.streaming_ready() is None ) |
| 671 |
""" |
| 672 |
) |
| 673 |
|
| 674 |
|
| 675 |
def test_requests_with_micropip( |
| 676 |
selenium_coverage: typing.Any, |
| 677 |
testserver_http: PyodideServerInfo, |
| 678 |
) -> None: |
| 679 |
@run_in_pyodide(packages=["micropip"]) # type: ignore[untyped-decorator] |
| 680 |
async def test_fn( |
| 681 |
selenium_coverage: typing.Any, http_host: str, http_port: int, https_port: int |
| 682 |
) -> None: |
| 683 |
import micropip # type: ignore[import-not-found] |
| 684 |
|
| 685 |
await micropip.install("requests") |
| 686 |
import requests |
| 687 |
|
| 688 |
r = requests.get(f"http://{http_host}:{http_port}/") |
| 689 |
assert r.status_code == 200 |
| 690 |
assert r.text == "Dummy server!" |
| 691 |
json_data = {"woo": "yay"} |
| 692 |
# try posting some json with requests on https |
| 693 |
r = requests.post(f"https://{http_host}:{https_port}/echo_json", json=json_data) |
| 694 |
assert r.json() == json_data |
| 695 |
|
| 696 |
test_fn( |
| 697 |
selenium_coverage, |
| 698 |
testserver_http.http_host, |
| 699 |
testserver_http.http_port, |
| 700 |
testserver_http.https_port, |
| 701 |
) |
| 702 |
|
| 703 |
|
| 704 |
def test_open_close( |
| 705 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 706 |
) -> None: |
| 707 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 708 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 709 |
from http.client import ResponseNotReady |
| 710 |
|
| 711 |
import pytest |
| 712 |
|
| 713 |
from urllib3.connection import HTTPConnection |
| 714 |
|
| 715 |
conn = HTTPConnection(host, port) |
| 716 |
# initially connection should be closed |
| 717 |
assert conn.is_closed is True |
| 718 |
# connection should have no response |
| 719 |
with pytest.raises(ResponseNotReady): |
| 720 |
response = conn.getresponse() |
| 721 |
# now make the response |
| 722 |
conn.request("GET", f"http://{host}:{port}/") |
| 723 |
# we never connect to proxy (or if we do, browser handles it) |
| 724 |
assert conn.has_connected_to_proxy is False |
| 725 |
# now connection should be open |
| 726 |
assert conn.is_closed is False |
| 727 |
# and should have a response |
| 728 |
response = conn.getresponse() |
| 729 |
assert response is not None |
| 730 |
conn.close() |
| 731 |
# now it is closed |
| 732 |
assert conn.is_closed is True |
| 733 |
# closed connection shouldn't have any response |
| 734 |
with pytest.raises(ResponseNotReady): |
| 735 |
conn.getresponse() |
| 736 |
|
| 737 |
pyodide_test( |
| 738 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 739 |
) |
| 740 |
|
| 741 |
|
| 742 |
# check that various ways that the worker may be broken |
| 743 |
# throw exceptions nicely, by deliberately breaking things |
| 744 |
# this is for coverage |
| 745 |
@pytest.mark.webworkers |
| 746 |
@pytest.mark.without_jspi |
| 747 |
def test_break_worker_streaming( |
| 748 |
testserver_http: PyodideServerInfo, |
| 749 |
run_from_server: ServerRunnerInfo, |
| 750 |
) -> None: |
| 751 |
worker_code = f""" |
| 752 |
import pytest |
| 753 |
import urllib3.contrib.emscripten.fetch |
| 754 |
import js |
| 755 |
import http.client |
| 756 |
|
| 757 |
await urllib3.contrib.emscripten.fetch.wait_for_streaming_ready() |
| 758 |
from urllib3.exceptions import TimeoutError |
| 759 |
from urllib3.connection import HTTPConnection |
| 760 |
conn = HTTPConnection("{testserver_http.http_host}", {testserver_http.http_port},timeout=1.0) |
| 761 |
# make the fetch worker return a bad response by: |
| 762 |
# 1) Clearing the int buffer |
| 763 |
# in the receive stream |
| 764 |
with pytest.raises(http.client.HTTPException): |
| 765 |
conn.request("GET","/",preload_content=False) |
| 766 |
response = conn.getresponse() |
| 767 |
body_internal = response._response.body |
| 768 |
assert(body_internal.int_buffer!=None) |
| 769 |
body_internal.int_buffer=None |
| 770 |
data=response.read() |
| 771 |
# 2) Monkeypatch postMessage so that it just sets an |
| 772 |
# exception status |
| 773 |
old_pm= body_internal.worker.postMessage |
| 774 |
with pytest.raises(http.client.HTTPException): |
| 775 |
conn.request("GET","/",preload_content=False) |
| 776 |
response = conn.getresponse() |
| 777 |
# make posted messages set an exception |
| 778 |
body_internal = response._response.body |
| 779 |
def set_exception(*args): |
| 780 |
body_internal.worker.postMessage = old_pm |
| 781 |
body_internal.int_buffer[1]=4 |
| 782 |
body_internal.byte_buffer[0]=ord("W") |
| 783 |
body_internal.byte_buffer[1]=ord("O") |
| 784 |
body_internal.byte_buffer[2]=ord("O") |
| 785 |
body_internal.byte_buffer[3]=ord("!") |
| 786 |
body_internal.byte_buffer[4]=0 |
| 787 |
js.Atomics.store(body_internal.int_buffer, 0, -4) |
| 788 |
js.Atomics.notify(body_internal.int_buffer,0) |
| 789 |
body_internal.worker.postMessage = set_exception |
| 790 |
data=response.read() |
| 791 |
# monkeypatch so it returns an unknown value for the magic number on initial fetch call |
| 792 |
with pytest.raises(http.client.HTTPException): |
| 793 |
# make posted messages set an exception |
| 794 |
worker=urllib3.contrib.emscripten.fetch._fetcher.js_worker |
| 795 |
def set_exception(self,*args): |
| 796 |
array=js.Int32Array.new(args[0].buffer) |
| 797 |
array[0]=-1234 |
| 798 |
worker.postMessage=set_exception.__get__(worker,worker.__class__) |
| 799 |
conn.request("GET","/",preload_content=False) |
| 800 |
response = conn.getresponse() |
| 801 |
data=response.read() |
| 802 |
urllib3.contrib.emscripten.fetch._fetcher.js_worker.postMessage=old_pm |
| 803 |
# 3) Stopping the worker receiving any messages which should cause a timeout error |
| 804 |
# in the receive stream |
| 805 |
with pytest.raises(TimeoutError): |
| 806 |
conn.request("GET","/",preload_content=False) |
| 807 |
response = conn.getresponse() |
| 808 |
# make posted messages not be send |
| 809 |
body_internal = response._response.body |
| 810 |
def ignore_message(*args): |
| 811 |
pass |
| 812 |
old_pm= body_internal.worker.postMessage |
| 813 |
body_internal.worker.postMessage = ignore_message |
| 814 |
data=response.read() |
| 815 |
body_internal.worker.postMessage = old_pm |
| 816 |
""" |
| 817 |
run_from_server.run_webworker(worker_code) |
| 818 |
|
| 819 |
|
| 820 |
def test_response_init_length( |
| 821 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 822 |
) -> None: |
| 823 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 824 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 825 |
import pytest |
| 826 |
|
| 827 |
import urllib3.exceptions |
| 828 |
from urllib3.connection import HTTPConnection |
| 829 |
from urllib3.response import BaseHTTPResponse |
| 830 |
|
| 831 |
conn = HTTPConnection(host, port) |
| 832 |
conn.request("GET", f"http://{host}:{port}/") |
| 833 |
response = conn.getresponse() |
| 834 |
assert isinstance(response, BaseHTTPResponse) |
| 835 |
# head shouldn't have length |
| 836 |
length = response._init_length("HEAD") |
| 837 |
assert length == 0 |
| 838 |
# multiple inconsistent lengths - should raise invalid header |
| 839 |
with pytest.raises(urllib3.exceptions.InvalidHeader): |
| 840 |
response.headers["Content-Length"] = "4,5,6" |
| 841 |
length = response._init_length("GET") |
| 842 |
# non-numeric length - should return None |
| 843 |
response.headers["Content-Length"] = "anna" |
| 844 |
length = response._init_length("GET") |
| 845 |
assert length is None |
| 846 |
# numeric length - should return it |
| 847 |
response.headers["Content-Length"] = "54" |
| 848 |
length = response._init_length("GET") |
| 849 |
assert length == 54 |
| 850 |
# negative length - should return None |
| 851 |
response.headers["Content-Length"] = "-12" |
| 852 |
length = response._init_length("GET") |
| 853 |
assert length is None |
| 854 |
# none -> None |
| 855 |
del response.headers["Content-Length"] |
| 856 |
length = response._init_length("GET") |
| 857 |
assert length is None |
| 858 |
|
| 859 |
pyodide_test( |
| 860 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 861 |
) |
| 862 |
|
| 863 |
|
| 864 |
def test_response_close_connection( |
| 865 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 866 |
) -> None: |
| 867 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 868 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 869 |
from urllib3.connection import HTTPConnection |
| 870 |
from urllib3.response import BaseHTTPResponse |
| 871 |
|
| 872 |
conn = HTTPConnection(host, port) |
| 873 |
conn.request("GET", f"http://{host}:{port}/") |
| 874 |
response = conn.getresponse() |
| 875 |
assert isinstance(response, BaseHTTPResponse) |
| 876 |
response.close() |
| 877 |
assert conn.is_closed |
| 878 |
|
| 879 |
pyodide_test( |
| 880 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 881 |
) |
| 882 |
|
| 883 |
|
| 884 |
def test_read_chunked( |
| 885 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 886 |
) -> None: |
| 887 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 888 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 889 |
from urllib3.connection import HTTPConnection |
| 890 |
|
| 891 |
conn = HTTPConnection(host, port) |
| 892 |
conn.request("GET", f"http://{host}:{port}/mediumfile", preload_content=False) |
| 893 |
response = conn.getresponse() |
| 894 |
count = 0 |
| 895 |
for x in response.read_chunked(512): |
| 896 |
count += 1 |
| 897 |
if count < 10: |
| 898 |
assert len(x) == 512 |
| 899 |
|
| 900 |
pyodide_test( |
| 901 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 902 |
) |
| 903 |
|
| 904 |
|
| 905 |
def test_retries( |
| 906 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 907 |
) -> None: |
| 908 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 909 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 910 |
import pytest |
| 911 |
|
| 912 |
import urllib3 |
| 913 |
|
| 914 |
pool = urllib3.HTTPConnectionPool( |
| 915 |
host, |
| 916 |
port, |
| 917 |
maxsize=1, |
| 918 |
block=True, |
| 919 |
retries=urllib3.util.Retry(connect=5, read=5, redirect=5), |
| 920 |
) |
| 921 |
|
| 922 |
# monkeypatch connection class to count calls |
| 923 |
old_request = urllib3.connection.HTTPConnection.request |
| 924 |
count = 0 |
| 925 |
|
| 926 |
def count_calls(self, *args, **argv): # type: ignore[no-untyped-def] |
| 927 |
nonlocal count |
| 928 |
count += 1 |
| 929 |
return old_request(self, *args, **argv) |
| 930 |
|
| 931 |
urllib3.connection.HTTPConnection.request = count_calls # type: ignore[method-assign] |
| 932 |
with pytest.raises(urllib3.exceptions.MaxRetryError): |
| 933 |
pool.urlopen("GET", "/") |
| 934 |
# this should fail, but should have tried 6 times total |
| 935 |
assert count == 6 |
| 936 |
|
| 937 |
pyodide_test(selenium_coverage, testserver_http.http_host, find_unused_port()) |
| 938 |
|
| 939 |
|
| 940 |
def test_redirects( |
| 941 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 942 |
) -> None: |
| 943 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 944 |
def pyodide_test(selenium_coverage: typing.Any, host: str, port: int) -> None: |
| 945 |
from urllib3 import request |
| 946 |
|
| 947 |
redirect_url = f"http://{host}:{port}/redirect" |
| 948 |
response = request("GET", redirect_url) |
| 949 |
assert response.status == 200 |
| 950 |
|
| 951 |
pyodide_test( |
| 952 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 953 |
) |
| 954 |
|
| 955 |
|
| 956 |
@pytest.mark.with_jspi |
| 957 |
def test_disabled_redirects( |
| 958 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 959 |
) -> None: |
| 960 |
""" |
| 961 |
Test that urllib3 can control redirects in Node.js. |
| 962 |
""" |
| 963 |
|
| 964 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 965 |
def pyodide_test(selenium_coverage: typing.Any, host: str, port: int) -> None: |
| 966 |
import pytest |
| 967 |
|
| 968 |
from urllib3 import PoolManager, request |
| 969 |
from urllib3.contrib.emscripten.fetch import _is_node_js |
| 970 |
from urllib3.exceptions import MaxRetryError |
| 971 |
|
| 972 |
if not _is_node_js(): |
| 973 |
pytest.skip("urllib3 does not control redirects in browsers.") |
| 974 |
|
| 975 |
redirect_url = f"http://{host}:{port}/redirect" |
| 976 |
|
| 977 |
with PoolManager(retries=0) as http: |
| 978 |
with pytest.raises(MaxRetryError): |
| 979 |
http.request("GET", redirect_url) |
| 980 |
|
| 981 |
response = http.request("GET", redirect_url, redirect=False) |
| 982 |
assert response.status == 303 |
| 983 |
|
| 984 |
with PoolManager(retries=False) as http: |
| 985 |
response = http.request("GET", redirect_url) |
| 986 |
assert response.status == 303 |
| 987 |
|
| 988 |
with pytest.raises(MaxRetryError): |
| 989 |
request("GET", redirect_url, retries=0) |
| 990 |
|
| 991 |
response = request("GET", redirect_url, redirect=False) |
| 992 |
assert response.status == 303 |
| 993 |
|
| 994 |
response = request("GET", redirect_url, retries=0, redirect=False) |
| 995 |
assert response.status == 303 |
| 996 |
|
| 997 |
pyodide_test( |
| 998 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 999 |
) |
| 1000 |
|
| 1001 |
|
| 1002 |
def test_insecure_requests_warning( |
| 1003 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 1004 |
) -> None: |
| 1005 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 1006 |
def pyodide_test(selenium_coverage, host: str, port: int, https_port: int) -> None: # type: ignore[no-untyped-def] |
| 1007 |
import warnings |
| 1008 |
|
| 1009 |
import urllib3 |
| 1010 |
import urllib3.exceptions |
| 1011 |
|
| 1012 |
http = urllib3.PoolManager() |
| 1013 |
|
| 1014 |
with warnings.catch_warnings(record=True) as w: |
| 1015 |
http.request("GET", f"https://{host}:{https_port}") |
| 1016 |
assert len(w) == 0 |
| 1017 |
|
| 1018 |
pyodide_test( |
| 1019 |
selenium_coverage, |
| 1020 |
testserver_http.http_host, |
| 1021 |
testserver_http.http_port, |
| 1022 |
testserver_http.https_port, |
| 1023 |
) |
| 1024 |
|
| 1025 |
|
| 1026 |
@pytest.mark.webworkers |
| 1027 |
def test_has_jspi_worker( |
| 1028 |
testserver_http: PyodideServerInfo, |
| 1029 |
run_from_server: ServerRunnerInfo, |
| 1030 |
prefer_jspi: bool, |
| 1031 |
) -> None: |
| 1032 |
worker_code = f""" |
| 1033 |
import urllib3.contrib.emscripten.fetch |
| 1034 |
assert(urllib3.contrib.emscripten.fetch.has_jspi() == {prefer_jspi}) |
| 1035 |
""" |
| 1036 |
|
| 1037 |
run_from_server.run_webworker(worker_code) |
| 1038 |
|
| 1039 |
|
| 1040 |
def test_has_jspi( |
| 1041 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo, prefer_jspi: bool |
| 1042 |
) -> None: |
| 1043 |
@run_in_pyodide |
| 1044 |
def pyodide_test(selenium, prefer_jspi): # type: ignore[no-untyped-def] |
| 1045 |
import urllib3.contrib.emscripten.fetch |
| 1046 |
|
| 1047 |
assert urllib3.contrib.emscripten.fetch.has_jspi() == prefer_jspi |
| 1048 |
|
| 1049 |
pyodide_test(selenium_coverage, prefer_jspi) |
| 1050 |
|
| 1051 |
|
| 1052 |
@pytest.mark.with_jspi |
| 1053 |
def test_timeout_jspi( |
| 1054 |
selenium_coverage: typing.Any, |
| 1055 |
testserver_http: PyodideServerInfo, |
| 1056 |
) -> None: |
| 1057 |
@run_in_pyodide |
| 1058 |
def pyodide_test(selenium, host, port): # type: ignore[no-untyped-def] |
| 1059 |
import pytest |
| 1060 |
|
| 1061 |
import urllib3.contrib.emscripten.fetch |
| 1062 |
from urllib3.connection import HTTPConnection |
| 1063 |
from urllib3.exceptions import TimeoutError |
| 1064 |
|
| 1065 |
conn = HTTPConnection(host, port, timeout=0.1) |
| 1066 |
assert urllib3.contrib.emscripten.fetch.has_jspi() is True |
| 1067 |
with pytest.raises(TimeoutError): |
| 1068 |
conn.request("GET", "/slow") |
| 1069 |
conn.getresponse() |
| 1070 |
|
| 1071 |
pyodide_test( |
| 1072 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 1073 |
) |
| 1074 |
|
| 1075 |
|
| 1076 |
@pytest.mark.with_jspi |
| 1077 |
def test_streaming_jspi( |
| 1078 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 1079 |
) -> None: |
| 1080 |
bigfile_url = ( |
| 1081 |
f"http://{testserver_http.http_host}:{testserver_http.http_port}/dripfeed" |
| 1082 |
) |
| 1083 |
|
| 1084 |
@run_in_pyodide |
| 1085 |
def pyodide_test(selenium, host, port, bigfile_url): # type: ignore[no-untyped-def] |
| 1086 |
import time |
| 1087 |
|
| 1088 |
from urllib3.connection import HTTPConnection |
| 1089 |
from urllib3.response import BaseHTTPResponse |
| 1090 |
|
| 1091 |
conn = HTTPConnection(host, port) |
| 1092 |
start_time = time.time() |
| 1093 |
conn.request("GET", bigfile_url, preload_content=False) |
| 1094 |
response = conn.getresponse() |
| 1095 |
assert isinstance(response, BaseHTTPResponse) |
| 1096 |
# first data should be received before the timeout |
| 1097 |
# on the server |
| 1098 |
first_data = response.read(32768) |
| 1099 |
assert time.time() - start_time < 2 |
| 1100 |
all_data = first_data + response.read() |
| 1101 |
# make sure that the timeout on server side really happened |
| 1102 |
# by checking that it took greater than the timeout |
| 1103 |
assert time.time() - start_time > 2 |
| 1104 |
assert len(all_data.decode("utf-8")) == 17825792 |
| 1105 |
# ensure that the content is not cached |
| 1106 |
assert response._body is None |
| 1107 |
assert response.data == b"" |
| 1108 |
|
| 1109 |
pyodide_test( |
| 1110 |
selenium_coverage, |
| 1111 |
testserver_http.http_host, |
| 1112 |
testserver_http.http_port, |
| 1113 |
bigfile_url, |
| 1114 |
) |
| 1115 |
|
| 1116 |
|
| 1117 |
# another streaming test - uses chunked read |
| 1118 |
# and streaming to check that it works okay |
| 1119 |
# (see https://github.com/urllib3/urllib3/issues/3555 ) |
| 1120 |
@pytest.mark.with_jspi |
| 1121 |
def test_streaming2_jspi( |
| 1122 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 1123 |
) -> None: |
| 1124 |
bigfile_url = ( |
| 1125 |
f"http://{testserver_http.http_host}:{testserver_http.http_port}/dripfeed" |
| 1126 |
) |
| 1127 |
|
| 1128 |
@run_in_pyodide |
| 1129 |
def pyodide_test(selenium, host, port, bigfile_url): # type: ignore[no-untyped-def] |
| 1130 |
from urllib3.connection import HTTPConnection |
| 1131 |
from urllib3.response import BaseHTTPResponse |
| 1132 |
|
| 1133 |
conn = HTTPConnection(host, port) |
| 1134 |
conn.request("GET", bigfile_url, preload_content=False) |
| 1135 |
response = conn.getresponse() |
| 1136 |
assert isinstance(response, BaseHTTPResponse) |
| 1137 |
# get first data |
| 1138 |
all_data = response.read(32768) |
| 1139 |
# now get the rest in chunks |
| 1140 |
# to make sure that streaming works |
| 1141 |
# correctly even if the low level read doesn't |
| 1142 |
# always return a full buffer (which it doesn't) |
| 1143 |
while not response._response.body.closed: # type: ignore[attr-defined] |
| 1144 |
all_data += response.read(32768) |
| 1145 |
assert len(all_data.decode("utf-8")) == 17825792 |
| 1146 |
|
| 1147 |
pyodide_test( |
| 1148 |
selenium_coverage, |
| 1149 |
testserver_http.http_host, |
| 1150 |
testserver_http.http_port, |
| 1151 |
bigfile_url, |
| 1152 |
) |
| 1153 |
|
| 1154 |
|
| 1155 |
def test_cache_content_ignored_during_and_after_partial_read( |
| 1156 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 1157 |
) -> None: |
| 1158 |
@run_in_pyodide |
| 1159 |
def pyodide_test(selenium, host, port): # type: ignore[no-untyped-def] |
| 1160 |
from urllib3.connection import HTTPConnection |
| 1161 |
from urllib3.response import BaseHTTPResponse |
| 1162 |
|
| 1163 |
conn = HTTPConnection(host, port) |
| 1164 |
conn.request("GET", f"http://{host}:{port}/dripfeed", preload_content=False) |
| 1165 |
response = conn.getresponse() |
| 1166 |
assert isinstance(response, BaseHTTPResponse) |
| 1167 |
# read some of the data but not all of it |
| 1168 |
data = response.read(32768, cache_content=True) |
| 1169 |
assert len(data) == 32768 |
| 1170 |
# check that the cached content is empty |
| 1171 |
assert response._body is None |
| 1172 |
# ensure the rest of the data is not cached either |
| 1173 |
data += response.read(cache_content=True) |
| 1174 |
assert len(data) == 17825792 |
| 1175 |
assert response._body is None |
| 1176 |
assert response.data == b"" |
| 1177 |
|
| 1178 |
pyodide_test( |
| 1179 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 1180 |
) |
| 1181 |
|
| 1182 |
|
| 1183 |
@pytest.mark.node_without_jspi |
| 1184 |
def test_non_jspi_fail_in_node( |
| 1185 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 1186 |
) -> None: |
| 1187 |
if selenium_coverage.browser != "node": |
| 1188 |
pytest.skip("node only test") |
| 1189 |
|
| 1190 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 1191 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 1192 |
import http.client |
| 1193 |
|
| 1194 |
import pytest |
| 1195 |
|
| 1196 |
from urllib3.connection import HTTPConnection |
| 1197 |
|
| 1198 |
conn = HTTPConnection(host, port) |
| 1199 |
url = f"http://{host}:{port}/" |
| 1200 |
# check streaming and non-streaming requests both fail |
| 1201 |
with pytest.raises(http.client.HTTPException): |
| 1202 |
conn.request("GET", url) |
| 1203 |
conn.getresponse() |
| 1204 |
with pytest.raises(http.client.HTTPException): |
| 1205 |
conn.request("GET", url, preload_content=False) |
| 1206 |
conn.getresponse() |
| 1207 |
|
| 1208 |
pyodide_test( |
| 1209 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 1210 |
) |
| 1211 |
|
| 1212 |
|
| 1213 |
@pytest.mark.with_jspi |
| 1214 |
def test_jspi_fetch_error( |
| 1215 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 1216 |
) -> None: |
| 1217 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 1218 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 1219 |
import http.client |
| 1220 |
|
| 1221 |
import pytest |
| 1222 |
|
| 1223 |
from urllib3.connection import HTTPConnection |
| 1224 |
|
| 1225 |
conn = HTTPConnection(host, port) |
| 1226 |
url = f"sdfsdfsffhttp://{host}:{port}/" |
| 1227 |
with pytest.raises(http.client.HTTPException): |
| 1228 |
conn.request("GET", url) |
| 1229 |
conn.getresponse() |
| 1230 |
|
| 1231 |
pyodide_test( |
| 1232 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 1233 |
) |
| 1234 |
|
| 1235 |
|
| 1236 |
@pytest.mark.with_jspi |
| 1237 |
def test_jspi_readstream_errors( |
| 1238 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 1239 |
) -> None: |
| 1240 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 1241 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 1242 |
import io |
| 1243 |
from http.client import HTTPException |
| 1244 |
|
| 1245 |
import pytest |
| 1246 |
|
| 1247 |
import urllib3.contrib.emscripten.fetch |
| 1248 |
from urllib3.connection import HTTPConnection |
| 1249 |
from urllib3.exceptions import TimeoutError |
| 1250 |
|
| 1251 |
conn = HTTPConnection(host, port) |
| 1252 |
url = f"http://{host}:{port}/" |
| 1253 |
conn.request("GET", url, preload_content=False) |
| 1254 |
response = conn.getresponse() |
| 1255 |
assert isinstance(response._response.body, io.RawIOBase) # type: ignore[attr-defined] |
| 1256 |
old_run_sync = urllib3.contrib.emscripten.fetch._run_sync_with_timeout |
| 1257 |
with pytest.raises(TimeoutError): |
| 1258 |
|
| 1259 |
def raise_timeout(*args, **argv): # type: ignore[no-untyped-def] |
| 1260 |
raise urllib3.contrib.emscripten.fetch._TimeoutError() |
| 1261 |
|
| 1262 |
urllib3.contrib.emscripten.fetch._run_sync_with_timeout = raise_timeout |
| 1263 |
response.read() |
| 1264 |
urllib3.contrib.emscripten.fetch._run_sync_with_timeout = old_run_sync |
| 1265 |
conn.request("GET", url, preload_content=False) |
| 1266 |
response = conn.getresponse() |
| 1267 |
with pytest.raises(HTTPException): |
| 1268 |
|
| 1269 |
def raise_error(*args, **argv): # type: ignore[no-untyped-def] |
| 1270 |
raise urllib3.contrib.emscripten.fetch._RequestError() |
| 1271 |
|
| 1272 |
urllib3.contrib.emscripten.fetch._run_sync_with_timeout = raise_error |
| 1273 |
response.read() |
| 1274 |
|
| 1275 |
pyodide_test( |
| 1276 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 1277 |
) |
| 1278 |
|
| 1279 |
|
| 1280 |
@pytest.mark.with_jspi |
| 1281 |
def test_has_jspi_exception( |
| 1282 |
selenium_coverage: typing.Any, testserver_http: PyodideServerInfo |
| 1283 |
) -> None: |
| 1284 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 1285 |
def pyodide_test(selenium_coverage, host: str, port: int) -> None: # type: ignore[no-untyped-def] |
| 1286 |
from unittest.mock import patch |
| 1287 |
|
| 1288 |
import pyodide.ffi # type: ignore[import-not-found] |
| 1289 |
|
| 1290 |
if hasattr(pyodide.ffi, "can_run_sync"): |
| 1291 |
|
| 1292 |
@patch("pyodide.ffi.can_run_sync") |
| 1293 |
def should_return_false(func): # type: ignore[no-untyped-def] |
| 1294 |
func.return_value = (20, False) |
| 1295 |
func.side_effect = ImportError() |
| 1296 |
from urllib3.contrib.emscripten.fetch import has_jspi |
| 1297 |
|
| 1298 |
assert has_jspi() is False |
| 1299 |
|
| 1300 |
else: |
| 1301 |
from unittest.mock import patch |
| 1302 |
|
| 1303 |
@patch("pyodide_js._module") |
| 1304 |
def should_return_false(func): # type: ignore[no-untyped-def] |
| 1305 |
from urllib3.contrib.emscripten.fetch import has_jspi |
| 1306 |
|
| 1307 |
assert has_jspi() is False |
| 1308 |
|
| 1309 |
should_return_false() |
| 1310 |
|
| 1311 |
pyodide_test( |
| 1312 |
selenium_coverage, testserver_http.http_host, testserver_http.http_port |
| 1313 |
) |
| 1314 |
|
| 1315 |
|
| 1316 |
@run_in_pyodide # type: ignore[untyped-decorator] |
| 1317 |
def test_pool_no_port(selenium_coverage: typing.Any) -> None: |
| 1318 |
from unittest.mock import patch |
| 1319 |
|
| 1320 |
from urllib3 import HTTPConnectionPool |
| 1321 |
from urllib3.contrib.emscripten.request import EmscriptenRequest |
| 1322 |
from urllib3.contrib.emscripten.response import EmscriptenResponse |
| 1323 |
|
| 1324 |
def send_request(request: EmscriptenRequest) -> EmscriptenResponse: |
| 1325 |
assert request.url == "http://example.com/" |
| 1326 |
return EmscriptenResponse( |
| 1327 |
status_code=200, headers={}, body=b"", request=request |
| 1328 |
) |
| 1329 |
|
| 1330 |
with patch("urllib3.contrib.emscripten.connection.send_request", new=send_request): |
| 1331 |
pool = HTTPConnectionPool("example.com", maxsize=10, block=True) |
| 1332 |
|
| 1333 |
pool.request("GET", "/") |