-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathtest_additional_headers_simple.py
More file actions
228 lines (182 loc) · 8.54 KB
/
test_additional_headers_simple.py
File metadata and controls
228 lines (182 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""Simplified tests for additional_headers functionality in Langfuse client.
This module tests that additional headers are properly configured in the HTTP clients.
"""
from typing import Sequence
import httpx
from opentelemetry.sdk.trace import ReadableSpan
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
from langfuse._client.client import Langfuse
class NoOpSpanExporter(SpanExporter):
"""Minimal exporter used to verify custom exporter injection."""
def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
return SpanExportResult.SUCCESS
def shutdown(self) -> None:
pass
class TestAdditionalHeadersSimple:
"""Simple test suite for additional_headers functionality."""
def teardown_method(self):
"""Clean up after each test to avoid singleton interference."""
from langfuse._client.resource_manager import LangfuseResourceManager
LangfuseResourceManager.reset()
def test_httpx_client_has_additional_headers_when_none_provided(self):
"""Test that additional headers are set in httpx client when no custom client is provided."""
additional_headers = {
"X-Custom-Header": "custom-value",
"X-Another-Header": "another-value",
}
langfuse = Langfuse(
public_key="test-public-key",
secret_key="test-secret-key",
host="https://mock-host.com",
additional_headers=additional_headers,
tracing_enabled=False, # Disable tracing to avoid OTEL setup
)
# Verify the httpx client has the additional headers
assert (
langfuse._resources.httpx_client.headers["X-Custom-Header"]
== "custom-value"
)
assert (
langfuse._resources.httpx_client.headers["X-Another-Header"]
== "another-value"
)
def test_custom_httpx_client_with_additional_headers_ignores_additional_headers(
self,
):
"""Test that when additional headers are provided with custom client, additional headers are ignored."""
# Create a custom httpx client with headers
existing_headers = {"X-Existing-Header": "existing-value"}
custom_client = httpx.Client(headers=existing_headers)
additional_headers = {
"X-Custom-Header": "custom-value",
"X-Another-Header": "another-value",
}
langfuse = Langfuse(
public_key="test-public-key",
secret_key="test-secret-key",
host="https://mock-host.com",
httpx_client=custom_client,
additional_headers=additional_headers,
tracing_enabled=False,
)
# Verify the original client is used (same instance)
assert langfuse._resources.httpx_client is custom_client
# Verify existing headers are preserved and additional headers are NOT added
assert (
langfuse._resources.httpx_client.headers["x-existing-header"]
== "existing-value"
)
# Additional headers should NOT be present
assert "x-custom-header" not in langfuse._resources.httpx_client.headers
assert "x-another-header" not in langfuse._resources.httpx_client.headers
def test_custom_httpx_client_without_additional_headers_preserves_client(self):
"""Test that when no additional headers are provided, the custom client is preserved."""
# Create a custom httpx client with headers
existing_headers = {"X-Existing-Header": "existing-value"}
custom_client = httpx.Client(headers=existing_headers)
langfuse = Langfuse(
public_key="test-public-key",
secret_key="test-secret-key",
host="https://mock-host.com",
httpx_client=custom_client,
additional_headers=None, # No additional headers
tracing_enabled=False,
)
# Note: The client instance might be different due to Fern API wrapper behavior,
# but the important thing is that the headers are preserved
# Verify existing headers are preserved
assert (
langfuse._resources.httpx_client.headers["x-existing-header"]
== "existing-value"
)
def test_media_manager_uses_custom_httpx_client(self):
"""Test that media manager reuses the configured custom httpx client."""
custom_client = httpx.Client()
langfuse = Langfuse(
public_key="test-public-key",
secret_key="test-secret-key",
host="https://mock-host.com",
httpx_client=custom_client,
tracing_enabled=False,
)
assert langfuse._resources is not None
assert langfuse._resources._media_manager._httpx_client is custom_client
def test_none_additional_headers_works(self):
"""Test that passing None for additional_headers works without errors."""
langfuse = Langfuse(
public_key="test-public-key",
secret_key="test-secret-key",
host="https://mock-host.com",
additional_headers=None,
tracing_enabled=False,
)
# Verify client was created successfully
assert langfuse is not None
assert langfuse._resources is not None
assert langfuse._resources.httpx_client is not None
def test_empty_additional_headers_works(self):
"""Test that passing an empty dict for additional_headers works."""
langfuse = Langfuse(
public_key="test-public-key",
secret_key="test-secret-key",
host="https://mock-host.com",
additional_headers={},
tracing_enabled=False,
)
# Verify client was created successfully
assert langfuse is not None
assert langfuse._resources is not None
assert langfuse._resources.httpx_client is not None
def test_span_processor_has_additional_headers_in_otel_exporter(self):
"""Test that span processor includes additional headers in OTEL exporter."""
from langfuse._client.span_processor import LangfuseSpanProcessor
additional_headers = {
"X-Custom-Trace-Header": "trace-value",
"X-Override-Default": "override-value",
}
# Create span processor with additional headers
processor = LangfuseSpanProcessor(
public_key="test-public-key",
secret_key="test-secret-key",
base_url="https://mock-host.com",
additional_headers=additional_headers,
)
# Get the OTLP span exporter to check its headers
exporter = processor.span_exporter
# Verify additional headers are in the exporter's headers
assert exporter._headers["X-Custom-Trace-Header"] == "trace-value"
assert exporter._headers["X-Override-Default"] == "override-value"
# Verify default headers are still present
assert "Authorization" in exporter._headers
assert "x-langfuse-sdk-name" in exporter._headers
assert "x-langfuse-public-key" in exporter._headers
# Check that our override worked
assert exporter._headers["X-Override-Default"] == "override-value"
def test_span_processor_none_additional_headers_works(self):
"""Test that span processor works with None additional headers."""
from langfuse._client.span_processor import LangfuseSpanProcessor
# Create span processor without additional headers
processor = LangfuseSpanProcessor(
public_key="test-public-key",
secret_key="test-secret-key",
base_url="https://mock-host.com",
additional_headers=None,
)
# Get the OTLP span exporter
exporter = processor.span_exporter
# Verify default headers are present
assert "Authorization" in exporter._headers
assert "x-langfuse-sdk-name" in exporter._headers
assert "x-langfuse-public-key" in exporter._headers
def test_span_processor_uses_custom_span_exporter_when_provided(self):
"""Test that a custom exporter bypasses the default OTLP exporter construction."""
from langfuse._client.span_processor import LangfuseSpanProcessor
custom_exporter = NoOpSpanExporter()
processor = LangfuseSpanProcessor(
public_key="test-public-key",
secret_key="test-secret-key",
base_url="https://mock-host.com",
additional_headers={"X-Custom-Trace-Header": "trace-value"},
span_exporter=custom_exporter,
)
assert processor.span_exporter is custom_exporter