Skip to content

Commit 364993f

Browse files
authored
fix(http): fixed support for IPv6 literal strings in url (#5731)
1 parent d198085 commit 364993f

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

lib/adapters/http.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
427427
if (config.socketPath) {
428428
options.socketPath = config.socketPath;
429429
} else {
430-
options.hostname = parsed.hostname;
430+
options.hostname = parsed.hostname.startsWith("[") ? parsed.hostname.slice(1, -1) : parsed.hostname;
431431
options.port = parsed.port;
432432
setProxy(options, config.proxy, protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path);
433433
}

test/unit/adapters/http.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,44 @@ describe('supports http with nodejs', function () {
159159
delete process.env.no_proxy;
160160
});
161161

162+
it('should support IPv4 literal strings', function (done) {
163+
164+
var data = {
165+
firstName: 'Fred',
166+
lastName: 'Flintstone',
167+
emailAddr: '[email protected]'
168+
};
169+
170+
server = http.createServer(function (req, res) {
171+
res.setHeader('Content-Type', 'application/json');
172+
res.end(JSON.stringify(data));
173+
}).listen(4444, function () {
174+
axios.get('http://127.0.0.1:4444/').then(function (res) {
175+
assert.deepEqual(res.data, data);
176+
done();
177+
}).catch(done);
178+
});
179+
});
180+
181+
it('should support IPv6 literal strings', function (done) {
182+
183+
var data = {
184+
firstName: 'Fred',
185+
lastName: 'Flintstone',
186+
emailAddr: '[email protected]'
187+
};
188+
189+
server = http.createServer(function (req, res) {
190+
res.setHeader('Content-Type', 'application/json');
191+
res.end(JSON.stringify(data));
192+
}).listen(4444, function () {
193+
axios.get('http://[::1]:4444/').then(function (res) {
194+
assert.deepEqual(res.data, data);
195+
done();
196+
}).catch(done);
197+
});
198+
});
199+
162200
it('should throw an error if the timeout property is not parsable as a number', function (done) {
163201

164202
server = http.createServer(function (req, res) {

0 commit comments

Comments
 (0)