|
| 1 | +from starlette.testclient import TestClient |
| 2 | +from htag import App |
| 3 | +from htag.web import WebApp |
| 4 | + |
| 5 | +class MyApp(App): |
| 6 | + def init(self): |
| 7 | + self += "Hello World" |
| 8 | + |
| 9 | +def test_htag_mode_default(): |
| 10 | + """Verify that by default, no protocol mocking is injected.""" |
| 11 | + webapp = WebApp(MyApp) |
| 12 | + client = TestClient(webapp.app) |
| 13 | + response = client.get("/") |
| 14 | + assert response.status_code == 200 |
| 15 | + assert "window.WebSocket=window.EventSource" not in response.text |
| 16 | + assert "Hello World" in response.text |
| 17 | + |
| 18 | +def test_htag_mode_http(): |
| 19 | + """Verify protocol mocking for 'http' mode.""" |
| 20 | + webapp = WebApp(MyApp) |
| 21 | + client = TestClient(webapp.app) |
| 22 | + response = client.get("/", cookies={"htag_mode": "http"}) |
| 23 | + assert response.status_code == 200 |
| 24 | + # 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 |
| 27 | + |
| 28 | +def test_htag_mode_sse(): |
| 29 | + """Verify protocol mocking for 'sse' mode.""" |
| 30 | + webapp = WebApp(MyApp) |
| 31 | + client = TestClient(webapp.app) |
| 32 | + response = client.get("/", cookies={"htag_mode": "sse"}) |
| 33 | + assert response.status_code == 200 |
| 34 | + # 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 |
| 38 | + |
| 39 | +def test_htag_mode_invalid(): |
| 40 | + """Verify that invalid cookie values are ignored.""" |
| 41 | + webapp = WebApp(MyApp) |
| 42 | + client = TestClient(webapp.app) |
| 43 | + response = client.get("/", cookies={"htag_mode": "invalid"}) |
| 44 | + assert response.status_code == 200 |
| 45 | + assert "window.WebSocket=function()" not in response.text |
| 46 | + assert "window.EventSource=function()" not in response.text |
0 commit comments