-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathrequest.test.ts
More file actions
64 lines (55 loc) · 1.74 KB
/
request.test.ts
File metadata and controls
64 lines (55 loc) · 1.74 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
import { createProxy } from '@slowreader/proxy'
import { deepEqual, equal } from 'node:assert/strict'
import { createServer, type Server } from 'node:http'
import type { AddressInfo } from 'node:net'
import { after, describe, test } from 'node:test'
import { proxyDebug, request, setProxyAsRequestMethod } from '../request.ts'
function getURL(server: Server): string {
let port = (server.address() as AddressInfo).port
return `http://localhost:${port}`
}
let target = createServer((req, res) => {
res.writeHead(200, {
'Content-Type': 'text/plain',
'X-Custom': 'value'
})
res.end('hello')
})
await new Promise<void>(resolve => target.listen(0, resolve))
let proxy = createServer(
createProxy({
allowLocalhost: true,
allowsFrom: '^http://test\\.app',
bodyTimeout: 10000,
maxSize: 10 * 1024 * 1024,
requestTimeout: 10000
})
)
await new Promise<void>(resolve => proxy.listen(0, resolve))
let proxyUrl = getURL(proxy)
let targetUrl = getURL(target)
after(() => {
target.close()
proxy.close()
})
let origin = { Origin: 'http://test.app' }
describe('proxy', () => {
test('routes requests through proxy', async () => {
setProxyAsRequestMethod(proxyUrl + '/')
let response = await request(targetUrl, { headers: origin })
equal(response.status, 200)
equal(await response.text(), 'hello')
equal(response.url, targetUrl)
})
test('sends debug headers when proxyDebug is set', async () => {
setProxyAsRequestMethod(proxyUrl + '/')
let captured: Headers
proxyDebug.set(headers => {
captured = headers
})
let response = await request(targetUrl, { headers: origin })
equal(response.status, 200)
deepEqual(captured!.get('content-type'), 'text/plain')
proxyDebug.set(false)
})
})