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

Fixed handling of array values for AxiosHeaders; #5085

Merged
merged 3 commits into from Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -39,7 +39,7 @@ export class AxiosHeaders {

normalize(format: boolean): AxiosHeaders;

toJSON(): RawAxiosHeaders;
toJSON(asStrings?: boolean): RawAxiosHeaders;

static from(thing?: AxiosHeaders | RawAxiosHeaders | string): AxiosHeaders;

Expand Down
14 changes: 4 additions & 10 deletions lib/core/AxiosHeaders.js
Expand Up @@ -15,7 +15,7 @@ function normalizeValue(value) {
return value;
}

return String(value);
return utils.isArray(value) ? value.map(normalizeValue) : String(value);
}

function parseTokens(str) {
Expand Down Expand Up @@ -102,13 +102,7 @@ Object.assign(AxiosHeaders.prototype, {
return;
}

if (utils.isArray(_value)) {
_value = _value.map(normalizeValue);
} else {
_value = normalizeValue(_value);
}

self[key || _header] = _value;
self[key || _header] = normalizeValue(_value);
}

if (utils.isPlainObject(header)) {
Expand Down Expand Up @@ -222,13 +216,13 @@ Object.assign(AxiosHeaders.prototype, {
return this;
},

toJSON: function() {
toJSON: function(asStrings) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is toJSON used? Cannot see anywhere where it is used with asString === true?
Are there cases where one wants arrays to be returned as string?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is toJSON used?

at least in test cases to make deep assertions, but...

asString === true

AxiosHeaders is a public class, so not 100% of its functionality should be used inside the Axios codebase, from the user's point of view, it should be a complete helper implementation for dealing with headers.

const obj = Object.create(null);

utils.forEach(Object.assign({}, this[$defaults] || null, this),
(value, header) => {
if (value == null || value === false) return;
obj[header] = utils.isArray(value) ? value.join(', ') : value;
obj[header] = asStrings && utils.isArray(value) ? value.join(', ') : value;
});

return obj;
Expand Down
10 changes: 10 additions & 0 deletions test/unit/core/AxiosHeaders.js
Expand Up @@ -327,5 +327,15 @@ describe('AxiosHeaders', function () {
bar: '3'
});
});

it('should support array values', function () {
const headers = new AxiosHeaders({
foo: [1,2,3]
});

assert.deepStrictEqual({...headers.normalize().toJSON()}, {
foo: ['1','2','3']
});
});
});
});
30 changes: 30 additions & 0 deletions test/unit/regression/bugs.js
@@ -1,4 +1,5 @@
import assert from 'assert';
import http from 'http';
import axios from '../../../index.js';

describe('issues', function () {
Expand All @@ -10,4 +11,33 @@ describe('issues', function () {
assert.strictEqual(data.args.foo2, 'bar2');
});
});

describe('5028', function () {
it('should handle set-cookie headers as an array', async function () {
const cookie1 = 'something=else; path=/; expires=Wed, 12 Apr 2023 12:03:42 GMT; samesite=lax; secure; httponly';
const cookie2 = 'something-ssr.sig=n4MlwVAaxQAxhbdJO5XbUpDw-lA; path=/; expires=Wed, 12 Apr 2023 12:03:42 GMT; samesite=lax; secure; httponly';

const server = http.createServer((req, res) => {
//res.setHeader('Set-Cookie', 'my=value');
res.setHeader('Set-Cookie', [cookie1, cookie2]);
res.writeHead(200);
res.write('Hi there');
res.end();
}).listen(0);

const request = axios.create();

request.interceptors.response.use((res) => {
assert.deepStrictEqual(res.headers['set-cookie'], [
cookie1, cookie2
]);
});

try {
await request({url: `http://localhost:${server.address().port}`});
} finally {
server.close()
}
});
});
});