UNPKG

axios-with-cookies

Version:

Fork of axios-cookiejar-support with configurable http(s)-agent

61 lines (60 loc) 2.38 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ava_1 = __importDefault(require("ava")); const axios_1 = __importDefault(require("axios")); const tough_cookie_1 = require("tough-cookie"); const __1 = require("../"); const helpers_1 = require("./helpers"); ava_1.default.before(() => { (0, __1.wrapper)(axios_1.default); }); ava_1.default.serial.afterEach.always(() => { delete axios_1.default.defaults.jar; }); ava_1.default.serial('should store cookies to cookiejar when default.jar was assigned', async (t) => { const { port } = await (0, helpers_1.createTestServer)([ (_req, res) => { res.setHeader('Set-Cookie', 'key=value'); res.end(); }, ]); const jar = new tough_cookie_1.CookieJar(); axios_1.default.defaults.jar = jar; await axios_1.default.get(`http://localhost:${port}`); const cookies = await jar.getCookies(`http://localhost:${port}`); t.like(cookies, { 0: { key: 'key', value: 'value' }, }); t.plan(1); }); ava_1.default.serial('should send cookies from cookiejar when default.jar was assigned', async (t) => { const { port } = await (0, helpers_1.createTestServer)([ (req, res) => { t.is(req.headers['cookie'], 'key=value'); res.end(); }, ]); const jar = new tough_cookie_1.CookieJar(); axios_1.default.defaults.jar = jar; await jar.setCookie('key=value', `http://localhost:${port}`); await axios_1.default.get(`http://localhost:${port}`); t.plan(1); }); ava_1.default.serial('should use config.cookiejar in preference to default.jar', async (t) => { const { port } = await (0, helpers_1.createTestServer)([ (req, res) => { t.is(req.headers['cookie'], 'key=config'); res.end(); }, ]); const defaultJar = new tough_cookie_1.CookieJar(); axios_1.default.defaults.jar = defaultJar; await defaultJar.setCookie('key=default', `http://localhost:${port}`); const configJar = new tough_cookie_1.CookieJar(); await configJar.setCookie('key=config', `http://localhost:${port}`); await axios_1.default.get(`http://localhost:${port}`, { jar: configJar }); t.plan(1); });