Skip to content

Commit

Permalink
Add https-proxy-server E2E test
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Apr 29, 2023
1 parent de70a45 commit 81438eb
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -5,6 +5,7 @@
"build": "turbo run build",
"lint": "turbo run lint",
"test": "turbo run test",
"test-e2e": "turbo run test-e2e",
"format": "prettier --write \"**/*.{ts,tsx,md}\""
},
"devDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions packages/agent-base/src/helpers.ts
Expand Up @@ -12,14 +12,15 @@ export async function toBuffer(stream: Readable): Promise<Buffer> {
return Buffer.concat(chunks, length);
}

export async function json(stream: Readable): Promise<Record<string, string>> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function json(stream: Readable): Promise<any> {
const buf = await toBuffer(stream);
return JSON.parse(buf.toString('utf8'));
}

export function req(
url: string | URL,
opts: https.RequestOptions
opts: https.RequestOptions = {}
): Promise<http.IncomingMessage> {
return new Promise((resolve, reject) => {
const href = typeof url === 'string' ? url : url.href;
Expand Down
3 changes: 2 additions & 1 deletion packages/https-proxy-agent/package.json
Expand Up @@ -9,7 +9,8 @@
],
"scripts": {
"build": "tsc",
"test": "jest --env node --verbose --bail",
"test": "jest --env node --verbose --bail test/test.ts",
"test-e2e": "jest --env node --verbose --bail test/e2e.test.ts",
"lint": "eslint src --ext .js,.ts",
"prepublishOnly": "npm run build"
},
Expand Down
63 changes: 63 additions & 0 deletions packages/https-proxy-agent/test/e2e.test.ts
@@ -0,0 +1,63 @@
import { req, json } from 'agent-base';
import { HttpsProxyAgent } from '../src';

interface NordVPNServer {
name: string;
domain: string;
flag: string;
features: { [key: string]: boolean };
}

jest.setTimeout(30000);

async function findNordVpnServer(): Promise<NordVPNServer> {
const res = await req('https://nordvpn.com/api/server');
const body = await json(res);
const server = (body as NordVPNServer[]).find((s) => s.features.proxy_ssl);
if (!server) {
throw new Error('Could not find `https` proxy server from NordVPN');
}
return server;
}

async function getRealIP(): Promise<string> {
const res = await req('https://dump.n8.io');
const body = await json(res);
return body.request.headers['x-real-ip'];
}

describe('HttpsProxyAgent', () => {
it('should work over NordVPN proxy', async () => {
const { NORDVPN_USERNAME, NORDVPN_PASSWORD } = process.env;
if (!NORDVPN_USERNAME) {
throw new Error('`NORDVPN_USERNAME` env var is not defined');
}
if (!NORDVPN_PASSWORD) {
throw new Error('`NORDVPN_PASSWORD` env var is not defined');
}

const [realIp, server] = await Promise.all([
getRealIP(),
findNordVpnServer(),
]);
console.log(
`Using NordVPN HTTPS proxy server: ${server.name} (${server.domain})`
);

const username = encodeURIComponent(NORDVPN_USERNAME);
const password = encodeURIComponent(NORDVPN_PASSWORD);

// NordVPN runs their HTTPS proxy servers on port 89
// https://www.reddit.com/r/nordvpn/comments/hvz48h/nordvpn_https_proxy/
const agent = new HttpsProxyAgent(
`https://${username}:${password}@${server.domain}:89`
);

const res = await req('https://dump.n8.io', { agent });
const body = await json(res);
expect(body.request.headers['x-real-ip']).not.toEqual(realIp);
expect(body.request.headers['x-vercel-ip-country']).toEqual(
server.flag
);
});
});
2 changes: 1 addition & 1 deletion packages/https-proxy-agent/test/tsconfig.json
@@ -1,4 +1,4 @@
{
"extends": "../tsconfig.json",
"include": ["test.ts"]
"include": ["*.ts"]
}
11 changes: 10 additions & 1 deletion turbo.json
@@ -1,6 +1,12 @@
{
"$schema": "https://turbo.build/schema.json",
"globalEnv": ["HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"],
"globalEnv": [
"HTTP_PROXY",
"HTTPS_PROXY",
"NO_PROXY",
"NORDVPN_USERNAME",
"NORDVPN_PASSWORD"
],
"pipeline": {
"build": {
"dependsOn": ["^build"],
Expand All @@ -9,6 +15,9 @@
"test": {
"dependsOn": ["build"]
},
"test-e2e": {
"dependsOn": ["build"]
},
"lint": {}
}
}

0 comments on commit 81438eb

Please sign in to comment.