Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/glean/api_client/_hooks/x_glean.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,10 @@ def before_request(
config_experimental,
)

# Create new headers dict with existing headers
new_headers = dict(request.headers)

if deprecated_value:
new_headers["X-Glean-Exclude-Deprecated-After"] = deprecated_value
request.headers["X-Glean-Exclude-Deprecated-After"] = deprecated_value

if experimental_value:
new_headers["X-Glean-Experimental"] = experimental_value
request.headers["X-Glean-Experimental"] = experimental_value

# Return new request with updated headers
return httpx.Request(
method=request.method,
url=request.url,
headers=new_headers,
content=request.content,
extensions=request.extensions,
)
return request
22 changes: 22 additions & 0 deletions tests/test_x_glean_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,27 @@ def test_preserves_request_method_and_url(self):
assert str(result.url) == "https://api.example.com/v1/search"


def test_handles_multipart_streaming_request(self):
"""Should handle multipart requests without raising RequestNotRead.

Multipart file uploads use a streaming body. The previous implementation
reconstructed the request via httpx.Request(..., content=request.content),
which raised httpx.RequestNotRead on streaming bodies.
"""
hook = XGlean()
request = httpx.Request(
"POST",
"https://example.com/rest/api/v1/uploadchatfiles",
data={"field": "value"},
files={"file": ("test.csv", b"a,b\n1,2", "text/csv")},
)
context = create_mock_context(include_experimental=True)

result = hook.before_request(context, request)

assert result.headers.get("X-Glean-Experimental") == "true"
assert result.method == "POST"


if __name__ == "__main__":
pytest.main([__file__])
Loading