PyWebBrowser/tests/url_test.py
Matthew Welch 015e46134e add support for file scheme
split url parsing into separate function
2024-12-06 11:31:09 -08:00

187 lines
3.7 KiB
Python

import pytest
from browser import URL
from browser import parse_url
from browser import Request
@pytest.mark.parametrize(
"url_string,scheme,host,port,path,query,fragment,parse_success",
[
(
"http://example.com",
"http",
"example.com",
80,
"/",
"",
"",
True,
),
(
"http://example.com/",
"http",
"example.com",
80,
"/",
"",
"",
True,
),
(
"https://example.com/",
"https",
"example.com",
443,
"/",
"",
"",
True,
),
(
"http://example.com:5000/",
"http",
"example.com",
5000,
"/",
"",
"",
True,
),
(
"http://example.com:5000/test/example",
"http",
"example.com",
5000,
"/test/example",
"",
"",
True,
),
(
"https://example.com:5000/test/example",
"https",
"example.com",
5000,
"/test/example",
"",
"",
True,
),
(
"file:///test.html",
"file",
"",
-1,
"/test.html",
"",
"",
True,
),
(
"file://file_host/test.html",
"file",
"file_host",
-1,
"/test.html",
"",
"",
True,
),
(
"file:///c:/test.txt",
"file",
"",
-1,
"c:/test.txt",
"",
"",
True,
),
(
r"file:///c:\test.txt",
"file",
"",
-1,
r"c:\test.txt",
"",
"",
True,
),
(
"file://file_host/",
"file",
"file_host",
-1,
"/",
"",
"",
False,
),
(
"htp://example.com/",
"htp",
"",
-1,
"",
"",
"",
False,
),
(
"file:test.txt",
"file",
"",
-1,
"test.txt",
"",
"",
True,
),
(
"file:/test.txt",
"file",
"",
-1,
"/test.txt",
"",
"",
True,
),
],
)
def test_url_parsing(url_string, scheme, host, port, path, query, fragment, parse_success):
url, success = parse_url(url_string)
assert url.scheme == scheme
assert url.host == host
assert url.port == port
assert url.path == path
assert url.query == query
assert url.fragment == fragment
assert success == parse_success
@pytest.mark.parametrize(
"http_server,url_string",
[
(80, "http://127.0.0.1/"),
(5000, "http://127.0.0.1:5000/"),
],
indirect=["http_server"],
)
def test_http_request(http_server, url_string):
url = URL(url_string)
assert Request(url).send_request() == "test"
@pytest.mark.parametrize(
"https_server,url_string",
[
(443, "https://127.0.0.1/"),
(5000, "https://127.0.0.1:5000/"),
],
indirect=["https_server"],
)
def test_https_request(https_server, url_string):
url = URL(url_string)
assert Request(url).send_request() == "test"