Skip to content

Commit 396489c

Browse files
authored
fix: fail to bind ipv6 (#362)
* fix: fail to bind ipv6 fix #361 * add test case
1 parent fa4ec6d commit 396489c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ function formatAddress(ip, port, root) {
7575
hostname = 'localhost';
7676
}
7777

78+
if (hostname.includes(':')) {
79+
hostname = `[${hostname}]`;
80+
}
81+
7882
const path = root.startsWith('/') ? root : `/${root}`;
7983
return new URL(`http://${hostname}:${port}${path}`).toString();
8084
}

test/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,42 @@ describe('server', () => {
277277
});
278278
});
279279

280+
it('bind ip `::1`', () => {
281+
const spy = sinon.spy();
282+
const stub = sinon.stub(hexo.log, 'info');
283+
stub.callsFake(spy);
284+
285+
return Promise.using(prepareServer({ip: '::1'}), app => {
286+
spy.args[1][1].should.contain('[::1]:4000');
287+
}).finally(() => {
288+
stub.restore();
289+
});
290+
});
291+
292+
it('bind ip `127.0.0.1`', () => {
293+
const spy = sinon.spy();
294+
const stub = sinon.stub(hexo.log, 'info');
295+
stub.callsFake(spy);
296+
297+
return Promise.using(prepareServer({ip: '127.0.0.1'}), app => {
298+
spy.args[1][1].should.contain('127.0.0.1:4000');
299+
}).finally(() => {
300+
stub.restore();
301+
});
302+
});
303+
304+
it('bind ip `localhost`', () => {
305+
const spy = sinon.spy();
306+
const stub = sinon.stub(hexo.log, 'info');
307+
stub.callsFake(spy);
308+
309+
return Promise.using(prepareServer({ip: 'localhost'}), app => {
310+
spy.args[1][1].should.contain('localhost:4000');
311+
}).finally(() => {
312+
stub.restore();
313+
});
314+
});
315+
280316
it('static', () => {
281317
const spy = sinon.spy();
282318
const stub = sinon.stub(hexo, 'load');

0 commit comments

Comments
 (0)