Skip to content

Commit

Permalink
test: IPv6 literals support
Browse files Browse the repository at this point in the history
  • Loading branch information
serverwentdown committed Sep 3, 2021
1 parent 7d20fea commit 5b8aa7c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
30 changes: 30 additions & 0 deletions test/main.js
Expand Up @@ -2334,3 +2334,33 @@ describe('node-fetch', () => {
expect(res.url).to.equal(`${base}m%C3%B6bius`);
});
});

describe('node-fetch using IPv6', () => {
const local = new TestServer('[::1]');
let base;

before(async () => {
await local.start();
base = `http://${local.hostname}:${local.port}/`;
});

after(async () => {
return local.stop();
});

it('should resolve into response', () => {
const url = `${base}hello`;
expect(url).to.contain('[::1]');
return fetch(url).then(res => {
expect(res).to.be.an.instanceof(Response);
expect(res.headers).to.be.an.instanceof(Headers);
expect(res.body).to.be.an.instanceof(stream.Transform);
expect(res.bodyUsed).to.be.false;

expect(res.url).to.equal(url);
expect(res.ok).to.be.true;
expect(res.status).to.equal(200);
expect(res.statusText).to.equal('OK');
});
});
});
13 changes: 7 additions & 6 deletions test/utils/server.js
Expand Up @@ -4,7 +4,7 @@ import {once} from 'events';
import Busboy from 'busboy';

export default class TestServer {
constructor() {
constructor(hostname) {
this.server = http.createServer(this.router);
// Node 8 default keepalive timeout is 5000ms
// make it shorter here as we want to close server quickly at the end of tests
Expand All @@ -15,10 +15,15 @@ export default class TestServer {
this.server.on('connection', socket => {
socket.setTimeout(1500);
});
this.hostname = hostname || 'localhost';
}

async start() {
this.server.listen(0, 'localhost');
let host = this.hostname;
if (host.startsWith('[')) {
host = host.slice(1, -1);
}
this.server.listen(0, host);
return once(this.server, 'listening');
}

Expand All @@ -31,10 +36,6 @@ export default class TestServer {
return this.server.address().port;
}

get hostname() {
return 'localhost';
}

mockResponse(responseHandler) {
this.server.nextResponseHandler = responseHandler;
return `http://${this.hostname}:${this.port}/mocked`;
Expand Down

0 comments on commit 5b8aa7c

Please sign in to comment.