Skip to content

Commit

Permalink
test: remove http-proxy package (#3101)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Jan 30, 2025
1 parent df83b16 commit 732ba38
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 179 deletions.
125 changes: 25 additions & 100 deletions src/Playwright.TestingHarnessTest/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/Playwright.TestingHarnessTest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"private": true,
"devDependencies": {
"@playwright/test": "^1.48.2",
"@types/http-proxy": "^1.17.15",
"fast-xml-parser": "^4.5.0",
"http-proxy": "^1.18.1"
"@types/node": "^22.12.0",
"fast-xml-parser": "^4.5.0"
}
}
47 changes: 47 additions & 0 deletions src/Playwright.TestingHarnessTest/tests/baseTest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from 'fs';
import http from 'http';
import path from 'path';
import childProcess from 'child_process';
import { test as base } from '@playwright/test';
import { XMLParser } from 'fast-xml-parser';
import { AddressInfo } from 'net';

type RunResult = {
command: string;
Expand All @@ -16,9 +18,16 @@ type RunResult = {
}

export const test = base.extend<{
proxyServer: ProxyServer;
testMode: 'nunit' | 'mstest' | 'xunit';
runTest: (files: Record<string, string>, command: string, env?: NodeJS.ProcessEnv) => Promise<RunResult>;
}>({
proxyServer: async ({}, use) => {
const proxyServer = new ProxyServer();
await proxyServer.listen();
await use(proxyServer);
await proxyServer.stop();
},
testMode: null,
runTest: async ({ testMode }, use, testInfo) => {
const testResults: RunResult[] = [];
Expand Down Expand Up @@ -127,4 +136,42 @@ function extractVstestMessages(stdout: string, prefix: string): string {
return out;
}

class ProxyServer {
private _server: http.Server;
public requests: { url: string, auth: string}[] = [];

constructor() {
this._server = http.createServer(this.handler.bind(this));
}

handler(req: http.IncomingMessage, res: http.ServerResponse) {
if (req.url.includes('google.com')) // Telemetry requests.
return;
if (!req.headers['proxy-authorization']) {
res.writeHead(407, { 'Proxy-Authenticate': 'Basic realm="Access to internal site"' });
res.end();
return;
}
const auth = Buffer.from(req.headers['proxy-authorization'].split(' ')[1], 'base64').toString();
this.requests.push({
url: req.url,
auth: auth,
})
res.writeHead(200);
res.end('OK');
}

listenAddr() {
return `http://127.0.0.1:${(this._server.address() as AddressInfo).port}`;
}

async listen() {
await new Promise<void>(resolve => this._server.listen(0, resolve));
}

async stop() {
await new Promise<void>(resolve => this._server.close(() => resolve()));
}
}

export { expect } from '@playwright/test';
30 changes: 3 additions & 27 deletions src/Playwright.TestingHarnessTest/tests/mstest/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import http from 'http';
import { test, expect } from '../baseTest';
import httpProxy from 'http-proxy';

test.use({ testMode: 'mstest' });

Expand Down Expand Up @@ -225,27 +224,7 @@ test('should be able to parse BrowserName and LaunchOptions.Headless from runset
expect(result.stdout).not.toContain("Headless")
});

test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest }) => {
const httpServer = http.createServer((req, res) => {
res.end('hello world!')
}).listen(3129);
const proxyServer = httpProxy.createProxyServer({
auth: 'user:pwd',
target: 'http://localhost:3129',
}).listen(3128);

const waitForProxyRequest = new Promise<[string, string]>((resolve) => {
proxyServer.on('proxyReq', (proxyReq, req, res, options) => {
if (req.url.includes('google.com')) // Telemetry requests.
{
return;
}
const authHeader = proxyReq.getHeader('authorization') as string;
const auth = Buffer.from(authHeader.split(' ')[1], 'base64').toString();
resolve([req.url, auth]);
});
})

test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ runTest, proxyServer }) => {
const result = await runTest({
'ExampleTests.cs': `
using System;
Expand Down Expand Up @@ -273,7 +252,7 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru
<LaunchOptions>
<Headless>false</Headless>
<Proxy>
<Server>http://127.0.0.1:3128</Server>
<Server>${proxyServer.listenAddr()}</Server>
<Username>user</Username>
<Password>pwd</Password>
</Proxy>
Expand All @@ -288,12 +267,9 @@ test('should be able to parse LaunchOptions.Proxy from runsettings', async ({ ru

expect(result.stdout).not.toContain("Headless");

const [url, auth] = await waitForProxyRequest;
const { url, auth } = proxyServer.requests.find(r => r.url === 'http://example.com/')!;;
expect(url).toBe('http://example.com/');
expect(auth).toBe('user:pwd');

proxyServer.close();
httpServer.close();
});

test('should be able to parse LaunchOptions.Args from runsettings', async ({ runTest }) => {
Expand Down
Loading

0 comments on commit 732ba38

Please sign in to comment.