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

Add option to ignore invalid cookies #826

Merged
merged 2 commits into from Jul 1, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`

Ignore invalid cookies instead of throwing an error. Only useful when the `cookieJar` option has been set. Not recommended.

###### 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