Skip to content

Commit

Permalink
fix(docs): update dependency urllib to v3 (#187)
Browse files Browse the repository at this point in the history
* chore(npm): update dependency urllib to v3

* fix: update urllib to v3

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: 3846masa <3846masahiro+git@gmail.com>
  • Loading branch information
renovate[bot] and 3846masa committed Jul 23, 2022
1 parent b4096e7 commit f7f7332
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 514 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,15 @@ await client.get('https://example.com');
#### `urllib`

```js
import urllib from 'urllib';
import { request, setGlobalDispatcher } from 'urllib';
import { CookieJar } from 'tough-cookie';
import { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';
import { CookieClient } from 'http-cookie-agent/undici';

const jar = new CookieJar();
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

const client = urllib.create({
agent: new HttpCookieAgent({ cookies: { jar } }),
httpsAgent: new HttpsCookieAgent({ cookies: { jar } }),
});

await client.request('https://example.com');
await request('https://example.com');
```

### Using with an asynchronous Cookie store
Expand Down
11 changes: 5 additions & 6 deletions examples/urllib/basic.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';
import { CookieAgent } from 'http-cookie-agent/undici';
import { CookieJar } from 'tough-cookie';
import urllib from 'urllib';
import { request, setGlobalDispatcher } from 'urllib';

const jar = new CookieJar();
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

await urllib.request('https://httpbin.org/cookies/set/session/userid', {
agent: new HttpCookieAgent({ cookies: { jar } }),
httpsAgent: new HttpsCookieAgent({ cookies: { jar } }),
});
await request('https://httpbin.org/cookies/set/session/userid');

const cookies = await jar.getCookies('https://httpbin.org');
console.log(cookies);
15 changes: 0 additions & 15 deletions examples/urllib/instance.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"tough-cookie": "4.0.0",
"typescript": "4.7.4",
"undici": "5.8.0",
"urllib": "2.38.1"
"urllib": "3.0.4"
},
"peerDependencies": {
"deasync": "^0.1.26",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import test from 'ava';
import { CookieJar } from 'tough-cookie';
import urllib from 'urllib';
import { request, setGlobalDispatcher } from 'urllib';

import { createTestServer, readStream } from '../../__tests__/helpers.mjs';
import { HttpCookieAgent } from '../index.js';
import { CookieAgent } from '../index.js';

test('should set cookies to CookieJar from Set-Cookie header', async (t) => {
test.serial('should set cookies to CookieJar from Set-Cookie header', async (t) => {
const jar = new CookieJar();
const agent = new HttpCookieAgent({ cookies: { jar } });
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

const { port } = await createTestServer([
(_req, res) => {
Expand All @@ -16,9 +17,7 @@ test('should set cookies to CookieJar from Set-Cookie header', async (t) => {
},
]);

await urllib.request(`http://localhost:${port}`, {
agent,
});
await request(`http://localhost:${port}`);

const cookies = await jar.getCookies(`http://localhost:${port}`);
t.is(cookies.length, 1);
Expand All @@ -27,9 +26,10 @@ test('should set cookies to CookieJar from Set-Cookie header', async (t) => {
t.plan(2);
});

test('should set cookies to CookieJar from multiple Set-Cookie headers', async (t) => {
test.serial('should set cookies to CookieJar from multiple Set-Cookie headers', async (t) => {
const jar = new CookieJar();
const agent = new HttpCookieAgent({ cookies: { jar } });
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

const { port } = await createTestServer([
(_req, res) => {
Expand All @@ -38,9 +38,7 @@ test('should set cookies to CookieJar from multiple Set-Cookie headers', async (
},
]);

await urllib.request(`http://localhost:${port}`, {
agent,
});
await request(`http://localhost:${port}`);

const cookies = await jar.getCookies(`http://localhost:${port}`);
t.is(cookies.length, 2);
Expand All @@ -50,9 +48,10 @@ test('should set cookies to CookieJar from multiple Set-Cookie headers', async (
t.plan(3);
});

test('should send cookies from CookieJar', async (t) => {
test.serial('should send cookies from CookieJar', async (t) => {
const jar = new CookieJar();
const agent = new HttpCookieAgent({ cookies: { jar } });
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

const { port } = await createTestServer([
(req, res) => {
Expand All @@ -63,16 +62,15 @@ test('should send cookies from CookieJar', async (t) => {

await jar.setCookie('key=value', `http://localhost:${port}`);

await urllib.request(`http://localhost:${port}`, {
agent,
});
await request(`http://localhost:${port}`);

t.plan(1);
});

test('should send cookies from both a request options and CookieJar', async (t) => {
test.serial('should send cookies from both a request options and CookieJar', async (t) => {
const jar = new CookieJar();
const agent = new HttpCookieAgent({ cookies: { jar } });
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

const { port } = await createTestServer([
(req, res) => {
Expand All @@ -83,38 +81,41 @@ test('should send cookies from both a request options and CookieJar', async (t)

await jar.setCookie('key1=value1', `http://localhost:${port}`);

await urllib.request(`http://localhost:${port}`, {
agent,
await request(`http://localhost:${port}`, {
headers: { Cookie: 'key2=value2' },
});

t.plan(1);
});

test('should send cookies from a request options when the key is duplicated in both a request options and CookieJar', async (t) => {
const jar = new CookieJar();
const agent = new HttpCookieAgent({ cookies: { jar } });
test.serial(
'should send cookies from a request options when the key is duplicated in both a request options and CookieJar',
async (t) => {
const jar = new CookieJar();
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

const { port } = await createTestServer([
(req, res) => {
t.is(req.headers['cookie'], 'key=expected');
res.end();
},
]);
const { port } = await createTestServer([
(req, res) => {
t.is(req.headers['cookie'], 'key=expected');
res.end();
},
]);

await jar.setCookie('key=notexpected', `http://localhost:${port}`);
await jar.setCookie('key=notexpected', `http://localhost:${port}`);

await urllib.request(`http://localhost:${port}`, {
agent,
headers: { Cookie: 'key=expected' },
});
await request(`http://localhost:${port}`, {
headers: { Cookie: 'key=expected' },
});

t.plan(1);
});
t.plan(1);
},
);

test('should send cookies from the first response when redirecting', async (t) => {
test.serial('should send cookies from the first response when redirecting', async (t) => {
const jar = new CookieJar();
const agent = new HttpCookieAgent({ cookies: { jar } });
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

const { port } = await createTestServer([
(_req, res) => {
Expand All @@ -129,20 +130,20 @@ test('should send cookies from the first response when redirecting', async (t) =
},
]);

await urllib.request(`http://localhost:${port}`, {
agent,
await request(`http://localhost:${port}`, {
followRedirect: true,
});

t.plan(1);
});

test('should emit error when CookieJar#getCookies throws error.', async (t) => {
test.serial('should emit error when CookieJar#getCookies throws error.', async (t) => {
const jar = new CookieJar();
jar.getCookiesSync = () => {
throw new Error('Error');
};
const agent = new HttpCookieAgent({ cookies: { jar } });
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

const { port } = await createTestServer([
(_req, res) => {
Expand All @@ -152,20 +153,19 @@ test('should emit error when CookieJar#getCookies throws error.', async (t) => {
]);

await t.throwsAsync(() => {
return urllib.request(`http://localhost:${port}`, {
agent,
});
return request(`http://localhost:${port}`);
});

t.plan(1);
});

test('should emit error when CookieJar#setCookie throws error.', async (t) => {
test.serial('should emit error when CookieJar#setCookie throws error.', async (t) => {
const jar = new CookieJar();
jar.setCookieSync = () => {
throw new Error('Error');
};
const agent = new HttpCookieAgent({ cookies: { jar } });
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

const { port } = await createTestServer([
(_req, res) => {
Expand All @@ -175,19 +175,18 @@ test('should emit error when CookieJar#setCookie throws error.', async (t) => {
]);

await t.throwsAsync(() => {
return urllib.request(`http://localhost:${port}`, {
agent,
});
return request(`http://localhost:${port}`);
});

t.plan(1);
});

test('should send post data when keepalive is enabled', async (t) => {
test.serial('should send post data when keepalive is enabled', async (t) => {
const times = 2;

const jar = new CookieJar();
const agent = new HttpCookieAgent({ cookies: { jar }, keepAlive: true });
const agent = new CookieAgent({ cookies: { jar } });
setGlobalDispatcher(agent);

const { port } = await createTestServer(
Array.from({ length: times }, (_, idx) => {
Expand All @@ -202,8 +201,7 @@ test('should send post data when keepalive is enabled', async (t) => {
await jar.setCookie('key=expected', `http://localhost:${port}`);

for (let idx = 0; idx < times; idx++) {
await urllib.request(`http://localhost:${port}`, {
agent,
await request(`http://localhost:${port}`, {
data: `{ "index": "${idx}" }`,
method: 'POST',
});
Expand Down

0 comments on commit f7f7332

Please sign in to comment.