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_responses.py · 5.6 KB · 167 lines
| 1 | """Tests for dbbasic_web.responses module""" |
| 2 | import pytest |
| 3 | from dbbasic_web.responses import ( |
| 4 | html, json, json_error, text, redirect, sse_event, now_iso |
| 5 | ) |
| 6 | |
| 7 | |
| 8 | class TestHtmlResponse: |
| 9 | """Test html() response helper""" |
| 10 | |
| 11 | def test_html_basic(self): |
| 12 | """html() should return correct tuple""" |
| 13 | status, headers, body = html("<h1>Hello</h1>") |
| 14 | assert status == 200 |
| 15 | assert ("content-type", "text/html; charset=utf-8") in headers |
| 16 | assert body == [b"<h1>Hello</h1>"] |
| 17 | |
| 18 | def test_html_custom_status(self): |
| 19 | """html() should accept custom status""" |
| 20 | status, headers, body = html("<h1>Not Found</h1>", status=404) |
| 21 | assert status == 404 |
| 22 | |
| 23 | def test_html_additional_headers(self): |
| 24 | """html() should merge additional headers""" |
| 25 | status, headers, body = html( |
| 26 | "<h1>Hello</h1>", |
| 27 | headers=[("x-custom", "value")] |
| 28 | ) |
| 29 | assert ("content-type", "text/html; charset=utf-8") in headers |
| 30 | assert ("x-custom", "value") in headers |
| 31 | |
| 32 | def test_html_encodes_utf8(self): |
| 33 | """html() should encode unicode to UTF-8""" |
| 34 | status, headers, body = html("<p>Caf\u00e9</p>") |
| 35 | assert body == ["<p>Caf\u00e9</p>".encode("utf-8")] |
| 36 | |
| 37 | |
| 38 | class TestJsonResponse: |
| 39 | """Test json() response helper""" |
| 40 | |
| 41 | def test_json_with_string(self): |
| 42 | """json() should accept string""" |
| 43 | status, headers, body = json('{"key": "value"}') |
| 44 | assert status == 200 |
| 45 | assert ("content-type", "application/json; charset=utf-8") in headers |
| 46 | assert body == [b'{"key": "value"}'] |
| 47 | |
| 48 | def test_json_with_bytes(self): |
| 49 | """json() should accept bytes""" |
| 50 | status, headers, body = json(b'{"key": "value"}') |
| 51 | assert body == [b'{"key": "value"}'] |
| 52 | |
| 53 | def test_json_custom_status(self): |
| 54 | """json() should accept custom status""" |
| 55 | status, headers, body = json('{}', status=201) |
| 56 | assert status == 201 |
| 57 | |
| 58 | def test_json_additional_headers(self): |
| 59 | """json() should merge additional headers""" |
| 60 | status, headers, body = json( |
| 61 | '{}', |
| 62 | headers=[("x-request-id", "123")] |
| 63 | ) |
| 64 | assert ("x-request-id", "123") in headers |
| 65 | |
| 66 | |
| 67 | class TestJsonErrorResponse: |
| 68 | """Test json_error() convenience helper""" |
| 69 | |
| 70 | def test_json_error_default_status(self): |
| 71 | """json_error() should default to 404""" |
| 72 | status, headers, body = json_error("Not found") |
| 73 | assert status == 404 |
| 74 | assert ("content-type", "application/json; charset=utf-8") in headers |
| 75 | |
| 76 | def test_json_error_custom_status(self): |
| 77 | """json_error() should accept custom status""" |
| 78 | status, headers, body = json_error("Bad request", status=400) |
| 79 | assert status == 400 |
| 80 | |
| 81 | def test_json_error_body_format(self): |
| 82 | """json_error() should format message correctly""" |
| 83 | import json as json_lib |
| 84 | status, headers, body = json_error("Something went wrong") |
| 85 | parsed = json_lib.loads(body[0]) |
| 86 | assert parsed == {"error": "Something went wrong"} |
| 87 | |
| 88 | |
| 89 | class TestTextResponse: |
| 90 | """Test text() response helper""" |
| 91 | |
| 92 | def test_text_basic(self): |
| 93 | """text() should return correct tuple""" |
| 94 | status, headers, body = text("Hello, World!") |
| 95 | assert status == 200 |
| 96 | assert ("content-type", "text/plain; charset=utf-8") in headers |
| 97 | assert body == [b"Hello, World!"] |
| 98 | |
| 99 | def test_text_custom_status(self): |
| 100 | """text() should accept custom status""" |
| 101 | status, headers, body = text("Error", status=500) |
| 102 | assert status == 500 |
| 103 | |
| 104 | |
| 105 | class TestRedirectResponse: |
| 106 | """Test redirect() response helper""" |
| 107 | |
| 108 | def test_redirect_default_status(self): |
| 109 | """redirect() should default to 302""" |
| 110 | status, headers, body = redirect("/login") |
| 111 | assert status == 302 |
| 112 | assert ("location", "/login") in headers |
| 113 | assert body == [b""] |
| 114 | |
| 115 | def test_redirect_custom_status(self): |
| 116 | """redirect() should accept custom status""" |
| 117 | status, headers, body = redirect("/new-page", status=301) |
| 118 | assert status == 301 |
| 119 | |
| 120 | |
| 121 | class TestSseEvent: |
| 122 | """Test sse_event() helper""" |
| 123 | |
| 124 | def test_sse_event_data_only(self): |
| 125 | """sse_event() should format data-only event""" |
| 126 | result = sse_event("hello") |
| 127 | assert result == b"data: hello\n\n" |
| 128 | |
| 129 | def test_sse_event_with_event_type(self): |
| 130 | """sse_event() should include event type""" |
| 131 | result = sse_event("hello", event="message") |
| 132 | assert b"event: message\n" in result |
| 133 | assert b"data: hello\n" in result |
| 134 | |
| 135 | def test_sse_event_with_id(self): |
| 136 | """sse_event() should include id""" |
| 137 | result = sse_event("hello", id="123") |
| 138 | assert b"id: 123\n" in result |
| 139 | assert b"data: hello\n" in result |
| 140 | |
| 141 | def test_sse_event_multiline_data(self): |
| 142 | """sse_event() should handle multiline data""" |
| 143 | result = sse_event("line1\nline2\nline3") |
| 144 | assert b"data: line1\n" in result |
| 145 | assert b"data: line2\n" in result |
| 146 | assert b"data: line3\n" in result |
| 147 | |
| 148 | def test_sse_event_empty_data(self): |
| 149 | """sse_event() should handle empty data""" |
| 150 | result = sse_event("") |
| 151 | assert b"data: \n" in result |
| 152 | |
| 153 | |
| 154 | class TestNowIso: |
| 155 | """Test now_iso() helper""" |
| 156 | |
| 157 | def test_now_iso_format(self): |
| 158 | """now_iso() should return ISO format with Z suffix""" |
| 159 | result = now_iso() |
| 160 | assert result.endswith("Z") |
| 161 | # Should be parseable datetime format |
| 162 | assert "T" in result |
| 163 | # Basic format: YYYY-MM-DDTHH:MM:SS.microsZ |
| 164 | parts = result.rstrip("Z").split("T") |
| 165 | assert len(parts) == 2 |
| 166 | date_part = parts[0] |
| 167 | assert len(date_part.split("-")) == 3 # YYYY-MM-DD |