Skip to content

Commit

Permalink
Add Headers Field to Request Response (#35)
Browse files Browse the repository at this point in the history
* added new field 'headers' to RequestResponse

* updated tests with asserts for headers

* updated test assertions for headers

* updated case of target header based on output

* removed useless assert
  • Loading branch information
fullerzz authored May 1, 2024
1 parent 89e897c commit a9890e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
37 changes: 31 additions & 6 deletions src/loamy/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class RequestResponse(msgspec.Struct):
request_map: RequestMap
status_code: int
body: dict | None = None
headers: dict[str, str] | None = None
error: BaseException | None = None


Expand Down Expand Up @@ -126,7 +127,11 @@ async def _send_get_request(
logger.trace("Successfully read response as text")
body = {"text": text}
response = RequestResponse(
request_map=req_map, status_code=status_code, body=body, error=error
request_map=req_map,
status_code=status_code,
body=body,
headers=dict(resp.headers),
error=error,
)
return response

Expand All @@ -152,7 +157,11 @@ async def _send_post_request(
logger.trace("Successfully read response as text")
body = {"text": text}
response = RequestResponse(
request_map=req_map, status_code=status_code, body=body, error=error
request_map=req_map,
status_code=status_code,
body=body,
headers=dict(resp.headers),
error=error,
)
return response

Expand All @@ -178,7 +187,11 @@ async def _send_put_request(
logger.trace("Successfully read response as text")
body = {"text": text}
response = RequestResponse(
request_map=req_map, status_code=status_code, body=body, error=error
request_map=req_map,
status_code=status_code,
body=body,
headers=dict(resp.headers),
error=error,
)
return response

Expand All @@ -204,7 +217,11 @@ async def _send_patch_request(
logger.trace("Successfully read response as text")
body = {"text": text}
response = RequestResponse(
request_map=req_map, status_code=status_code, body=body, error=error
request_map=req_map,
status_code=status_code,
body=body,
headers=dict(resp.headers),
error=error,
)
return response

Expand All @@ -230,7 +247,11 @@ async def _send_options_request(
logger.trace("Successfully read response as text")
body = {"text": text}
response = RequestResponse(
request_map=req_map, status_code=status_code, body=body, error=error
request_map=req_map,
status_code=status_code,
body=body,
headers=dict(resp.headers),
error=error,
)
return response

Expand All @@ -256,6 +277,10 @@ async def _send_delete_request(
logger.trace("Successfully read response as text")
body = {"text": text}
response = RequestResponse(
request_map=req_map, status_code=status_code, body=body, error=error
request_map=req_map,
status_code=status_code,
body=body,
headers=dict(resp.headers),
error=error,
)
return response
4 changes: 3 additions & 1 deletion tests/bin/test_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ async def index() -> Response:
@post("/foo")
async def post_foo(request: Request) -> Response:
data = await request.json()
return json(data)
resp: Response = json(data)
resp.add_header(b"X-Test", b"Test")
return resp


@get("/exception")
Expand Down
8 changes: 6 additions & 2 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ def request_map_to_trigger_exception() -> RequestMap:


def test_send_requests(request_map_collection: List[RequestMap]) -> None:
assert True is True
session = Clump(requests=request_map_collection)
responses: List[RequestResponse] = session.send_requests()
assert len(responses) == 100
for response in responses:
assert response.status_code == 200
assert response.error is None

assert response.headers is not None
assert response.headers["Content-Type"] == "application/json"
if response.request_map.http_op == "POST":
assert response.headers is not None
assert "x-test" in response.headers
assert "Test" == response.headers["x-test"]

def test_send_requests_with_exceptions(
request_map_collection: List[RequestMap],
Expand Down

0 comments on commit a9890e7

Please sign in to comment.