Skip to content

Commit c076eba

Browse files
Hage Yaapahacksparrow
authored andcommitted
fix(http-server): correctly format IPv6 host in url
Enclose IPv6 host within `[]` in `url` property.
1 parent 46f9996 commit c076eba

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ matrix:
3030
branches:
3131
only:
3232
- master
33+
34+
before_script:
35+
# Add an IPv6 config - see the corresponding Travis issue
36+
# https://github.com/travis-ci/travis-ci/issues/8361
37+
- if [ "${TRAVIS_OS_NAME}" == "linux" ]; then
38+
sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6';
39+
fi

packages/http-server/src/http-server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ export class HttpServer {
100100
* URL of the HTTP / HTTPS server
101101
*/
102102
public get url(): string {
103-
return `${this._protocol}://${this.host}:${this.port}`;
103+
let host = this.host;
104+
if (this._address.family === 'IPv6') {
105+
host = `[${host}]`;
106+
}
107+
return `${this._protocol}://${host}:${this.port}`;
104108
}
105109

106110
/**

packages/http-server/test/integration/http-server.integration.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
55
import {HttpServer} from '../../';
66
import {supertest, expect} from '@loopback/testlab';
77
import * as makeRequest from 'request-promise-native';
8-
import {ServerRequest, ServerResponse} from 'http';
8+
import {ServerRequest, ServerResponse, get, IncomingMessage} from 'http';
99

1010
describe('HttpServer (integration)', () => {
1111
let server: HttpServer | undefined;
1212

1313
afterEach(stopServer);
1414

15+
it('formats IPv6 url correctly', async () => {
16+
server = new HttpServer(dummyRequestHandler, {host: '::1'});
17+
await server.start();
18+
expect(server.address!.family).to.equal('IPv6');
19+
const response = await getAsync(server.url);
20+
expect(response.statusCode).to.equal(200);
21+
});
22+
1523
it('starts server', async () => {
1624
server = new HttpServer(dummyRequestHandler);
1725
await server.start();
@@ -134,4 +142,10 @@ describe('HttpServer (integration)', () => {
134142
if (!server) return;
135143
await server.stop();
136144
}
145+
146+
function getAsync(url: string): Promise<IncomingMessage> {
147+
return new Promise((resolve, reject) => {
148+
get(url, resolve).on('error', reject);
149+
});
150+
}
137151
});

0 commit comments

Comments
 (0)