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

401 Unauthorized Error using HTTPLocust #466

Closed
tiffanywilson opened this issue Aug 26, 2016 · 2 comments
Closed

401 Unauthorized Error using HTTPLocust #466

tiffanywilson opened this issue Aug 26, 2016 · 2 comments

Comments

@tiffanywilson
Copy link

tiffanywilson commented Aug 26, 2016

@heyman I have created a test using the scenario of a user which should login, then get a list of orders from an API endpoint. My test will login and extract the authToken (verified by printing in console), but any subsequent requests get an error 'HTTP Error(u'401 Unauthorized for this url')'. Everything I've read suggests session persistence is built into the HttpLocust class. I have tried changing the order of the requests, combining them into a single task, etc. Can you provide some direction on this? Here's my code:

`from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):

def on_start(self):
    print 'Login'
    response = self.client.post('/authendpoint/', {'username': 'username', 'password': 'password'})
    authToken = response.json()['auth_token']
    print authToken

@task(5)
def getOpenOrders(self):
    print 'Get Open Orders'
    self.client.get('/api/getOrdersEndpoint')

class User(HttpLocust):
host = 'http://localhost:3000'
task_set = UserBehavior
min_wait = 5000
max_wait = 10000`

Note: I did try making the authToken global and changing the second request to 'self.client.get('/api/getOrdersEndpoint', {'auth_token': authToken}). I also tried extracting the cookies using response.cookies, then inserting into the second request in a similar fashion. These only got me a new error (get() allows only 2 parameters, 3 were supplied).

@sweeneyb
Copy link

I'm actually looking for an example of how to test endpoints behind OAuth2, as well. I'm very new to the tool, but I'm getting the 401 as well and it's not obvious how to get around it. Thanks!

@heyman
Copy link
Member

heyman commented Oct 29, 2016

@tiffanywilson: Locust persists cookies between requests, which is the most common way for web apps to keep track of users' sessions. However, if the web app that is being tested doesn't rely on cookies for sessions, the auth token must be provided in the way that the app expects.

@sweeneyb Very hard to say what might be causing it, but it's most likely not an issue with Locust.

Closing this issue, but feel free to re-open it if you have indications that there's some kind of bug or other issue in Locust.

@heyman heyman closed this as completed Oct 29, 2016
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

3 participants