add support for base64 encoding with the data scheme

This commit is contained in:
Matthew Welch 2025-02-09 14:56:38 -08:00
parent 5f4826cc73
commit 9e0192fe52
2 changed files with 10 additions and 2 deletions

View File

@ -1,3 +1,4 @@
import base64
import os.path
import socket
import ssl
@ -129,6 +130,8 @@ class Request:
return f.read()
def data_request(self):
if self.url.media_encoding == "base64":
return base64.b64decode(self.url.data)
return self.url.data

View File

@ -1,3 +1,5 @@
import base64
import pytest
from browser import URL
@ -188,7 +190,7 @@ def test_url_parsing(url_string, scheme, host, port, path, query, fragment, pars
True,
),
(
"data:text/plain;base64,This is some text",
"data:text/plain;base64,VGhpcyBpcyBzb21lIHRleHQ=",
"text/plain",
"base64",
"This is some text",
@ -200,7 +202,10 @@ def test_data_url_parsing(url_string, media_type, media_encoding, data, parse_su
url, success = parse_url(url_string)
assert url.media_type == media_type
assert url.media_encoding == media_encoding
assert url.data == data
if media_encoding == "base64":
assert base64.b64decode(url.data).decode("utf8") == data;
else:
assert url.data == data
assert success == parse_success