httptoolkit / mockttp

Powerful friendly HTTP mock server & proxy library
https://httptoolkit.com
Apache License 2.0
784 stars 88 forks source link

thenCallback(): Multiple HTTP response headers with the same name #24

Closed dongmuni-rankwave closed 5 years ago

dongmuni-rankwave commented 5 years ago

Thank you for making a good library first. There was work to test the HTTPS MITM related logic, and I use it very well. But there was one thing that made me sad.

In order to respond to multiple HTTP headers with the same name, I assigned the value of the header to array[], and see that they are combined into one header and the value is separated by a comma(',').

proxyServer.anyRequest().thenCallback(request => 
    ({ 
        status: 200, 
        headers: { 
            'content-type': 'text/plain', 
            'set-cookie': ['key1=value1', 'key2=value2', 'key3=value3'] 
        }, 
        body: 'Hello World' 
    })
);

I checked the request-util.ts code and found it to be 'value.toString()'.

export const setHeaders = (response: express.Response, headers: Headers) => {
    Object.keys(headers).forEach((header) => {
        let value = headers[header];
        if (!value) return;

        response.setHeader(header, value.toString());
    });
};

So when I removed '.toString()' and tested it, I found that it is normally divided into three headers.

response.setHeader(header, value);

pimterry commented 5 years ago

Excellent point! Yeah, I totally agree - now fixed in https://github.com/httptoolkit/mockttp/commit/d3a450e87fc453a0df2f7931dd25590f0fd1e0e8. I've got a few other changes to include soon, but this'll get shipped in the next release.

Thanks for the report, great suggestion :+1:

dongmuni-rankwave commented 5 years ago

Thank you for your quick handling. I hope you have a happy day.

pimterry commented 5 years ago

Now published as v0.14.2

dongmuni-rankwave commented 5 years ago

Good!