Pass headers in "onResponse" of requests array
See original GitHub issueI’m trying to test an application which has the server set a cookie in the response, redirects the user to a different path and then checks the for the cookie in the second request. So in short: In step 1 I send a GET to the server which responds with a 302 and a cookie. In step 2 I need go to the location of the 302 and send along the cookie I received in the first request.
I’m using the autocannon requests array to set up the sequence of requests that need to happen.
My problem is that the “onResponse” function doesn’t expose the headers that are returned from the first response. This means I can’t access the “set-cookie” header which in turn means I can’t provide the cookie in the header of the next request
My autocannon setup is like this:
autocannon({
url: "localhost:3000",
requests: [
{
// Start the request
method: 'GET',
path: '/auth,
onResponse: (status, body, context) => {
// here I would like to access the "set-cookie" header so I can add it to the context like this:
// context.cookie = headers["set-cookie"];
// but no headers are passed to the onResponse method.
context.path = body.url;
}
},
{
// Now go the path returned in the previous response and send along the cookie
method: 'GET',
setupRequest: (req: any, context: any) => ({
...req,
path: context.path,
headers: {"cookie": context.cookie}
}),
onResponse: (status: number, body: string, context: { [key: string]: any }, headers: any) => {
}
}
Are there any ways around this? Is there another way I can use autocannon to achieve what I need?
I checked out the clients API but I don’t think the client.on(“headers”) event helps my case as it doesn’t keep track of where in the request cycle I am. Further it just gives the headers keys and values back as a flat list so I have no idea of knowing where a header stops and the next header begins.
A solution for me would be to pass on the headers in the onResponse method. I can make a PR for this but I’d like to know if there are alternatives first.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
I would like to take this PR 😃
After testing this out it works great in most cases however it doesn’t seem to work when a server sends back multiple headers with the same key but a different value. For example my server sends back multiple
set-cookie
headers.The current implementation puts all of the these headers under the same
set-cookie
key, effectively overwriting the previous value for each header that has the same key.I think a better solution would be to instead create an array with the values in the case where a server send back multiple headers with the same key.
To summarize current behaviour: when server sends:
results in headers object -->
{"set-cookie": "ghi"}
New/expected behaviour: when server sends:
results in headers object -->
{"set-cookie": ["abc", "def", "ghi"]}