askrobots / dbbasic-web
Public
A Unix-style Python web framework: filesystem routing, flat files, messages and cron, with CGI's simplicity at modern speed.
Dan Quellhorst
Replace multipart parser with stdlib-only implementation
eb957a2
9 months, 2 weeks ago
History
test_router.py · 6.6 KB · 173 lines
| 1 | """Tests for dbbasic_web.router module""" |
| 2 | import pytest |
| 3 | from dbbasic_web.router import ( |
| 4 | parse_query_string, parse_form_data, parse_cookies |
| 5 | ) |
| 6 | |
| 7 | |
| 8 | class TestParseQueryString: |
| 9 | """Test parse_query_string() function""" |
| 10 | |
| 11 | def test_empty_string(self): |
| 12 | """Empty string should return empty dict""" |
| 13 | assert parse_query_string("") == {} |
| 14 | |
| 15 | def test_single_param(self): |
| 16 | """Single param should parse correctly""" |
| 17 | assert parse_query_string("page=1") == {"page": "1"} |
| 18 | |
| 19 | def test_multiple_params(self): |
| 20 | """Multiple params should parse correctly""" |
| 21 | result = parse_query_string("page=1&limit=10&sort=name") |
| 22 | assert result == {"page": "1", "limit": "10", "sort": "name"} |
| 23 | |
| 24 | def test_url_encoded_values(self): |
| 25 | """URL encoded values should be decoded""" |
| 26 | result = parse_query_string("name=John%20Doe&email=john%40example.com") |
| 27 | assert result == {"name": "John Doe", "email": "john@example.com"} |
| 28 | |
| 29 | def test_empty_value(self): |
| 30 | """Empty value is discarded by parse_qsl default behavior""" |
| 31 | result = parse_query_string("key=") |
| 32 | # Note: urllib.parse.parse_qsl discards empty values by default |
| 33 | assert result == {} |
| 34 | |
| 35 | |
| 36 | class TestParseFormData: |
| 37 | """Test parse_form_data() function""" |
| 38 | |
| 39 | def test_empty_body(self): |
| 40 | """Empty body should return empty dicts""" |
| 41 | form, files = parse_form_data(b"", "application/x-www-form-urlencoded") |
| 42 | assert form == {} |
| 43 | assert files == {} |
| 44 | |
| 45 | def test_urlencoded_form(self): |
| 46 | """URL encoded form should parse correctly""" |
| 47 | body = b"username=alice&password=secret" |
| 48 | content_type = "application/x-www-form-urlencoded" |
| 49 | form, files = parse_form_data(body, content_type) |
| 50 | assert form == {"username": "alice", "password": "secret"} |
| 51 | assert files == {} |
| 52 | |
| 53 | def test_urlencoded_with_charset(self): |
| 54 | """URL encoded with charset should parse correctly""" |
| 55 | body = b"name=test" |
| 56 | content_type = "application/x-www-form-urlencoded; charset=utf-8" |
| 57 | form, files = parse_form_data(body, content_type) |
| 58 | assert form == {"name": "test"} |
| 59 | assert files == {} |
| 60 | |
| 61 | def test_unknown_content_type(self): |
| 62 | """Unknown content type should return empty dicts""" |
| 63 | form, files = parse_form_data(b"some data", "text/plain") |
| 64 | assert form == {} |
| 65 | assert files == {} |
| 66 | |
| 67 | def test_multipart_form_field(self): |
| 68 | """Multipart form should parse text fields""" |
| 69 | boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW" |
| 70 | body = ( |
| 71 | b"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n" |
| 72 | b'Content-Disposition: form-data; name="username"\r\n' |
| 73 | b"\r\n" |
| 74 | b"alice\r\n" |
| 75 | b"------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n" |
| 76 | ) |
| 77 | content_type = f"multipart/form-data; boundary={boundary}" |
| 78 | form, files = parse_form_data(body, content_type) |
| 79 | assert form == {"username": "alice"} |
| 80 | assert files == {} |
| 81 | |
| 82 | def test_multipart_file_upload(self): |
| 83 | """Multipart form should parse file uploads""" |
| 84 | boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW" |
| 85 | file_content = b"Hello, this is file content!" |
| 86 | body = ( |
| 87 | b"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n" |
| 88 | b'Content-Disposition: form-data; name="document"; filename="test.txt"\r\n' |
| 89 | b"Content-Type: text/plain\r\n" |
| 90 | b"\r\n" |
| 91 | + file_content + |
| 92 | b"\r\n" |
| 93 | b"------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n" |
| 94 | ) |
| 95 | content_type = f"multipart/form-data; boundary={boundary}" |
| 96 | form, files = parse_form_data(body, content_type) |
| 97 | assert form == {} |
| 98 | assert "document" in files |
| 99 | assert files["document"]["filename"] == "test.txt" |
| 100 | assert files["document"]["content"] == file_content |
| 101 | |
| 102 | def test_multipart_mixed_fields_and_files(self): |
| 103 | """Multipart form should handle both fields and files""" |
| 104 | boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW" |
| 105 | file_content = b"\x89PNG\r\n\x1a\n" # PNG magic bytes |
| 106 | body = ( |
| 107 | b"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n" |
| 108 | b'Content-Disposition: form-data; name="title"\r\n' |
| 109 | b"\r\n" |
| 110 | b"My Photo\r\n" |
| 111 | b"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n" |
| 112 | b'Content-Disposition: form-data; name="photo"; filename="image.png"\r\n' |
| 113 | b"Content-Type: image/png\r\n" |
| 114 | b"\r\n" |
| 115 | + file_content + |
| 116 | b"\r\n" |
| 117 | b"------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n" |
| 118 | ) |
| 119 | content_type = f"multipart/form-data; boundary={boundary}" |
| 120 | form, files = parse_form_data(body, content_type) |
| 121 | assert form == {"title": "My Photo"} |
| 122 | assert "photo" in files |
| 123 | assert files["photo"]["filename"] == "image.png" |
| 124 | assert files["photo"]["content"] == file_content |
| 125 | |
| 126 | |
| 127 | class TestParseCookies: |
| 128 | """Test parse_cookies() function""" |
| 129 | |
| 130 | def test_empty_headers(self): |
| 131 | """Empty headers should return empty dict""" |
| 132 | assert parse_cookies([]) == {} |
| 133 | |
| 134 | def test_single_cookie(self): |
| 135 | """Single cookie should parse correctly""" |
| 136 | headers = [(b"cookie", b"session=abc123")] |
| 137 | result = parse_cookies(headers) |
| 138 | assert result == {"session": "abc123"} |
| 139 | |
| 140 | def test_multiple_cookies(self): |
| 141 | """Multiple cookies should parse correctly""" |
| 142 | headers = [(b"cookie", b"session=abc123; user=alice; theme=dark")] |
| 143 | result = parse_cookies(headers) |
| 144 | assert result == {"session": "abc123", "user": "alice", "theme": "dark"} |
| 145 | |
| 146 | def test_cookie_with_spaces(self): |
| 147 | """Cookies with spaces should be trimmed""" |
| 148 | headers = [(b"cookie", b"session = abc123 ; user = alice")] |
| 149 | result = parse_cookies(headers) |
| 150 | assert result == {"session": "abc123", "user": "alice"} |
| 151 | |
| 152 | def test_string_header_values(self): |
| 153 | """String header values should work""" |
| 154 | headers = [(b"cookie", "session=abc123")] |
| 155 | result = parse_cookies(headers) |
| 156 | assert result == {"session": "abc123"} |
| 157 | |
| 158 | def test_ignores_non_cookie_headers(self): |
| 159 | """Non-cookie headers should be ignored""" |
| 160 | headers = [ |
| 161 | (b"content-type", b"application/json"), |
| 162 | (b"cookie", b"session=abc123"), |
| 163 | (b"accept", b"*/*"), |
| 164 | ] |
| 165 | result = parse_cookies(headers) |
| 166 | assert result == {"session": "abc123"} |
| 167 | |
| 168 | def test_cookie_without_value(self): |
| 169 | """Cookie without = should be ignored""" |
| 170 | headers = [(b"cookie", b"invalid; session=abc123")] |
| 171 | result = parse_cookies(headers) |
| 172 | # 'invalid' has no =, so only 'session' should be parsed |
| 173 | assert result == {"session": "abc123"} |