@3846masa/axios-cookiejar-support
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -12,12 +12,13 @@ 'use strict'; | ||
axios.get('https://mockbin.org/request', { | ||
jar: cookieJar, | ||
withCredentials: true // IMPORTANT! | ||
}) | ||
.then((response) => { | ||
const data = response.data; | ||
console.log(data.headers.cookie); | ||
}) | ||
.catch((err) => { | ||
console.error(err.stack || err); | ||
}); | ||
axios | ||
.get('https://mockbin.org/request', { | ||
jar: cookieJar, | ||
withCredentials: true, // IMPORTANT! | ||
}) | ||
.then(response => { | ||
const data = response.data; | ||
console.log(data.headers.cookie); | ||
}) | ||
.catch(err => { | ||
console.error(err.stack || err); | ||
}); |
@@ -13,10 +13,11 @@ 'use strict'; | ||
axios.get('https://google.com') | ||
.then((response) => { | ||
const config = response.config; | ||
// axios.defaults.jar === config.jar | ||
console.log(config.jar.toJSON()); | ||
}) | ||
.catch((err) => { | ||
console.error(err.stack || err); | ||
}); | ||
axios | ||
.get('https://google.com') | ||
.then(response => { | ||
const config = response.config; | ||
// axios.defaults.jar === config.jar | ||
console.log(config.jar.toJSON()); | ||
}) | ||
.catch(err => { | ||
console.error(err.stack || err); | ||
}); |
@@ -11,12 +11,13 @@ 'use strict'; | ||
axios.get('https://google.com', { | ||
jar: cookieJar, | ||
withCredentials: true | ||
}) | ||
.then((response) => { | ||
const config = response.config; | ||
console.log(config.jar.toJSON()); | ||
}) | ||
.catch((err) => { | ||
console.error(err.stack || err); | ||
}); | ||
axios | ||
.get('https://google.com', { | ||
jar: cookieJar, | ||
withCredentials: true, | ||
}) | ||
.then(response => { | ||
const config = response.config; | ||
console.log(config.jar.toJSON()); | ||
}) | ||
.catch(err => { | ||
console.error(err.stack || err); | ||
}); |
'use strict'; | ||
const pify = require('pify'); | ||
const isAbsoluteURL = require('axios/lib/helpers/isAbsoluteURL'); | ||
const combineURLs = require('axios/lib/helpers/combineURLs'); | ||
function requestInterceptorResolve (config) { | ||
function requestInterceptorResolve(config) { | ||
return Promise.resolve(config) | ||
.then(function backupOriginalConfigs (config) { | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
local.backupOptions = local.backupOptions || {}; | ||
return config; | ||
}) | ||
.then(function redirectSetup (config) { | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
.then(function backupOriginalConfigs(config) { | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
local.backupOptions = local.backupOptions || {}; | ||
return config; | ||
}) | ||
.then(function redirectSetup(config) { | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
local.redirectCount = | ||
isFinite(config.maxRedirects) ? config.maxRedirects : 5; | ||
local.redirectCount = isFinite(config.maxRedirects) ? config.maxRedirects : 5; | ||
local.backupOptions.maxRedirects = | ||
local.backupOptions.maxRedirects || config.maxRedirects; | ||
local.backupOptions.maxRedirects = local.backupOptions.maxRedirects || config.maxRedirects; | ||
local.backupOptions.validateStatus = | ||
local.backupOptions.validateStatus || config.validateStatus; | ||
local.backupOptions.validateStatus = local.backupOptions.validateStatus || config.validateStatus; | ||
config.maxRedirects = 0; | ||
config.validateStatus = undefined; | ||
config.maxRedirects = 0; | ||
config.validateStatus = undefined; | ||
return config; | ||
}) | ||
.then(function cookieSetup (config) { | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
return config; | ||
}) | ||
.then(function cookieSetup(config) { | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (local.jar && config.withCredentials) { | ||
const getCookieString = | ||
pify(local.jar.getCookieString.bind(local.jar)); | ||
if (local.jar && config.withCredentials) { | ||
const getCookieString = pify(local.jar.getCookieString.bind(local.jar)); | ||
return getCookieString(config.url) | ||
.then(function (cookieString) { | ||
if (!cookieString) { | ||
return; | ||
} | ||
config.headers['Cookie'] = | ||
(config.headers['Cookie']) | ||
? cookieString + '; ' + config.headers['Cookie'] | ||
: cookieString; | ||
}).then(function () { return config; }); | ||
} | ||
const requestUrl = | ||
config.baseURL && !isAbsoluteURL(config.url) ? combineURLs(config.baseURL, config.url) : config.url; | ||
return getCookieString(requestUrl) | ||
.then(function(cookieString) { | ||
if (!cookieString) { | ||
return; | ||
} | ||
config.headers['Cookie'] = config.headers['Cookie'] | ||
? cookieString + '; ' + config.headers['Cookie'] | ||
: cookieString; | ||
}) | ||
.then(function() { | ||
return config; | ||
}); | ||
} | ||
return config; | ||
}); | ||
}; | ||
return config; | ||
}); | ||
} | ||
function requestInterceptorReject (error) { | ||
function requestInterceptorReject(error) { | ||
return Promise.reject(error); | ||
}; | ||
} | ||
function requestInterceptorWrapper (instance) { | ||
instance.interceptors.request.use( | ||
requestInterceptorResolve, | ||
requestInterceptorReject | ||
); | ||
function requestInterceptorWrapper(instance) { | ||
instance.interceptors.request.use(requestInterceptorResolve, requestInterceptorReject); | ||
return instance; | ||
@@ -62,0 +61,0 @@ } |
@@ -7,105 +7,100 @@ 'use strict'; | ||
function responseInterceptorResolve (response, instance) { | ||
function responseInterceptorResolve(response, instance) { | ||
return Promise.resolve(response) | ||
.then(function setCookies (response) { | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
.then(function setCookies(response) { | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (!local.jar || !response.headers['set-cookie']) { | ||
return response; | ||
} | ||
if (!local.jar || !response.headers['set-cookie']) { | ||
return response; | ||
} | ||
const setCookie = pify(local.jar.setCookie.bind(local.jar)); | ||
const setCookiePromiseList = []; | ||
const setCookie = pify(local.jar.setCookie.bind(local.jar)); | ||
const setCookiePromiseList = []; | ||
if (Array.isArray(response.headers['set-cookie'])) { | ||
const cookies = response.headers['set-cookie']; | ||
cookies.forEach(function (cookie) { | ||
if (Array.isArray(response.headers['set-cookie'])) { | ||
const cookies = response.headers['set-cookie']; | ||
cookies.forEach(function(cookie) { | ||
setCookiePromiseList.push(setCookie(cookie, config.url)); | ||
}); | ||
} else { | ||
const cookie = response.headers['set-cookie']; | ||
setCookiePromiseList.push(setCookie(cookie, config.url)); | ||
} | ||
return Promise.all(setCookiePromiseList).then(function() { | ||
return response; | ||
}); | ||
} else { | ||
const cookie = response.headers['set-cookie']; | ||
setCookiePromiseList.push(setCookie(cookie, config.url)); | ||
} | ||
}) | ||
.then(function redirectSetup(response) { | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
return Promise.all(setCookiePromiseList) | ||
.then(function () { return response; }); | ||
}) | ||
.then(function redirectSetup (response) { | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
local.backupOptions.baseURL = local.backupOptions.baseURL || config.baseURL; | ||
local.backupOptions.url = local.backupOptions.url || config.url; | ||
local.backupOptions.method = local.backupOptions.method || config.method; | ||
local.backupOptions.baseURL = | ||
local.backupOptions.baseURL || config.baseURL; | ||
local.backupOptions.url = | ||
local.backupOptions.url || config.url; | ||
local.backupOptions.method = | ||
local.backupOptions.method || config.method; | ||
config.baseURL = undefined; | ||
config.url = config.url = url.resolve(response.config.url, response.headers['location'] || ''); | ||
config.baseURL = undefined; | ||
config.url = config.url = url.resolve(response.config.url, response.headers['location'] || ''); | ||
local.redirectCount--; | ||
local.redirectCount--; | ||
return response; | ||
}) | ||
.then(function redirect(response) { | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
return response; | ||
}) | ||
.then(function redirect (response) { | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (local.redirectCount < 0 || !response.headers['location']) { | ||
return response; | ||
} | ||
if (response.status !== 307) { | ||
config.method = 'get'; | ||
} | ||
if (local.redirectCount < 0 || !response.headers['location']) { | ||
return response; | ||
} | ||
if (response.status !== 307) { | ||
config.method = 'get'; | ||
} | ||
config.maxRedirects = local.redirectCount; | ||
return instance.request(config); | ||
}) | ||
.then(function restoreCookieJar(response) { | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (!local || !local.jar) return response; | ||
config.maxRedirects = local.redirectCount; | ||
return instance.request(config); | ||
}) | ||
.then(function restoreCookieJar (response) { | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (!local || !local.jar) return response; | ||
if (instance.defaults.jar && (!config.jar || config.jar === true)) { | ||
instance.defaults.jar = local.jar; | ||
} | ||
config.jar = local.jar; | ||
if (instance.defaults.jar && (!config.jar || config.jar === true)) { | ||
instance.defaults.jar = local.jar; | ||
} | ||
config.jar = local.jar; | ||
return response; | ||
}) | ||
.then(function restoreConfigs(response) { | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (!local) return response; | ||
return response; | ||
}) | ||
.then(function restoreConfigs (response) { | ||
const config = response.config; | ||
const local = config._COOKIEJAR_SUPPORT_LOCAL; | ||
if (!local) return response; | ||
for (let key in local.backupOptions) { | ||
config[key] = local.backupOptions[key]; | ||
} | ||
for (let key in local.backupOptions) { | ||
config[key] = local.backupOptions[key]; | ||
} | ||
return response; | ||
}) | ||
.then(function deleteLocals (response) { | ||
delete response.config._COOKIEJAR_SUPPORT_LOCAL; | ||
return response; | ||
}) | ||
.then(function validate (response) { | ||
return new Promise(function (resolve, reject) { | ||
settle(resolve, reject, response); | ||
return response; | ||
}) | ||
.then(function deleteLocals(response) { | ||
delete response.config._COOKIEJAR_SUPPORT_LOCAL; | ||
return response; | ||
}) | ||
.then(function validate(response) { | ||
return new Promise(function(resolve, reject) { | ||
settle(resolve, reject, response); | ||
}); | ||
}); | ||
}); | ||
} | ||
function responseInterceptorReject (error) { | ||
function responseInterceptorReject(error) { | ||
return Promise.reject(error); | ||
} | ||
function responseInterceptorWrapper (instance) { | ||
instance.interceptors.response.use( | ||
function (response) { | ||
return responseInterceptorResolve(response, instance); | ||
}, | ||
responseInterceptorReject | ||
); | ||
function responseInterceptorWrapper(instance) { | ||
instance.interceptors.response.use(function(response) { | ||
return responseInterceptorResolve(response, instance); | ||
}, responseInterceptorReject); | ||
return instance; | ||
@@ -112,0 +107,0 @@ } |
{ | ||
"name": "@3846masa/axios-cookiejar-support", | ||
"description": "Add tough-cookie support to axios.", | ||
"version": "0.1.3", | ||
"author": "3846masa", | ||
"browser": { | ||
"./index.js": "./noop.js" | ||
"license": "MIT", | ||
"homepage": "https://github.com/3846masa/axios-cookiejar-support#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/3846masa/axios-cookiejar-support.git" | ||
}, | ||
@@ -12,2 +14,12 @@ "bugs": { | ||
}, | ||
"version": "0.1.4", | ||
"main": "./index.js", | ||
"directories": { | ||
"example": "example", | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "mocha --require intelli-espower-loader", | ||
"postversion": "git commit --amend --allow-empty -m \"[skip ci] `git log -1 --pretty=%s`\"" | ||
}, | ||
"dependencies": { | ||
@@ -18,20 +30,15 @@ "@types/tough-cookie": "^2.3.1", | ||
}, | ||
"peerDependencies": { | ||
"axios": ">=0.16.2" | ||
}, | ||
"devDependencies": { | ||
"axios": "0.17.0", | ||
"axios": "0.17.1", | ||
"cookie": "0.3.1", | ||
"decache": "4.1.0", | ||
"eslint": "4.10.0", | ||
"eslint-config-standard": "10.2.1", | ||
"eslint-plugin-promise": "3.6.0", | ||
"eslint-plugin-standard": "3.0.1", | ||
"decache": "4.3.0", | ||
"eslint": "4.11.0", | ||
"intelli-espower-loader": "1.0.1", | ||
"mocha": "4.0.1", | ||
"nock": "9.0.27", | ||
"power-assert": "1.4.3" | ||
"nock": "9.1.0", | ||
"power-assert": "1.4.4" | ||
}, | ||
"directories": { | ||
"example": "example", | ||
"test": "test" | ||
}, | ||
"homepage": "https://github.com/3846masa/axios-cookiejar-support#readme", | ||
"keywords": [ | ||
@@ -44,16 +51,6 @@ "axios", | ||
], | ||
"license": "MIT", | ||
"main": "./index.js", | ||
"types": "./index.d.ts", | ||
"peerDependencies": { | ||
"axios": "^0.16.2" | ||
"browser": { | ||
"./index.js": "./noop.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/3846masa/axios-cookiejar-support.git" | ||
}, | ||
"scripts": { | ||
"postversion": "git commit --amend --allow-empty -m \"[skip ci] `git log -1 --pretty=%s`\"", | ||
"test": "mocha --require intelli-espower-loader" | ||
} | ||
"types": "./index.d.ts" | ||
} |
279
test/test.js
@@ -40,3 +40,3 @@ 'use strict'; | ||
it('should receive response', (done) => { | ||
it('should receive response', done => { | ||
nock('http://example.com') | ||
@@ -46,7 +46,9 @@ .get('/') | ||
axios.get('http://example.com') | ||
.then((res) => { | ||
axios | ||
.get('http://example.com') | ||
.then(res => { | ||
assert.strictEqual(res.status, 200); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
@@ -60,54 +62,62 @@ | ||
it('should not redirect if config.maxRedirects = 0', (done) => { | ||
it('should not redirect if config.maxRedirects = 0', done => { | ||
nockHost.get('/').reply(302, null, { | ||
'Location': 'http://example.com/redirect' | ||
Location: 'http://example.com/redirect', | ||
}); | ||
axios.get('http://example.com', { maxRedirects: 0 }) | ||
.then((res) => { | ||
axios | ||
.get('http://example.com', { maxRedirects: 0 }) | ||
.then(res => { | ||
assert.strictEqual(res.status, 302); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should redirect 30X response', (done) => { | ||
it('should redirect 30X response', done => { | ||
nockHost.post('/').reply(302, null, { | ||
'Location': 'http://example.com/redirect' | ||
Location: 'http://example.com/redirect', | ||
}); | ||
nockHost.get('/redirect').reply(200); | ||
axios.post('http://example.com') | ||
.then((res) => { | ||
axios | ||
.post('http://example.com') | ||
.then(res => { | ||
assert.strictEqual(res.status, 200); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should redirect many times 30X response', (done) => { | ||
it('should redirect many times 30X response', done => { | ||
nockHost.post('/').reply(302, null, { | ||
'Location': 'http://example.com/redirect/01' | ||
Location: 'http://example.com/redirect/01', | ||
}); | ||
nockHost.get('/redirect/01').reply(302, null, { | ||
'Location': 'http://example.com/redirect/02' | ||
Location: 'http://example.com/redirect/02', | ||
}); | ||
nockHost.get('/redirect/02').reply(200); | ||
axios.post('http://example.com') | ||
.then((res) => { | ||
axios | ||
.post('http://example.com') | ||
.then(res => { | ||
assert.strictEqual(res.status, 200); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should redirect 307 response', (done) => { | ||
it('should redirect 307 response', done => { | ||
nockHost.post('/').reply(307, null, { | ||
'Location': 'http://example.com/redirect' | ||
Location: 'http://example.com/redirect', | ||
}); | ||
nockHost.post('/redirect').reply(200); | ||
axios.post('http://example.com') | ||
.then((res) => { | ||
axios | ||
.post('http://example.com') | ||
.then(res => { | ||
assert.strictEqual(res.status, 200); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
@@ -117,10 +127,9 @@ | ||
nockHost.post('/').reply(302, null, { | ||
'Location': '/redirect' | ||
Location: '/redirect', | ||
}); | ||
nockHost.get('/redirect').reply(200); | ||
return axios.post('https://example.com') | ||
.then((res) => { | ||
assert.strictEqual(res.status, 200); | ||
}); | ||
return axios.post('https://example.com').then(res => { | ||
assert.strictEqual(res.status, 200); | ||
}); | ||
}); | ||
@@ -131,14 +140,13 @@ | ||
anotherNockHost.get('/redirect').reply(302, null, { | ||
'Location': '/final' | ||
Location: '/final', | ||
}); | ||
anotherNockHost.get('/final').reply(200, 'final'); | ||
nockHost.post('/').reply(302, null, { | ||
'Location': 'http://another.com/redirect' | ||
Location: 'http://another.com/redirect', | ||
}); | ||
return axios.post('http://example.com') | ||
.then((res) => { | ||
assert.strictEqual(res.status, 200); | ||
assert.strictEqual(res.data, 'final'); | ||
}); | ||
return axios.post('http://example.com').then(res => { | ||
assert.strictEqual(res.status, 200); | ||
assert.strictEqual(res.data, 'final'); | ||
}); | ||
}); | ||
@@ -149,12 +157,13 @@ | ||
anotherNockHost.get('/redirect').reply(302, null, { | ||
'Location': '/final' | ||
Location: '/final', | ||
}); | ||
anotherNockHost.get('/final').reply(200, 'final'); | ||
nockHost.post('/').reply(302, null, { | ||
'Location': 'http://another.com/redirect' | ||
Location: 'http://another.com/redirect', | ||
}); | ||
return axios.create({ baseURL: 'http://example.com' }) | ||
return axios | ||
.create({ baseURL: 'http://example.com' }) | ||
.post('http://example.com') | ||
.then((res) => { | ||
.then(res => { | ||
assert.strictEqual(res.status, 200); | ||
@@ -166,5 +175,5 @@ assert.strictEqual(res.data, 'final'); | ||
context('when hasn\'t defaults.jar', () => { | ||
context('and hasn\'t config.jar', () => { | ||
it('should not create cookiejar', (done) => { | ||
context("when hasn't defaults.jar", () => { | ||
context("and hasn't config.jar", () => { | ||
it('should not create cookiejar', done => { | ||
nock('http://example.com') | ||
@@ -174,7 +183,9 @@ .get('/') | ||
axios.get('http://example.com') | ||
.then((res) => { | ||
axios | ||
.get('http://example.com') | ||
.then(res => { | ||
assert.ok(!res.config.jar); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
@@ -184,3 +195,3 @@ }); | ||
context('and has config.jar', () => { | ||
it('should create cookiejar if config.jar = true', (done) => { | ||
it('should create cookiejar if config.jar = true', done => { | ||
nock('http://example.com') | ||
@@ -190,4 +201,5 @@ .get('/') | ||
axios.get('http://example.com', { jar: true }) | ||
.then((res) => { | ||
axios | ||
.get('http://example.com', { jar: true }) | ||
.then(res => { | ||
assert.ok(res.config.jar); | ||
@@ -197,6 +209,7 @@ assert.notStrictEqual(res.config.jar, true); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should not create cookiejar if cookie.jar = false', (done) => { | ||
it('should not create cookiejar if cookie.jar = false', done => { | ||
nock('http://example.com') | ||
@@ -206,10 +219,12 @@ .get('/') | ||
axios.get('http://example.com', { jar: false }) | ||
.then((res) => { | ||
axios | ||
.get('http://example.com', { jar: false }) | ||
.then(res => { | ||
assert.ok(!res.config.jar); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should use config.jar if config.jar = CookieJar', (done) => { | ||
it('should use config.jar if config.jar = CookieJar', done => { | ||
nock('http://example.com') | ||
@@ -219,7 +234,9 @@ .get('/') | ||
axios.get('http://example.com', { jar: cookieJar }) | ||
.then((res) => { | ||
axios | ||
.get('http://example.com', { jar: cookieJar }) | ||
.then(res => { | ||
assert.strictEqual(res.config.jar, cookieJar); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
@@ -234,4 +251,4 @@ }); | ||
context('and hasn\'t config.jar', () => { | ||
it('should create cookiejar if defaults.jar = true', (done) => { | ||
context("and hasn't config.jar", () => { | ||
it('should create cookiejar if defaults.jar = true', done => { | ||
nock('http://example.com') | ||
@@ -242,4 +259,5 @@ .get('/') | ||
axios.defaults.jar = true; | ||
axios.get('http://example.com') | ||
.then((res) => { | ||
axios | ||
.get('http://example.com') | ||
.then(res => { | ||
assert.ok(res.config.jar); | ||
@@ -253,6 +271,7 @@ assert.notStrictEqual(res.config.jar, true); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should use defaults.jar if defaults.jar = CookieJar', (done) => { | ||
it('should use defaults.jar if defaults.jar = CookieJar', done => { | ||
nock('http://example.com') | ||
@@ -262,8 +281,10 @@ .get('/') | ||
axios.get('http://example.com') | ||
.then((res) => { | ||
axios | ||
.get('http://example.com') | ||
.then(res => { | ||
assert.strictEqual(res.config.jar, cookieJar); | ||
assert.strictEqual(res.config.jar, axios.defaults.jar); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
@@ -273,3 +294,3 @@ }); | ||
context('and has config.jar', () => { | ||
it('should create cookiejar if defaults.jar = true && config.jar = true', (done) => { | ||
it('should create cookiejar if defaults.jar = true && config.jar = true', done => { | ||
nock('http://example.com') | ||
@@ -280,4 +301,5 @@ .get('/') | ||
axios.defaults.jar = true; | ||
axios.get('http://example.com', { jar: true }) | ||
.then((res) => { | ||
axios | ||
.get('http://example.com', { jar: true }) | ||
.then(res => { | ||
assert.ok(res.config.jar); | ||
@@ -291,6 +313,7 @@ assert.notStrictEqual(res.config.jar, true); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should use defaults.jar if config.jar = true', (done) => { | ||
it('should use defaults.jar if config.jar = true', done => { | ||
nock('http://example.com') | ||
@@ -300,10 +323,12 @@ .get('/') | ||
axios.get('http://example.com', { jar: true }) | ||
.then((res) => { | ||
axios | ||
.get('http://example.com', { jar: true }) | ||
.then(res => { | ||
assert.strictEqual(res.config.jar, axios.defaults.jar); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should not create cookiejar if config.jar = false', (done) => { | ||
it('should not create cookiejar if config.jar = false', done => { | ||
nock('http://example.com') | ||
@@ -313,10 +338,12 @@ .get('/') | ||
axios.get('http://example.com', { jar: false }) | ||
.then((res) => { | ||
axios | ||
.get('http://example.com', { jar: false }) | ||
.then(res => { | ||
assert.ok(!res.config.jar); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should use config.jar if config.jar = CookieJar', (done) => { | ||
it('should use config.jar if config.jar = CookieJar', done => { | ||
const anotherCookieJar = new tough.CookieJar(); | ||
@@ -327,7 +354,9 @@ nock('http://example.com') | ||
axios.get('http://example.com', { jar: anotherCookieJar }) | ||
.then((res) => { | ||
axios | ||
.get('http://example.com', { jar: anotherCookieJar }) | ||
.then(res => { | ||
assert.notStrictEqual(res.config.jar, axios.defaults.jar); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
@@ -343,15 +372,16 @@ }); | ||
key: 'test', | ||
value: 'value' | ||
value: 'value', | ||
}); | ||
}); | ||
it('should store cookie', (done) => { | ||
it('should store cookie', done => { | ||
nock('http://example.com') | ||
.get('/') | ||
.reply(200, null, { | ||
'Set-Cookie': originalCookie.toString() | ||
'Set-Cookie': originalCookie.toString(), | ||
}); | ||
axios.get('http://example.com') | ||
.then((res) => { | ||
axios | ||
.get('http://example.com') | ||
.then(res => { | ||
const receivedCookies = res.config.jar.getCookiesSync('http://example.com'); | ||
@@ -362,10 +392,11 @@ assert.strictEqual(receivedCookies.length, 1); | ||
}) | ||
.then(done).catch(done); | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should send cookie if withCredentials = true', (done) => { | ||
it('should send cookie if withCredentials = true', done => { | ||
cookieJar.setCookieSync(originalCookie, 'http://example.com'); | ||
nock('http://example.com') | ||
.matchHeader('cookie', (value) => { | ||
.matchHeader('cookie', value => { | ||
assert.ok(value); | ||
@@ -380,13 +411,39 @@ const receivedCookie = cookie.parse(value); | ||
axios.get('http://example.com', { withCredentials: true }) | ||
.then((res) => { | ||
axios | ||
.get('http://example.com', { withCredentials: true }) | ||
.then(res => { | ||
assert.strictEqual(res.status, 200); | ||
}).then(done).catch(done); | ||
}) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should not send cookie if withCredentials = false', (done) => { | ||
it('should send cookie if withCredentials = true and set baseURL', done => { | ||
cookieJar.setCookieSync(originalCookie, 'http://example.com'); | ||
nock('http://example.com') | ||
.matchHeader('cookie', (value) => { | ||
.matchHeader('cookie', value => { | ||
assert.ok(value); | ||
const receivedCookie = cookie.parse(value); | ||
assert.ok(originalCookie.key in receivedCookie); | ||
assert.strictEqual(receivedCookie[originalCookie.key], originalCookie.value); | ||
return true; | ||
}) | ||
.get('/') | ||
.reply(200); | ||
axios | ||
.get('/', { withCredentials: true, baseURL: 'http://example.com' }) | ||
.then(res => { | ||
assert.strictEqual(res.status, 200); | ||
}) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should not send cookie if withCredentials = false', done => { | ||
cookieJar.setCookieSync(originalCookie, 'http://example.com'); | ||
nock('http://example.com') | ||
.matchHeader('cookie', value => { | ||
assert.ok(!value); | ||
@@ -398,13 +455,16 @@ return true; | ||
axios.get('http://example.com', { withCredentials: false }) | ||
.then((res) => { | ||
axios | ||
.get('http://example.com', { withCredentials: false }) | ||
.then(res => { | ||
assert.strictEqual(res.status, 200); | ||
}).then(done).catch(done); | ||
}) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
it('should send all cookies if set headers[\'Cookie\']', (done) => { | ||
it("should send all cookies if set headers['Cookie']", done => { | ||
cookieJar.setCookieSync(originalCookie, 'http://example.com'); | ||
nock('http://example.com') | ||
.matchHeader('cookie', (value) => { | ||
.matchHeader('cookie', value => { | ||
assert.ok(value); | ||
@@ -421,11 +481,14 @@ const receivedCookie = cookie.parse(value); | ||
axios.get('http://example.com', { | ||
headers: { 'Cookie': 'from=string' }, | ||
withCredentials: true | ||
}) | ||
.then((res) => { | ||
axios | ||
.get('http://example.com', { | ||
headers: { Cookie: 'from=string' }, | ||
withCredentials: true, | ||
}) | ||
.then(res => { | ||
assert.strictEqual(res.status, 200); | ||
}).then(done).catch(done); | ||
}) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
79053
8
16
668