Repository navigation
Add rest method to HttpUser - #3512
Merged
Merged
Conversation
Move FastHttpUser's rest/rest_ implementation into a RestMixin shared by HttpUser and FastHttpUser, so JSON/REST response checking is available with both clients. FastHttpUser behaviour is unchanged. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01QBWFWqjbUQGS1c9LsM2bxX
Collaborator
|
Looks brilliant, thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
rest()/rest_()were only onFastHttpUser. People on the defaultHttpUserwho test JSON APIs had to write thecatch_response+json()+JSONDecodeErrorpattern themselves (see "REST/JSON APIs" in writing-a-locustfile.rst). k6 also makes JSON response checks first-class for its default HTTP client. This PR adds the same helpers toHttpUser.PR #2274 moved
restfromRestUserintoFastHttpUser. I couldn't find any issue or discussion that says it should stay FastHttpUser-only.What changed
rest/rest_code (and_callstack_regex) moved unchanged out ofFastHttpUserinto a smallRestMixininlocust/user/users.py.HttpUserandFastHttpUserboth inherit from it.FastHttpUserbehaves exactly as before. The only edits to the moved code are typing: the yield type is now a generic parameter, andself.clientis untyped inside the mixin.RestMixinis generic over the response type, so type checkers still see the right types:FastHttpUser.rest()yieldslocust.contrib.fasthttp.RestResponseContextManager, as before.HttpUser.rest()yields a newlocust.clients.RestResponseContextManager, aResponseContextManagerwithjs: dict, mirroring the fasthttp one.rest/rest_are now listed in theHttpUserAPI docs, and the REST sections in writing-a-locustfile.rst and increase-performance.rst say the method is available on both users.Usage
Testing
TestHttpUserResttolocust/test/test_http.py:test_rest_successandtest_rest_failmirror the tests intest_fasthttp.py.content=Nonethere), an exception raised inside the with-block, andrest_(timestamp query parameter and the?_=...stats name).uv run pytest locust/test/test_http.py locust/test/test_fasthttp.py locust/test/test_locust_class.py locust/test/test_users.py: 155 passed, 1 failed. The failure isTestFastHttpSsl::test_ssl_request_insecure, which also fails on master in my local environment.uv run ruff check locust/ examples/,uv run ruff format --check,uv run mypy locust/: all clean.reveal_typein a small locustfile to confirm the response types shown above, and checked thatexamples/rest.pytype-checks the same as on master.sphinx-buildrenderslocust.HttpUser.rest/rest_andFastHttpUser.rest/rest_.Claude Code helped write this change. I reviewed it.
🤖 Generated with Claude Code
https://claude.ai/code/session_01QBWFWqjbUQGS1c9LsM2bxX