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

client.setHeaders only sets headers for first request #280

Closed
stephan-nordnes-eriksen opened this issue Sep 3, 2020 · 1 comment
Closed

Comments

@stephan-nordnes-eriksen
Copy link

I'm not sure if this is intended behavior or a bug, but in the setupClient, while doing a client.setHeaders, it only affects the first request in the requests array.

Example:

'use strict'

const autocannon = require('autocannon')

autocannon({
    url: `https://example.com`,
    connections: 1,
    duration: 1,
    amount: 1,
    setupClient: (client) => {
        const userInfo = getNextUserInfo()
        // I expect this to set the headers for all requests in the list below
        client.setHeaders({
            'Authorization': userInfo.authorizationToken,
            'Content-Type': 'application/json',
        })
    },
    requests: [
        {
            path: `/someEndpoint`,
            method: 'POST',
            setupRequest: (req, context) => {
                // I do this hack to get the headers forwarded to the next request;
                // context.authHeaders = req.headers // left out to show bug
                return {
                    ...req,
                    body: JSON.stringify({
                        command: 'startTournamentLevel',
                    }),
                }
            },
            onResponse: (status, body, context) => {
                if (status === 200) {
                    context.data = JSON.parse(body)
                }
            },
        },
        {
            path: `/someOtherEndpoint`,
            method: 'POST',
            setupRequest: (req, context) => {
                console.log('req.headers is not set', JSON.stringify(req.headers))
                return {
                    ...req,
                    // headers: context.authHeaders, // I do this hack to get the headers forwarded. Left out to expose bug
                    body: JSON.stringify({
                        someData: context.data.some,
                    }),
                }
            },
        }],
}, console.log)

I think I have found the reason in the source code;

The method on the httpClient passes the to the requestIterator

Client.prototype.setHeaders = function (newHeaders) {

It seems this method only sets the headers for the currentRequest

RequestIterator.prototype.setHeaders = function (newHeaders) {

I'm not sure what is the correct behavior, but I would expect that perhaps the httpClient saved the headers, and added it to all requests as the new default.

Again, not sure if it is a bug, but it feels like it. I was able to work around it by adding the headers to the context object on the first request, in case anyone else has this issue.

@mcollina
Copy link
Owner

mcollina commented Sep 3, 2020

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

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