Skip to content

Commit

Permalink
chore: add http proxy test (#2433)
Browse files Browse the repository at this point in the history
  • Loading branch information
tripodsan authored Oct 3, 2024
1 parent 415ab7e commit 069c0f1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/fetch-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const httpProxyHandler = {
: new HttpProxyAgent(proxyUrl);
// eslint-disable-next-line no-console
console.debug(`using proxy ${proxyUrl}`);
// we need to use `node-fetch` directly to pass the agent. adobe-fetch does not support it.
return nodeFetch(url, {
...init,
agent,
Expand Down
73 changes: 73 additions & 0 deletions test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import os from 'os';
import assert from 'assert';
import fse from 'fs-extra';
import path from 'path';
import { h1NoCache } from '@adobe/fetch';
import * as http from 'node:http';
import { HelixProject } from '../src/server/HelixProject.js';
import {
Nock, assertHttp, createTestRoot, setupProject, rawGet,
Expand Down Expand Up @@ -261,6 +263,77 @@ describe('Helix Server', () => {
}
});

it('delivers from proxy (via http proxy).', async () => {
const cwd = await setupProject(path.join(__rootdir, 'test', 'fixtures', 'project'), testRoot);
const project = new HelixProject()
.withCwd(cwd)
.withHttpPort(0)
.withPrintIndex(true)
.withProxyUrl('http://main--foo--bar.hlx.page');

await project.init();
project.log.level = 'silly';

// create a http proxy server
process.env.ALL_PROXY = 'http://127.0.0.1:8002';
const proxyRequests = [];
const proxy = await new Promise((resolve) => {
const p = http
.createServer(async (req, res) => {
try {
// Delete accept header due to nock conflict
delete req.headers.accept;
console.log('http proxy request', req.url);

Check warning on line 286 in test/server.test.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
const resp = await h1NoCache().fetch(req.url, {});
console.log('http proxy response for', req.url, resp.status);

Check warning on line 288 in test/server.test.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
res.writeHead(resp.status, resp.headers.plain());
res.write(await resp.buffer());
res.end();
proxyRequests.push(req.url);
} catch (e) {
console.error('http proxy error:', e);

Check warning on line 294 in test/server.test.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
res.writeHead(500);
res.end();
}
})
.listen(8002, '127.0.0.1', () => {
console.log('http proxy server listening on port 8002');

Check warning on line 300 in test/server.test.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
resolve(p);
});
});

nock('http://main--foo--bar.hlx.page')
.get('/readme.html')
.reply(200, 'hello readme', {
'content-type': 'text/html',
'content-security-policy': 'self;',
})
.get('/head.html')
.reply(200, '<link rel="stylesheet" href="/styles.css"/>', {
'content-type': 'text/html',
});

try {
await project.start();
const resp = await getFetch()(`http://127.0.0.1:${project.server.port}/readme.html`, {
cache: 'no-store',
});
const ret = await resp.text();
assert.strictEqual(resp.status, 200);
assert.strictEqual(ret.trim(), 'hello readme');
assert.strictEqual(resp.headers.get('access-control-allow-origin'), '*');
assert.strictEqual(resp.headers.get('content-security-policy'), null);
assert.strictEqual(resp.headers.get('via'), '1.0 main--foo--bar.hlx.page');

// ensure that request went through http proxy
assert.deepStrictEqual(proxyRequests, [`http://127.0.0.1:${project.server.port}/readme.html`]);
} finally {
proxy.close();
await project.stop();
delete process.env.ALL_PROXY;
}
});

it('delivers from proxy (with head).', async () => {
const cwd = await setupProject(path.join(__rootdir, 'test', 'fixtures', 'project'), testRoot);
const project = new HelixProject()
Expand Down

0 comments on commit 069c0f1

Please sign in to comment.