Skip to content

Commit f9603f3

Browse files
committed
Mention pool usage
1 parent 968a19a commit f9603f3

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

docs/proxy.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Connecting through a proxy
22

3-
Conneting through a proxy is possible by properly configuring the client constructor and request.
3+
Conneting through a proxy is possible by properly configuring the `Client` or `Pool` constructor and request.
44

5-
The proxy url should be passed to the client constructor, while the upstream server url
5+
The proxy url should be passed to the `Client` or `Pool` constructor, while the upstream server url
66
should be added to every request call in the `path`.
77
For instance, if you need to send a request to the `/hello` route of your upstream server,
88
the `path` should be `path: 'http://upstream.server:port/hello?foo=bar'`.
@@ -14,7 +14,7 @@ If you proxy requires basic authentication, you can send it via the `proxy-autho
1414
```js
1515
import { Client } from 'undici'
1616
import { createServer } from 'http'
17-
import proxy = from 'proxy'
17+
import proxy from 'proxy'
1818

1919
const server = await buildServer()
2020
const proxy = await buildProxy()
@@ -67,7 +67,7 @@ function buildProxy () {
6767
```js
6868
import { Client } from 'undici'
6969
import { createServer } from 'http'
70-
import proxy = from 'proxy'
70+
import proxy from 'proxy'
7171

7272
const server = await buildServer()
7373
const proxy = await buildProxy()

test/proxy.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { test } = require('tap')
4-
const { Client } = require('..')
4+
const { Client, Pool } = require('..')
55
const { createServer } = require('http')
66
const proxy = require('proxy')
77

@@ -82,6 +82,41 @@ test('connect through proxy with auth', async (t) => {
8282
client.close()
8383
})
8484

85+
test('connect through proxy (with pool)', async (t) => {
86+
t.plan(3)
87+
88+
const server = await buildServer()
89+
const proxy = await buildProxy()
90+
91+
const serverUrl = `http://localhost:${server.address().port}`
92+
const proxyUrl = `http://localhost:${proxy.address().port}`
93+
94+
server.on('request', (req, res) => {
95+
t.strictEqual(req.url, '/hello?foo=bar')
96+
res.setHeader('content-type', 'application/json')
97+
res.end(JSON.stringify({ hello: 'world' }))
98+
})
99+
100+
const pool = new Pool(proxyUrl)
101+
102+
const response = await pool.request({
103+
method: 'GET',
104+
path: serverUrl + '/hello?foo=bar'
105+
})
106+
107+
response.body.setEncoding('utf8')
108+
let data = ''
109+
for await (const chunk of response.body) {
110+
data += chunk
111+
}
112+
t.strictEqual(response.statusCode, 200)
113+
t.deepEqual(JSON.parse(data), { hello: 'world' })
114+
115+
server.close()
116+
proxy.close()
117+
pool.close()
118+
})
119+
85120
function buildServer () {
86121
return new Promise((resolve, reject) => {
87122
const server = createServer()

0 commit comments

Comments
 (0)