Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

got AttributeError while using "with client.get(...) as response" #165

Closed
jiunjiunma opened this issue Jun 18, 2014 · 1 comment
Closed

Comments

@jiunjiunma
Copy link

Hi,
I am following the example in your documentation to change the response status. My code looks like something this:
with self.client.post(url, data, ...) as response:
if response.status_code == 200 or response.status_code == 401:
response.success()

However, I keep getting the following python errors while running it. It seems the __exit__ method was not found so the "with" syntax wouldn't work. 

Not sure if it's a bug or anything else I have to do to make it work. Your help will be really appreciated.

Thanks,
--Jiunjiun

[2014-06-18 10:07:46,757] jma/ERROR/stderr: Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/locust/core.py", line 267, in run
self.execute_next_task()
File "/Library/Python/2.7/site-packages/locust/core.py", line 293, in execute_next_task
self.execute_task(task["callable"], _task["args"], *_task["kwargs"])
File "/Library/Python/2.7/site-packages/locust/core.py", line 305, in execute_task
task(self, _args, *_kwargs)
File "/Users/jma/PycharmProjects/locust/rtc-register.py", line 52, in generate_account_token_form_post
self.validate_credential(username, password)
File "/Users/jma/PycharmProjects/locust/rtc-register.py", line 69, in validate_credential
with self.client.post(url, data, headers = {'content-type': 'application/json'}) as response:
AttributeError: exit

@heyman
Copy link
Member

heyman commented Jun 24, 2014

In order to make Locust's HTTP client return a context manager (that can be used with the with statement), you must add catch_response=True to the arguments. Like this:

with self.client.post("/some/url", data, catch_response=True) as response:
    if response.status_code == 401:
        response.success()

For more info, see the documentation: http://docs.locust.io/en/latest/writing-a-locustfile.html#manually-controlling-if-a-request-should-be-considered-successful-or-a-failure

@heyman heyman closed this as completed Jun 24, 2014
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

No branches or pull requests

2 participants