Skip to content

Commit

Permalink
Add option to ignore invalid cookies
Browse files Browse the repository at this point in the history
When options.ignoreInvalidCookies is set, ignore invalid cookies
instead of throwing an error
  • Loading branch information
iczero committed Jun 30, 2019
1 parent 3bb5aa7 commit a48cef9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -243,6 +243,13 @@ Type: [`tough.CookieJar` instance](https://github.com/salesforce/tough-cookie#co

Cookie support. You don't have to care about parsing or how to store them. [Example.](#cookies)

###### ignoreInvalidCookies

Type: `boolean`<br>
Default: `false`

When set to `true`, silently ignore invalid cookies instead of throwing an error. Only useful when the `cookieJar` option has been set.

###### encoding

Type: `string | null`<br>
Expand Down
7 changes: 6 additions & 1 deletion source/request-as-event-emitter.ts
Expand Up @@ -130,7 +130,12 @@ export default (options: NormalizedOptions, input?: TransformStream) => {

const rawCookies = typedResponse.headers['set-cookie'];
if (options.cookieJar && rawCookies) {
await Promise.all(rawCookies.map((rawCookie: string) => setCookie!(rawCookie, typedResponse.url!)));
let promises = rawCookies.map((rawCookie: string) => setCookie!(rawCookie, typedResponse.url!));
if (options.ignoreInvalidCookies) {
promises = promises.map(p => p.catch(() => {}));
}

await Promise.all(promises);
}

if (options.followRedirect && 'location' in typedResponse.headers) {
Expand Down
1 change: 1 addition & 0 deletions source/utils/types.ts
Expand Up @@ -124,6 +124,7 @@ export interface Options extends Omit<https.RequestOptions, 'agent' | 'timeout'
retry?: number | Partial<RetryOption | NormalizedRetryOptions>;
throwHttpErrors?: boolean;
cookieJar?: CookieJar;
ignoreInvalidCookies?: boolean;
request?: RequestFunction;
agent?: http.Agent | https.Agent | boolean | AgentByProtocol;
gotTimeout?: number | Delays;
Expand Down
17 changes: 17 additions & 0 deletions test/cookies.ts
Expand Up @@ -70,6 +70,23 @@ test('throws on invalid cookies', withServer, async (t, server, got) => {
await t.throwsAsync(got({cookieJar}), 'Cookie has domain set to a public suffix');
});

test('does not throw on invalid cookies when options.ignoreInvalidCookies is set', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('set-cookie', 'hello=world; domain=localhost');
response.end();
});

const cookieJar = new toughCookie.CookieJar();

await got({
cookieJar,
ignoreInvalidCookies: true
});

const cookies = cookieJar.getCookiesSync(server.url);
t.is(cookies.length, 0);
});

test('catches store errors', async t => {
const error = 'Some error';
// @ts-ignore
Expand Down

0 comments on commit a48cef9

Please sign in to comment.