Skip to content

Commit

Permalink
Make sure it's compatible with tough-cookie@4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Apr 15, 2020
1 parent bddf707 commit 407d597
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -66,7 +66,7 @@
"@types/node-fetch": "^2.5.5",
"@types/request": "^2.48.4",
"@types/sinon": "^9.0.0",
"@types/tough-cookie": "^2.3.5",
"@types/tough-cookie": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^2.27.0",
"@typescript-eslint/parser": "^2.27.0",
"ava": "^3.6.0",
Expand All @@ -89,7 +89,7 @@
"slow-stream": "0.0.4",
"tempy": "^0.5.0",
"to-readable-stream": "^2.1.0",
"tough-cookie": "^3.0.0",
"tough-cookie": "^4.0.0",
"typescript": "3.7.5",
"xo": "^0.29.0"
},
Expand Down
9 changes: 4 additions & 5 deletions source/core/index.ts
Expand Up @@ -720,14 +720,13 @@ export default class Request extends Duplex implements RequestEvents<Request> {
if (options.cookieJar) {
let {setCookie, getCookieString} = options.cookieJar;

// Horrible `tough-cookie` check
assert.function_(setCookie);
assert.function_(getCookieString);

/* istanbul ignore next: Horrible `tough-cookie` v3 check */
if (setCookie.length === 4 && getCookieString.length === 0) {
setCookie = promisify(setCookie.bind(options.cookieJar));
getCookieString = promisify(getCookieString.bind(options.cookieJar));
} else if (setCookie.length !== 2) {
throw new TypeError('`options.cookieJar.setCookie` needs to be an async function with 2 arguments');
} else if (getCookieString.length !== 1) {
throw new TypeError('`options.cookieJar.getCookieString` needs to be an async function with 1 argument');
}

options.cookieJar = {setCookie, getCookieString};
Expand Down
10 changes: 5 additions & 5 deletions test/cookies.ts
Expand Up @@ -178,19 +178,19 @@ test('throws on invalid `options.cookieJar.setCookie`', async t => {
// @ts-ignore Error tests
await t.throwsAsync(got('https://example.com', {
cookieJar: {
setCookie: () => {}
setCookie: 123
}
}), {message: '`options.cookieJar.setCookie` needs to be an async function with 2 arguments'});
}), {message: 'Expected value which is `Function`, received value of type `number`.'});
});

test('throws on invalid `options.cookieJar.getCookieString`', async t => {
// @ts-ignore Error tests
await t.throwsAsync(got('https://example.com', {
cookieJar: {
setCookie: async (_rawCookie: string, _url: string) => {},
getCookieString: () => {}
setCookie: async () => {},
getCookieString: 123
}
}), {message: '`options.cookieJar.getCookieString` needs to be an async function with 1 argument'});
}), {message: 'Expected value which is `Function`, received value of type `number`.'});
});

test('cookies are cleared when redirecting to a different hostname (no cookieJar)', withServer, async (t, server, got) => {
Expand Down

0 comments on commit 407d597

Please sign in to comment.