stock-bot/test-proxy-auth.ts
2025-06-13 13:38:02 -04:00

156 lines
4.7 KiB
TypeScript

/**
* Test Playwright proxy authentication specifically
*/
import { Browser } from '@stock-bot/browser';
async function testPlaywrightProxyAuth() {
console.log('🔐 Testing Playwright Proxy Authentication...');
try {
await Browser.initialize({
headless: true,
timeout: 15000,
blockResources: false,
});
console.log('✅ Browser initialized');
// Test 1: Without proxy
console.log('\n📍 Test 1: Without proxy');
const { page: page1, contextId: ctx1 } = await Browser.createPageWithProxy(
'https://httpbin.org/ip'
);
let events1 = 0;
let ip1 = null;
page1.onNetworkEvent(event => {
events1++;
console.log(` 📡 Event: ${event.type} ${event.url}`);
if (event.type === 'response' && event.url.includes('/ip') && event.responseData) {
ip1 = JSON.parse(event.responseData).origin;
console.log(` 🌐 Your IP: ${ip1}`);
}
});
await page1.waitForLoadState('domcontentloaded');
await new Promise(resolve => setTimeout(resolve, 2000));
await Browser.closeContext(ctx1);
console.log(` Events captured: ${events1}`);
// Test 2: With proxy using new authentication method
console.log('\n🔒 Test 2: With proxy (new auth method)');
const { page: page2, contextId: ctx2 } = await Browser.createPageWithProxy(
'https://httpbin.org/ip',
'http://doimvbnb-US-rotate:w5fpiwrb9895@p.webshare.io:80'
);
let events2 = 0;
let ip2 = null;
page2.onNetworkEvent(event => {
events2++;
console.log(` 📡 Event: ${event.type} ${event.url}`);
if (event.type === 'response' && event.url.includes('/ip') && event.responseData) {
ip2 = JSON.parse(event.responseData).origin;
console.log(` 🔄 Proxy IP: ${ip2}`);
}
});
await page2.waitForLoadState('domcontentloaded');
await new Promise(resolve => setTimeout(resolve, 2000));
await Browser.closeContext(ctx2);
console.log(` Events captured: ${events2}`);
// Results
console.log('\n📊 Results:');
console.log(` 🌐 Direct IP: ${ip1 || 'Not captured'}`);
console.log(` 🔄 Proxy IP: ${ip2 || 'Not captured'}`);
console.log(` 📡 Direct events: ${events1}`);
console.log(` 📡 Proxy events: ${events2}`);
if (ip1 && ip2 && ip1 !== ip2) {
console.log('✅ Proxy authentication is working - different IPs detected!');
return true;
} else if (events1 > 0 || events2 > 0) {
console.log('⚠️ Network monitoring working, but proxy may not be changing IP');
return true;
} else {
console.log('❌ No network events captured');
return false;
}
} catch (error) {
console.error('❌ Test failed:', error);
return false;
} finally {
await Browser.close();
}
}
async function testManualPageEvaluation() {
console.log('\n🧪 Test 3: Manual page evaluation (without network monitoring)');
try {
await Browser.initialize({
headless: true,
timeout: 10000,
blockResources: false,
});
const { page, contextId } = await Browser.createPageWithProxy(
'https://httpbin.org/ip',
'http://doimvbnb-US-rotate:w5fpiwrb9895@p.webshare.io:80'
);
// Try to get the page content directly
const title = await page.title();
console.log(` 📄 Page title: "${title}"`);
// Try to evaluate some JavaScript
const result = await page.evaluate(() => {
return {
url: window.location.href,
userAgent: navigator.userAgent.substring(0, 50),
readyState: document.readyState,
};
});
console.log(` 🔍 Page info:`, result);
// Try to get page content
const bodyText = await page.locator('body').textContent();
if (bodyText) {
console.log(` 📝 Body content (first 200 chars): ${bodyText.substring(0, 200)}...`);
// Check if it looks like an IP response
if (bodyText.includes('origin')) {
console.log(' ✅ Looks like httpbin.org response!');
}
}
await Browser.closeContext(contextId);
return true;
} catch (error) {
console.error(' ❌ Manual evaluation failed:', error);
return false;
} finally {
await Browser.close();
}
}
async function runProxyTests() {
console.log('🚀 Playwright Proxy Authentication Tests\n');
const authResult = await testPlaywrightProxyAuth();
const manualResult = await testManualPageEvaluation();
console.log(`\n🏁 Final Results:`);
console.log(` 🔐 Proxy auth test: ${authResult ? '✅ PASS' : '❌ FAIL'}`);
console.log(` 🧪 Manual eval test: ${manualResult ? '✅ PASS' : '❌ FAIL'}`);
}
if (import.meta.main) {
runProxyTests().catch(console.error);
}
export { testPlaywrightProxyAuth, testManualPageEvaluation };