-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhttp.spec.ts
More file actions
106 lines (85 loc) · 3.54 KB
/
http.spec.ts
File metadata and controls
106 lines (85 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { test, describe, before } from 'node:test';
import assert from 'node:assert';
import { request, getProfiles } from '../node-wreq';
describe('HTTP', () => {
before(() => {
console.log('🔌 HTTP Test Suite\n');
});
test('should return available browser profiles', () => {
const profiles = getProfiles();
assert.ok(Array.isArray(profiles), 'Profiles should be an array');
assert.ok(profiles.length > 0, 'Should have at least one profile');
assert.ok(
profiles.some((p) => p.includes('chrome')) ||
profiles.some((p) => p.includes('firefox')) ||
profiles.some((p) => p.includes('safari')),
'Should include standard browser profiles'
);
console.log('Available profiles:', profiles.join(', '));
});
test('should make a simple GET request', async () => {
const response = await request({
url: 'https://httpbin.org/get',
browser: 'chrome_131',
timeout: 10000,
});
assert.ok(response.status >= 200 && response.status < 300, 'Should return successful status');
assert.ok(Object.keys(response.headers).length > 0, 'Should have response headers');
assert.ok(response.body.length > 0, 'Should have response body');
const body = JSON.parse(response.body);
assert.ok(body.headers['User-Agent'], 'Should have User-Agent header');
console.log('Status:', response.status);
console.log('User-Agent:', body.headers['User-Agent']);
});
test('should work with different browser profiles', async () => {
const testUrl = 'https://httpbin.org/user-agent';
const browsers = ['chrome_137', 'firefox_139', 'safari_18'];
for (const browser of browsers) {
const response = await request({
url: testUrl,
browser: browser as any,
timeout: 10000,
});
assert.ok(response.status === 200, `${browser} should return status 200`);
const data = JSON.parse(response.body);
assert.ok(data['user-agent'], `${browser} should have user-agent`);
console.log(`${browser}:`, data['user-agent'].substring(0, 70) + '...');
}
});
test('should handle timeout errors', async () => {
await assert.rejects(
async () => {
await request({
url: 'https://httpbin.org/delay/10',
browser: 'chrome_137',
timeout: 1000, // 1 second timeout for 10 second delay
});
},
{
name: 'RequestError',
},
'Should throw an error on timeout'
);
});
test('should capture multiple cookies from Set-Cookie headers', async () => {
// httpbin.org/cookies/set allows us to set multiple cookies
const response = await request({
url: 'https://httpbin.org/cookies/set?session=abc123&csrf_token=xyz789&user_pref=dark',
browser: 'chrome_131',
timeout: 10000,
});
assert.ok(response.status >= 200 && response.status < 400, 'Should return successful status');
assert.ok(typeof response.cookies === 'object', 'Should have cookies object');
// The server should set multiple cookies
const cookieKeys = Object.keys(response.cookies);
console.log('Captured cookies:', response.cookies);
assert.ok(cookieKeys.length > 0, 'Should capture at least one cookie');
// Check if multiple cookies were captured
// httpbin redirects after setting cookies, so we should have the cookies in the response
if (cookieKeys.length > 1) {
console.log('✓ Multiple cookies captured successfully:', cookieKeys.join(', '));
} else {
console.log('Note: Only one cookie captured. Cookie count:', cookieKeys.length);
}
});
});