Skip to content

Commit 8ce2c8e

Browse files
Merge pull request #143 from gleanwork/fix/x-glean-hook-multipart-upload
Fix RequestNotRead crash in XGlean hook for multipart file uploads
2 parents 96802b5 + c8b4d19 commit 8ce2c8e

2 files changed

Lines changed: 25 additions & 13 deletions

File tree

src/glean/api_client/_hooks/x_glean.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,10 @@ def before_request(
6161
config_experimental,
6262
)
6363

64-
# Create new headers dict with existing headers
65-
new_headers = dict(request.headers)
66-
6764
if deprecated_value:
68-
new_headers["X-Glean-Exclude-Deprecated-After"] = deprecated_value
65+
request.headers["X-Glean-Exclude-Deprecated-After"] = deprecated_value
6966

7067
if experimental_value:
71-
new_headers["X-Glean-Experimental"] = experimental_value
68+
request.headers["X-Glean-Experimental"] = experimental_value
7269

73-
# Return new request with updated headers
74-
return httpx.Request(
75-
method=request.method,
76-
url=request.url,
77-
headers=new_headers,
78-
content=request.content,
79-
extensions=request.extensions,
80-
)
70+
return request

tests/test_x_glean_hook.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,5 +239,27 @@ def test_preserves_request_method_and_url(self):
239239
assert str(result.url) == "https://api.example.com/v1/search"
240240

241241

242+
def test_handles_multipart_streaming_request(self):
243+
"""Should handle multipart requests without raising RequestNotRead.
244+
245+
Multipart file uploads use a streaming body. The previous implementation
246+
reconstructed the request via httpx.Request(..., content=request.content),
247+
which raised httpx.RequestNotRead on streaming bodies.
248+
"""
249+
hook = XGlean()
250+
request = httpx.Request(
251+
"POST",
252+
"https://example.com/rest/api/v1/uploadchatfiles",
253+
data={"field": "value"},
254+
files={"file": ("test.csv", b"a,b\n1,2", "text/csv")},
255+
)
256+
context = create_mock_context(include_experimental=True)
257+
258+
result = hook.before_request(context, request)
259+
260+
assert result.headers.get("X-Glean-Experimental") == "true"
261+
assert result.method == "POST"
262+
263+
242264
if __name__ == "__main__":
243265
pytest.main([__file__])

0 commit comments

Comments
 (0)