From 28eb9e31a30bcc5c38830ac4c9646dcf3122ccb2 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Thu, 8 Dec 2016 11:49:24 -0800 Subject: [PATCH] Fix cookie string handling with requests Closes #82 --- custom_typings/env.d.ts | 10 ---------- lib/index.ts | 21 ++++++++++++--------- lib/test/index.ts | 4 ++-- package.json | 2 +- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/custom_typings/env.d.ts b/custom_typings/env.d.ts index 442b90f..a94f1e8 100644 --- a/custom_typings/env.d.ts +++ b/custom_typings/env.d.ts @@ -3,13 +3,3 @@ declare namespace NodeJS { browser: boolean } } - -declare module 'http' { - interface ClientRequest { - getHeader (name: string): string - } -} - -interface XMLHttpRequest { - responseURL: string -} diff --git a/lib/index.ts b/lib/index.ts index f6f1eef..dc2d2d7 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -5,7 +5,7 @@ import urlLib = require('url') import extend = require('xtend') import arrify = require('arrify') import concat = require('concat-stream') -import { Cookie } from 'tough-cookie' +import { Cookie, CookieJar } from 'tough-cookie' import Promise = require('any-promise') import { createUnzip } from 'zlib' import { Headers } from './base' @@ -26,7 +26,7 @@ const validTypes = ['text', 'buffer', 'array', 'uint8array', 'stream'] export interface Options { type?: Types unzip?: boolean - jar?: any + jar?: CookieJar agent?: any maxRedirects?: number rejectUnauthorized?: boolean @@ -275,24 +275,27 @@ function falsey () { * Read cookies from the cookie jar. */ function getAttachCookies (request: Request, options: Options): (url: string) => Promise { - const { jar } = options - const cookie = request.getAll('Cookie') + const requestCookieString = request.getAll('Cookie').join('; ') - if (!jar) { + if (!options.jar) { return () => Promise.resolve() } return function (url: string) { return new Promise(function (resolve, reject) { - request.set('Cookie', cookie) + let cookieString = requestCookieString - options.jar.getCookies(url, function (err: Error, cookies: Cookie[]) { + options.jar.getCookieString(url, function (err: Error, jarCookieString?: string) { if (err) { return reject(err) } - if (cookies.length) { - request.append('Cookie', cookies.join('; ')) + if (jarCookieString) { + cookieString = cookieString ? `${cookieString}; ${jarCookieString}` : jarCookieString + } + + if (cookieString) { + request.set('Cookie', cookieString) } return resolve() diff --git a/lib/test/index.ts b/lib/test/index.ts index d09729d..8d7a96d 100644 --- a/lib/test/index.ts +++ b/lib/test/index.ts @@ -872,12 +872,12 @@ if (!process.browser) { t.notOk(res.get('Cookie')) t.ok(res.get('Set-Cookie')) - cookie = res.get('Set-Cookie') + cookie = res.get('Set-Cookie').split(/ *; */, 1)[0] return instance(REMOTE_URL + '/echo') }) .then(function (res) { - t.equal(res.get('Cookie').toLowerCase(), cookie.toLowerCase()) + t.equal(res.get('Cookie'), cookie) t.notOk(res.get('Set-Cookie')) }) }) diff --git a/package.json b/package.json index 4561226..76a12bf 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "methods": "^1.1.2", "tap-spec": "^4.1.1", "tape-run": "2.1.0", - "typescript": "^2.0.3", + "typescript": "^2.1.4", "typings": "^1.0.2" }, "dependencies": {