Skip to content

Commit

Permalink
Fix cookie string handling with requests
Browse files Browse the repository at this point in the history
Closes #82
  • Loading branch information
blakeembrey committed Dec 8, 2016
1 parent 3841002 commit 28eb9e3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
10 changes: 0 additions & 10 deletions custom_typings/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,3 @@ declare namespace NodeJS {
browser: boolean
}
}

declare module 'http' {
interface ClientRequest {
getHeader (name: string): string
}
}

interface XMLHttpRequest {
responseURL: string
}
21 changes: 12 additions & 9 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -275,24 +275,27 @@ function falsey () {
* Read cookies from the cookie jar.
*/
function getAttachCookies (request: Request, options: Options): (url: string) => Promise<any> {
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()
Expand Down
4 changes: 2 additions & 2 deletions lib/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 28eb9e3

Please sign in to comment.