Skip to content

Commit

Permalink
test: do not use fetch when overriding host header
Browse files Browse the repository at this point in the history
  • Loading branch information
fredriklindberg committed Oct 30, 2023
1 parent 0bb6986 commit deeed7e
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 122 deletions.
33 changes: 23 additions & 10 deletions test/e2e/test_cluster.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import child_process from 'child_process';
import crypto from 'crypto';
import assert from 'assert/strict';
import http from 'http';
import { setTimeout } from 'timers/promises';
import { createAccount, createEchoServer, exposrCliImageTag, getAuthToken, getTunnel, putTunnel } from './e2e-utils.js';

Expand Down Expand Up @@ -145,22 +146,34 @@ describe('Cluster E2E', () => {

const ingressUrl = new URL(data.ingress.http.url);

res = await fetch("http://localhost:8080", {
method: 'POST',
headers: {
"Host": `${ingressUrl.hostname}:8080`
},
body: "echo"
})

data = await res.text()
let status;
([status, data] = await new Promise((resolve) => {
const req = http.request({
hostname: 'localhost',
port: 8080,
method: 'POST',
path: '/',
headers: {
"Host": ingressUrl.hostname
}
}, (res) => {
let data = '';

res.on('data', (chunk) => {
data += chunk;
});

res.on('close', () => { resolve([res.statusCode, data])});
});
req.end('echo');
}));

exposrCliTerminator();
await echoServerTerminate();
node1.terminate();
node2.terminate();

assert(res.status == 200, `expected status code 200, got ${res.status}`);
assert(status == 200, `expected status code 200, got ${status}`);
assert(data == "echo", `did not get response from echo server through WS tunnel, got ${data}`);
}).timeout(120000);
});
Expand Down
42 changes: 28 additions & 14 deletions test/e2e/test_ssh.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'assert/strict';
import crypto from 'crypto';
import http from 'node:http';
import { setTimeout } from 'timers/promises';
import { createAccount, createEchoServer, getAuthToken, getTunnel, putTunnel, sshClient } from './e2e-utils.js';

Expand All @@ -26,7 +27,7 @@ describe('SSH transport E2E', () => {

after(async () => {
process.env.NODE_ENV = "test";
await terminator(undefined, {gracefulTimeout: 1000, drainTimeout: 500});
await terminator(undefined, {gracefulTimeout: 1000, drainTimeout: 500});
await echoServerTerminator()
});

Expand Down Expand Up @@ -54,7 +55,7 @@ describe('SSH transport E2E', () => {
assert(res.status == 200, "could not create tunnel")

res = await getTunnel(authToken, tunnelId);
let data = await res.json();
let data = await res.json();
assert(data?.transport?.ssh?.enabled == true, "SSH transport not enabled");
assert(typeof data?.transport?.ssh?.url == 'string', "No SSH connect URL available");

Expand All @@ -66,32 +67,45 @@ describe('SSH transport E2E', () => {
data?.transport?.ssh?.username,
data?.transport?.ssh?.password,
targetUrl,
);
);

authToken = await getAuthToken(account.account_id);
do {
await setTimeout(1000);
res = await getTunnel(authToken, tunnelId);
data = await res.json();
data = await res.json();
} while (data?.connection?.connected == false);

assert(data?.connection?.connected == true, "tunnel not connected");

const ingressUrl = new URL(data.ingress.http.url);

res = await fetch("http://localhost:8080", {
method: 'POST',
headers: {
"Host": ingressUrl.hostname
},
body: "echo"
})
let status;
([status, data] = await new Promise((resolve) => {
const req = http.request({
hostname: 'localhost',
port: 8080,
method: 'POST',
path: '/',
headers: {
"Host": ingressUrl.hostname
}
}, (res) => {
let data = '';

assert(res.status == 200, `expected status code 200, got ${res.status}`);
data = await res.text()
assert(data == "echo", `did not get response from echo server through WS tunnel, got ${data}`)
res.on('data', (chunk) => {
data += chunk;
});

res.on('close', () => { resolve([res.statusCode, data])});
});
req.end('echo');
}));

terminateClient();

assert(status == 200, `expected status code 200, got ${status}`);
assert(data == "echo", `did not get response from echo server through WS tunnel, got ${data}`);

}).timeout(60000);
});
34 changes: 23 additions & 11 deletions test/e2e/test_ws.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'assert/strict';
import crypto from 'crypto';
import http from 'node:http';
import { setTimeout } from 'timers/promises';
import { createAccount, createEchoServer, getAuthToken, getTunnel, putTunnel, startExposr } from './e2e-utils.js';
import { PGSQL_URL, REDIS_URL } from '../env.js';
Expand Down Expand Up @@ -59,22 +60,33 @@ describe('Websocket E2E', () => {

assert(data?.connection?.connected == true, "tunnel not connected");

const ingressUrl = new URL(data.ingress.http.url);
let status;
([status, data] = await new Promise((resolve) => {
const req = http.request({
hostname: 'localhost',
port: 8080,
method: 'POST',
path: '/',
headers: {
"Host": ingressUrl.hostname
}
}, (res) => {
let data = '';

res = await fetch("http://localhost:8080", {
method: 'POST',
headers: {
"Host": ingressUrl.hostname
},
body: "echo"
})
res.on('data', (chunk) => {
data += chunk;
});

assert(res.status == 200, `expected status code 200, got ${res.status}`);
data = await res.text()
assert(data == "echo", `did not get response from echo server through WS tunnel, got ${data}`);
res.on('close', () => { resolve([res.statusCode, data])});
});
req.end('echo');
}));

exposrCliTerminator();
await terminator(undefined, {gracefulTimeout: 10000, drainTimeout: 500});

assert(status == 200, `expected status code 200, got ${status}`);
assert(data == "echo", `did not get response from echo server through WS tunnel, got ${data}`);
}).timeout(60000);
});
});
30 changes: 22 additions & 8 deletions test/system/ingress/test_http_ingress.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,34 @@ describe('http ingress', () => {
});
});

res = await fetch("http://127.0.0.1:10000", {
method: "GET",
headers: {
Host: `${tunnel.id}.localhost.example`
}
let [status, data] = await new Promise((resolve) => {
const req = http.request({
hostname: 'localhost',
port: 10000,
method: 'GET',
path: '/',
headers: {
"Host": `${tunnel.id}.localhost.example`
}
}, (res) => {
let data = '';

res.on('data', (chunk) => {
data += chunk;
});

res.on('close', () => { resolve([res.statusCode, data])});
});
req.end('echo');
});

const data = await res.text();
assert(data == "AA", `did not get expected reply, got ${data}`);

await client.destroy();
await transport.destroy();
await sockPair.terminate();

assert(status == 200, `expected status code 200, got ${status}`);
assert(data == "AA", `did not get expected reply, got ${data}`);

}).timeout(2000);

it(`http ingress can handle websocket upgrades`, async () => {
Expand Down
7 changes: 6 additions & 1 deletion test/unit/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,16 @@ export const createEchoHttpServer = async (port: number = 20000, crtPath?: strin
server.on('request', handleRequest);
server.on('upgrade', handleUpgrade);

server.listen(port);
await new Promise((resolve) => {
server.listen(port, () => {
resolve(undefined);
});
});
return {
destroy: async () => {
await new Promise((resolve) => {
server.close(resolve);
server.closeAllConnections();
server.removeAllListeners('request');
server.removeAllListeners('upgrade');
});
Expand Down
Loading

0 comments on commit deeed7e

Please sign in to comment.