PyWebBrowser/tests/url_test.py

231 lines
4.8 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,
),
(
"data:text,This is some text",
"data",
"",
-1,
"text,This is some text",
"",
"",
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(
"url_string,media_type,media_encoding,data,parse_success",
[
(
"data:text,This is some text",
"text",
"",
"This is some text",
True,
),
(
"data:text/plain,This is some text",
"text/plain",
"",
"This is some text",
True,
),
(
"data:text/plain;base64,This is some text",
"text/plain",
"base64",
"This is some text",
True,
),
],
)
def test_data_url_parsing(url_string, media_type, media_encoding, data, parse_success):
url, success = parse_url(url_string)
assert url.media_type == media_type
assert url.media_encoding == media_encoding
assert url.data == data
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"