Skip to content

Commit 67dfc1c

Browse files
committed
v2.0.22
refactor: replace legacy WebSocket/EventSource mocks with a robust class-based implementation in runner and update tests
1 parent 27be2f9 commit 67dfc1c

4 files changed

Lines changed: 14 additions & 11 deletions

File tree

‎htag/runner.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,13 @@ def _render_page(self) -> str:
184184
request = current_request.get()
185185
if request and hasattr(request, "cookies"):
186186
mode = request.cookies.get("htag_mode")
187-
if mode == "http":
188-
protocol_patch = "window.WebSocket=window.EventSource=function(){var self=this;setTimeout(function(){if(self.onerror)self.onerror(new Error('Forced HTTP mode'))},0)};"
189-
elif mode == "sse":
190-
protocol_patch = "window.WebSocket=function(){var self=this;setTimeout(function(){if(self.onerror)self.onerror(new Error('Forced SSE mode'))},0)};"
187+
if mode in ("http", "sse"):
188+
# Robust mock for WebSocket (and EventSource for http mode)
189+
mock_js = "class{constructor(){Object.assign(this,{readyState:3,close:()=>{},send:()=>{}});setTimeout(()=>this.onerror&&this.onerror(new Event('error')),0)}}; ['CONNECTING','OPEN','CLOSING','CLOSED'].forEach((name,i)=>{if(window.WebSocket)window.WebSocket[name]=i; if(window.EventSource)window.EventSource[name]=i;});"
190+
if mode == "http":
191+
protocol_patch = f"window.WebSocket=window.EventSource={mock_js}"
192+
else:
193+
protocol_patch = f"window.WebSocket={mock_js}"
191194

192195
html_content = f"""
193196
<!DOCTYPE html>

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ include-package-data = true
99

1010
[project]
1111
name = "htag"
12-
version = "2.0.21"
12+
version = "2.0.22"
1313
description = "Python3 GUI toolkit for building 'beautiful' applications for mobile, web, and desktop from a single codebase"
1414
readme = "README.md"
1515
requires-python = ">=3.10"

‎tests/test_htag_mode.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def test_htag_mode_http():
2222
response = client.get("/", cookies={"htag_mode": "http"})
2323
assert response.status_code == 200
2424
# Both WebSocket and EventSource should be mocked
25-
assert "window.WebSocket=window.EventSource=function()" in response.text
26-
assert "Forced HTTP mode" in response.text
25+
assert "window.WebSocket=window.EventSource=class{" in response.text
26+
assert "new Event('error')" in response.text
2727

2828
def test_htag_mode_sse():
2929
"""Verify protocol mocking for 'sse' mode."""
@@ -32,9 +32,9 @@ def test_htag_mode_sse():
3232
response = client.get("/", cookies={"htag_mode": "sse"})
3333
assert response.status_code == 200
3434
# Only WebSocket should be mocked
35-
assert "window.WebSocket=function()" in response.text
36-
assert "window.EventSource=function()" not in response.text
37-
assert "Forced SSE mode" in response.text
35+
assert "window.WebSocket=class{" in response.text
36+
assert "window.EventSource=class{" not in response.text
37+
assert "new Event('error')" in response.text
3838

3939
def test_htag_mode_invalid():
4040
"""Verify that invalid cookie values are ignored."""

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)