Skip to content

Commit

Permalink
Fixed for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ninadbstack committed Nov 29, 2023
1 parent 8c99181 commit c950178
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/core/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ export class Server extends http.Server {
// return a string representation of the server address
address() {
let port = this.port;
let host = `http://${this.host}`;
// we need to specifically map 0.0.0.0 to localhost on windows as even though we
// can listen to all interfaces using 0.0.0.0 we cant make a request on 0.0.0.0 as
// its an invalid ip address as per spec, but unix systems allow request to it and
// falls back to localhost
let host = `http://${this.host === '0.0.0.0' ? 'localhost' : this.host}`;
return port ? `${host}:${port}` : host;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/percy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('Percy', () => {

describe('#address()', () => {
it('returns the server API address', async () => {
expect(percy.address()).toEqual('http://0.0.0.0:5338');
expect(percy.address()).toEqual('http://localhost:5338');
});
});

Expand Down
31 changes: 29 additions & 2 deletions packages/core/test/unit/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,38 @@ describe('Unit / Server', () => {

describe('#address()', () => {
it('returns the localhost address for the server', () => {
expect(server.address()).toEqual('http://0.0.0.0:8000');
// converts default 0.0.0.0 to localhost
expect(server.address()).toEqual('http://localhost:8000');
});

it('does not include the port without a default when not listening', () => {
expect(Server.createServer().address()).toEqual('http://0.0.0.0');
expect(Server.createServer().address()).toEqual('http://localhost');
});

describe('with PERCY_SERVER_HOST set', () => {
afterEach(() => {
delete process.env.PERCY_SERVER_HOST;
});

describe('when PERCY_SERVER_HOST=localhost', () => {
beforeEach(() => {
process.env.PERCY_SERVER_HOST = 'localhost';
});

it('it uses localhost correctly', () => {
expect(Server.createServer().address()).toEqual('http://localhost');
});
});

describe('when PERCY_SERVER_HOST=120.22.12.1', () => {
beforeEach(() => {
process.env.PERCY_SERVER_HOST = '120.22.12.1';
});

it('it uses 120.22.12.1 correctly', () => {
expect(Server.createServer().address()).toEqual('http://120.22.12.1');
});
});
});
});

Expand Down

0 comments on commit c950178

Please sign in to comment.