Skip to content

Add rest method to HttpUser - #3512

Merged
cyberw merged 1 commit into
locustio:masterfrom
anandghegde:httpuser-rest
Sep 14, 2026
Merged

cyberw merged 1 commit into
locustio:masterfrom
anandghegde:httpuser-rest

Conversation

@anandghegde

Copy link
Copy Markdown
Contributor

rest() / rest_() were only on FastHttpUser. People on the default HttpUser who test JSON APIs had to write the catch_response + json() + JSONDecodeError pattern 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 to HttpUser.

PR #2274 moved rest from RestUser into FastHttpUser. I couldn't find any issue or discussion that says it should stay FastHttpUser-only.

What changed

  • The rest/rest_ code (and _callstack_regex) moved unchanged out of FastHttpUser into a small RestMixin in locust/user/users.py. HttpUser and FastHttpUser both inherit from it.
    • FastHttpUser behaves exactly as before. The only edits to the moved code are typing: the yield type is now a generic parameter, and self.client is untyped inside the mixin.
  • RestMixin is generic over the response type, so type checkers still see the right types:
    • FastHttpUser.rest() yields locust.contrib.fasthttp.RestResponseContextManager, as before.
    • HttpUser.rest() yields a new locust.clients.RestResponseContextManager, a ResponseContextManager with js: dict, mirroring the fasthttp one.
  • Docs: rest/rest_ are now listed in the HttpUser API docs, and the REST sections in writing-a-locustfile.rst and increase-performance.rst say the method is available on both users.

Usage

from locust import HttpUser, task

class MyUser(HttpUser):
    @task
    def t(self):
        with self.rest("POST", "/api/items", json={"foo": 1}) as resp:
            assert resp.js["foo"] == 1, "unexpected foo,"

Testing

  • Added TestHttpUserRest to locust/test/test_http.py:
    • test_rest_success and test_rest_fail mirror the tests in test_fasthttp.py.
    • New tests cover invalid JSON, a connection error (python-requests returns content=None there), an exception raised inside the with-block, and rest_ (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 is TestFastHttpSsl::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.
  • I ran mypy with reveal_type in a small locustfile to confirm the response types shown above, and checked that examples/rest.py type-checks the same as on master.
  • sphinx-build renders locust.HttpUser.rest/rest_ and FastHttpUser.rest/rest_.

Claude Code helped write this change. I reviewed it.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QBWFWqjbUQGS1c9LsM2bxX

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
@cyberw

cyberw commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

Looks brilliant, thanks!

@cyberw
cyberw merged commit 0d2475e into locustio:master Sep 14, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants