Skip to content

Commit

Permalink
fix: correctly format IPv6 address URLs in HTTP tests (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik authored Dec 2, 2024
1 parent 39afd66 commit ec38c42
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/command/http-command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TLSSocket } from 'node:tls';
import type { Socket as NetSocket } from 'node:net';
import { isIPv6, type Socket as NetSocket } from 'node:net';
import http from 'node:http';
import https from 'node:https';
import http2 from 'http2-wrapper';
Expand Down Expand Up @@ -140,7 +140,7 @@ export const urlBuilder = (options: HttpOptions): string => {
const port = options.port ? options.port : (options.protocol === 'HTTP' ? 80 : 443);
const path = `/${options.request.path}`.replace(/^\/\//, '/');
const query = options.request.query.length > 0 ? `?${options.request.query}`.replace(/^\?\?/, '?') : '';
const url = `${protocolPrefix}://${options.target}:${port}${path}${query}`;
const url = `${protocolPrefix}://${isIPv6(options.target) ? `[${options.target}]` : options.target}:${port}${path}${query}`;

return url;
};
Expand Down
59 changes: 59 additions & 0 deletions test/unit/command/http-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,65 @@ describe('http command', () => {
});
});

describe('target', () => {
it('should enclose an IPv6 addresses in brackets', () => {
const options = {
type: 'http' as const,
target: '2606:4700:4700::1111',
protocol: 'HTTP',
request: {
method: 'GET',
path: '/',
query: '',
},
inProgressUpdates: false,
ipVersion: 6,
};

const url = urlBuilder(options);

expect(url).to.equal('http://[2606:4700:4700::1111]:80/');
});

it('should not enclose an IPv4 addresses in brackets', () => {
const options = {
type: 'http' as const,
target: '1.1.1.1',
protocol: 'HTTP',
request: {
method: 'GET',
path: '/',
query: '',
},
inProgressUpdates: false,
ipVersion: 6,
};

const url = urlBuilder(options);

expect(url).to.equal('http://1.1.1.1:80/');
});

it('should enclose a domain in brackets', () => {
const options = {
type: 'http' as const,
target: 'jsdelivr.com',
protocol: 'HTTP',
request: {
method: 'GET',
path: '/',
query: '',
},
inProgressUpdates: false,
ipVersion: 6,
};

const url = urlBuilder(options);

expect(url).to.equal('http://jsdelivr.com:80/');
});
});

describe('port', () => {
it('should set custom port', () => {
const options = {
Expand Down

0 comments on commit ec38c42

Please sign in to comment.