|
| 1 | +import pytest |
| 2 | + |
| 3 | +from ld_eventsource import * |
| 4 | +from ld_eventsource.actions import * |
| 5 | +from ld_eventsource.config import * |
| 6 | +from ld_eventsource.errors import * |
| 7 | +from ld_eventsource.testing.helpers import * |
| 8 | + |
| 9 | + |
| 10 | +def test_start_action_with_no_headers(): |
| 11 | + """Test that Start action can be created without headers""" |
| 12 | + start = Start() |
| 13 | + assert start.headers is None |
| 14 | + |
| 15 | + |
| 16 | +def test_start_action_with_headers(): |
| 17 | + """Test that Start action can be created with headers""" |
| 18 | + headers = {'Content-Type': 'text/event-stream', 'X-Custom': 'value'} |
| 19 | + start = Start(headers) |
| 20 | + assert start.headers == headers |
| 21 | + |
| 22 | + |
| 23 | +def test_headers_exposed_in_start_action(): |
| 24 | + """Test that headers from connection are exposed in Start action""" |
| 25 | + headers = {'Content-Type': 'text/event-stream', 'X-Test-Header': 'test-value'} |
| 26 | + mock = MockConnectStrategy( |
| 27 | + RespondWithData("event: test\ndata: data1\n\n", headers=headers) |
| 28 | + ) |
| 29 | + |
| 30 | + with SSEClient(connect=mock) as client: |
| 31 | + all_items = list(client.all) |
| 32 | + |
| 33 | + # First item should be Start with headers |
| 34 | + assert isinstance(all_items[0], Start) |
| 35 | + assert all_items[0].headers == headers |
| 36 | + |
| 37 | + # Second item should be the event |
| 38 | + assert isinstance(all_items[1], Event) |
| 39 | + assert all_items[1].event == 'test' |
| 40 | + |
| 41 | + # Third item should be Fault (end of stream) |
| 42 | + assert isinstance(all_items[2], Fault) |
| 43 | + assert all_items[2].error is None |
| 44 | + |
| 45 | + |
| 46 | +def test_headers_not_visible_in_events_iterator(): |
| 47 | + """Test that headers are only visible when using .all, not .events""" |
| 48 | + headers = {'X-Custom': 'value'} |
| 49 | + mock = MockConnectStrategy( |
| 50 | + RespondWithData("event: test\ndata: data1\n\n", headers=headers) |
| 51 | + ) |
| 52 | + |
| 53 | + with SSEClient(connect=mock) as client: |
| 54 | + events = list(client.events) |
| 55 | + |
| 56 | + # Should only get the event, no Start action |
| 57 | + assert len(events) == 1 |
| 58 | + assert isinstance(events[0], Event) |
| 59 | + assert events[0].event == 'test' |
| 60 | + |
| 61 | + |
| 62 | +def test_no_headers_when_not_provided(): |
| 63 | + """Test that Start action has None headers when connection doesn't provide them""" |
| 64 | + mock = MockConnectStrategy( |
| 65 | + RespondWithData("event: test\ndata: data1\n\n") |
| 66 | + ) |
| 67 | + |
| 68 | + with SSEClient(connect=mock) as client: |
| 69 | + all_items = list(client.all) |
| 70 | + |
| 71 | + # First item should be Start with no headers |
| 72 | + assert isinstance(all_items[0], Start) |
| 73 | + assert all_items[0].headers is None |
| 74 | + |
| 75 | + |
| 76 | +def test_different_headers_on_reconnection(): |
| 77 | + """Test that reconnection yields new Start with potentially different headers""" |
| 78 | + headers1 = {'X-Connection': 'first'} |
| 79 | + headers2 = {'X-Connection': 'second'} |
| 80 | + |
| 81 | + mock = MockConnectStrategy( |
| 82 | + RespondWithData("event: test1\ndata: data1\n\n", headers=headers1), |
| 83 | + RespondWithData("event: test2\ndata: data2\n\n", headers=headers2) |
| 84 | + ) |
| 85 | + |
| 86 | + with SSEClient( |
| 87 | + connect=mock, |
| 88 | + error_strategy=ErrorStrategy.from_lambda(lambda _: (ErrorStrategy.CONTINUE, None)), |
| 89 | + retry_delay_strategy=no_delay() |
| 90 | + ) as client: |
| 91 | + items = [] |
| 92 | + for item in client.all: |
| 93 | + items.append(item) |
| 94 | + # Stop after we get the second Start (from reconnection) |
| 95 | + if isinstance(item, Start) and len([i for i in items if isinstance(i, Start)]) == 2: |
| 96 | + break |
| 97 | + |
| 98 | + # Find all Start actions |
| 99 | + starts = [item for item in items if isinstance(item, Start)] |
| 100 | + assert len(starts) >= 2 |
| 101 | + |
| 102 | + # First connection should have first headers |
| 103 | + assert starts[0].headers == headers1 |
| 104 | + |
| 105 | + # Second connection should have second headers |
| 106 | + assert starts[1].headers == headers2 |
| 107 | + |
| 108 | + |
| 109 | +def test_headers_on_retry_after_error(): |
| 110 | + """Test that headers are provided on successful retry after an error""" |
| 111 | + error = HTTPStatusError(503) |
| 112 | + headers = {'X-Retry': 'success'} |
| 113 | + |
| 114 | + mock = MockConnectStrategy( |
| 115 | + RejectConnection(error), |
| 116 | + RespondWithData("event: test\ndata: data1\n\n", headers=headers) |
| 117 | + ) |
| 118 | + |
| 119 | + with SSEClient( |
| 120 | + connect=mock, |
| 121 | + error_strategy=ErrorStrategy.from_lambda(lambda _: (ErrorStrategy.CONTINUE, None)), |
| 122 | + retry_delay_strategy=no_delay() |
| 123 | + ) as client: |
| 124 | + items = [] |
| 125 | + for item in client.all: |
| 126 | + items.append(item) |
| 127 | + if isinstance(item, Event): |
| 128 | + break |
| 129 | + |
| 130 | + # Should have: Fault (from error), Start (from retry), Event |
| 131 | + assert isinstance(items[0], Fault) |
| 132 | + assert isinstance(items[0].error, HTTPStatusError) |
| 133 | + |
| 134 | + assert isinstance(items[1], Start) |
| 135 | + assert items[1].headers == headers |
| 136 | + |
| 137 | + assert isinstance(items[2], Event) |
| 138 | + |
| 139 | + |
| 140 | +def test_connection_result_headers_property(): |
| 141 | + """Test that ConnectionResult properly stores and returns headers""" |
| 142 | + headers = {'X-Test': 'value'} |
| 143 | + result = ConnectionResult(stream=iter([b'data']), closer=None, headers=headers) |
| 144 | + assert result.headers == headers |
| 145 | + |
| 146 | + |
| 147 | +def test_connection_result_no_headers(): |
| 148 | + """Test that ConnectionResult returns None when no headers provided""" |
| 149 | + result = ConnectionResult(stream=iter([b'data']), closer=None) |
| 150 | + assert result.headers is None |
0 commit comments