Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added node-fetch support #5

Merged
merged 2 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ needle('get', 'http://localhost:9200', {
})
```

### [node-fetch](https://github.com/node-fetch/node-fetch)

```js
fetch('http://localhost:9200', {
agent: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: 'http://localhost:8080'
})
})
```

## License

This software is licensed under the [MIT](./LICENSE).
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"ava": "^3.10.1",
"got": "^11.5.1",
"needle": "^2.5.0",
"node-fetch": "^2.6.0",
"proxy": "^1.0.2",
"standard": "^14.3.4",
"tsd": "^0.13.1"
Expand Down
103 changes: 103 additions & 0 deletions test/node-fetch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
'use strict'

const fetch = require('node-fetch')
const test = require('ava')
const {
createServer,
createSecureServer,
createProxy,
createSecureProxy
} = require('./utils')
const { HttpProxyAgent, HttpsProxyAgent } = require('../')

test('http to http', async t => {
const server = await createServer()
const proxy = await createProxy()
server.on('request', (req, res) => res.end('ok'))

const response = await fetch(`http://${server.address().address}:${server.address().port}`, {
agent: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
})

t.is(await response.text(), 'ok')
t.is(response.status, 200)

server.close()
proxy.close()
})

test('https to http', async t => {
const server = await createServer()
const proxy = await createSecureProxy()
server.on('request', (req, res) => res.end('ok'))

const response = await fetch(`http://${server.address().address}:${server.address().port}`, {
agent: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `https://${proxy.address().address}:${proxy.address().port}`
})
})

t.is(await response.text(), 'ok')
t.is(response.status, 200)

server.close()
proxy.close()
})

test('http to https', async t => {
const server = await createSecureServer()
const proxy = await createProxy()
server.on('request', (req, res) => res.end('ok'))

const response = await fetch(`https://${server.address().address}:${server.address().port}`, {
agent: new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
})

t.is(await response.text(), 'ok')
t.is(response.status, 200)

server.close()
proxy.close()
})

test('https to https', async t => {
const server = await createSecureServer()
const proxy = await createSecureProxy()
server.on('request', (req, res) => res.end('ok'))

const response = await fetch(`https://${server.address().address}:${server.address().port}`, {
agent: new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `https://${proxy.address().address}:${proxy.address().port}`
})
})

t.is(await response.text(), 'ok')
t.is(response.status, 200)

server.close()
proxy.close()
})