-
Notifications
You must be signed in to change notification settings - Fork 201
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
Cookie authentication #36
Comments
From my experience Frisby passes cookies on to subsequent requests automatically. |
I am able to get my tests to pass if I manually set the header cookie to a value that I generate via the browser. But I get Unauthorized if I do not manually assign a cookie session value. |
Are you nesting your tests? frisby.create('le Test')
.post('loginUrl', {user: 'hello', pass: 'world'})
.after(function() {
frisby.create('Test private url')
.get('privateUrl')
.expectStatus(200)
.toss()
})
.toss() |
Certainly am.
|
Just found the .afterJSON also passes the actual NodeJS response object, after the body, i.e. .afterJSON(function(body, res) {}); Looking through that I can see the cookie is being passed back:
|
You just need to set the header manually when you perform the next request using frisby.create('POST Login as admin')
.post(URL +'api/login', { username: 'xxxxxx', password: 'xxxxxx', eventId: 11})
.afterJSON(function(response, res) {
frisby.create('Get schools')
.get(URL +'api/schools')
.addHeader('Set-Cookie', res.headers['set-cookie'])
.expectJSONTypes('*', {
name: String,
coordinator: Object,
principal: Object,
user: Object
})
.toss();
})
.toss(); |
Also note that, in general, APIs should be stateless, so they really shouldn't be setting cookies (most API integrations don't store and re-send cookie headers). If you're using OAuth, you should only have to send an |
Thanks Vlucas & cboden, got it to work using this.
|
I agree Vlucas, a true RESTful api should be stateless, and use OAUTH or similar to manage authentication. |
Just wanted to share my solution if someone else comes here.I had the same problem. So I did the following:
|
thank you thank you guys!! @jozzhart - this line: |
Really really helpful! Thanks 💃 |
Exactly what I was looking for! Thx! |
same with mostly-novice ".addHeader('Cookie', cookie)" saved my life. |
can the get request be used with the .addHeader('xxx', xxx) command? |
@bradleywhit Yes, of course - and it is in the code above. |
It looks like the frisby api changed a bit in the past couple of years. For posterity, I did it just now using: return frisby.fetch(loginUrl, {
method: 'POST',
body: JSON.stringify({
username: 'myuser',
password: 'mypass'
}),
credentials: 'same-origin'
})
...
.then(function(res) {
return frisby.fetch(statusUrl, {
method: 'GET',
headers: {
cookie: res.headers.get('set-cookie')
},
credentials: 'same-origin'
})
...;
});
}); |
I see it's possible to authenticate using OAUTH by making subsequent calls after making the OAUTH call, but is there a way to get the returned cookie from a login POST and then add it to the header?
The text was updated successfully, but these errors were encountered: