Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions test/nginx/src/mocha/nginx.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { isIPv6 } = require('node:net');
const tls = require('node:tls');
const { Readable } = require('stream');

Expand Down Expand Up @@ -1181,13 +1182,20 @@ async function resetMock(port) {
//
// 1. do not follow redirects
// 2. allow overriding of fetch's "forbidden" headers: https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name
function request(url, { body, ...options }={}) {
function request(urlString, { body, ...options }={}) {
if(!options.headers) options.headers = {};
if(!options.headers.host) options.headers.host = 'odk-nginx.example.test';

const url = new URL(urlString);
if(url.username || url.password) throw new Error('Basic auth creds not yet supported.');

options.host = safeIpv6(url.hostname);
Comment thread
alxndrsn marked this conversation as resolved.
Outdated
options.port = url.port;
options.path = urlString.replace(/^http(s?):\/\/[^/]*/, '') || '/';
Comment thread
alxndrsn marked this conversation as resolved.
Outdated

return new Promise((resolve, reject) => {
try {
const req = getProtocolImplFrom(url).request(url, options, res => {
const req = getProtocolImplFrom(url).request(options, res => {
res.on('error', reject);

const body = new Readable({ read:() => {} });
Expand Down Expand Up @@ -1223,8 +1231,7 @@ function request(url, { body, ...options }={}) {
});
}

function getProtocolImplFrom(url) {
const { protocol } = new URL(url);
function getProtocolImplFrom({ protocol }) {
switch(protocol) {
case 'http:': return require('node:http');
case 'https:': return require('node:https');
Expand Down Expand Up @@ -1288,3 +1295,8 @@ function assertCsp(actual, expected) {
);
}
}

function safeIpv6(hostname) {
const maybeV6 = hostname.replace(/^\[(.*)\]$/, (_, $1) => $1);
return isIPv6(maybeV6) ? maybeV6 : hostname;
}
Loading