axios-with-cookies
Version:
Fork of axios-cookiejar-support with configurable http(s)-agent
235 lines (234 loc) • 8.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_http_1 = __importDefault(require("node:http"));
const node_https_1 = __importDefault(require("node:https"));
const ava_1 = __importDefault(require("ava"));
const axios_1 = __importDefault(require("axios"));
const http_1 = require("http-cookie-agent/http");
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('should receive response correctly', async (t) => {
const { port } = await (0, helpers_1.createTestServer)([
(_req, res) => {
res.end('Hello World!');
}
]);
const jar = new tough_cookie_1.CookieJar();
const { data, status } = await axios_1.default.get(`http://localhost:${port}`, { jar, responseType: 'text' });
t.is(status, 200);
t.is(data, 'Hello World!');
t.plan(2);
});
ava_1.default.serial('should store cookies to cookiejar', 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();
await axios_1.default.get(`http://localhost:${port}`, { jar });
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', 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();
await jar.setCookie('key=value', `http://localhost:${port}`);
await axios_1.default.get(`http://localhost:${port}`, { jar });
t.plan(1);
});
ava_1.default.serial('should merge cookies from cookiejar and cookie header', async (t) => {
const { port } = await (0, helpers_1.createTestServer)([
(req, res) => {
t.is(req.headers['cookie'], 'key1=value1; key2=value2');
res.end();
}
]);
const jar = new tough_cookie_1.CookieJar();
await jar.setCookie('key1=value1', `http://localhost:${port}`);
await axios_1.default.get(`http://localhost:${port}`, {
headers: { Cookie: 'key2=value2' },
jar
});
t.plan(1);
});
ava_1.default.serial('should send cookies which received first request when redirecting to same domain', async (t) => {
const { port } = await (0, helpers_1.createTestServer)([
(_req, res) => {
res.statusCode = 301;
res.setHeader('Location', '/another-path');
res.setHeader('Set-Cookie', 'key=value');
res.end();
},
(req, res) => {
t.is(req.headers['cookie'], 'key=value');
res.end();
}
]);
const jar = new tough_cookie_1.CookieJar();
await axios_1.default.get(`http://localhost:${port}`, { jar });
t.plan(1);
});
ava_1.default.serial('should not send cookies which received first request when redirecting to another domain', async (t) => {
const { port } = await (0, helpers_1.createTestServer)([
(_req, res) => {
res.statusCode = 301;
res.setHeader('Location', `http://127.0.0.1:${port}`);
res.setHeader('Set-Cookie', 'key=value');
res.end();
},
(req, res) => {
t.false('cookie' in req.headers);
res.end();
}
]);
const jar = new tough_cookie_1.CookieJar();
await axios_1.default.get(`http://localhost:${port}`, { jar });
t.plan(1);
});
ava_1.default.serial('should send cookies even when target is same host but different port', async (t) => {
const { port: firstServerPort } = await (0, helpers_1.createTestServer)([
(_req, res) => {
res.setHeader('Set-Cookie', 'key=expected');
res.end();
}
]);
const { port: secondServerPort } = await (0, helpers_1.createTestServer)([
(req, res) => {
t.is(req.headers['cookie'], 'key=expected');
res.end();
}
]);
const jar = new tough_cookie_1.CookieJar();
await axios_1.default.get(`http://localhost:${firstServerPort}`, { jar });
await axios_1.default.get(`http://localhost:${secondServerPort}`, { jar });
t.plan(1);
});
ava_1.default.serial('should throw error when config.jar was assigned with boolean', async (t) => {
const { port, server } = await (0, helpers_1.createTestServer)([
(_req, res) => {
res.end();
}
]);
await t.throwsAsync(async () => {
await axios_1.default.get(`http://localhost:${port}`, {
// @ts-expect-error ...
jar: true
});
}, {
message: 'config.jar does not accept boolean since axios-cookiejar-support@2.0.0.'
});
t.plan(1);
server.close();
});
ava_1.default.serial('should work when config.httpAgent is instance of HttpCookieAgent', async (t) => {
const { port, server } = await (0, helpers_1.createTestServer)([
(_req, res) => {
res.end();
},
(_req, res) => {
res.write('success');
res.end();
}
]);
const jar = new tough_cookie_1.CookieJar();
await t.notThrowsAsync(async () => {
const { config } = await axios_1.default.get(`http://localhost:${port}`, { jar });
// simulate request retry
t.assert(config.httpAgent instanceof http_1.HttpCookieAgent);
const { data } = await axios_1.default.get(`http://localhost:${port}`, { httpAgent: config.httpAgent, jar });
t.assert(data === 'success', '');
});
t.plan(3);
server.close();
});
ava_1.default.serial('should work when config.httpsAgent is instance of HttpsCookieAgent', async (t) => {
const { port, server } = await (0, helpers_1.createTestServer)([
(_req, res) => {
res.end();
},
(_req, res) => {
res.write('success');
res.end();
}
]);
const jar = new tough_cookie_1.CookieJar();
await t.notThrowsAsync(async () => {
const { config } = await axios_1.default.get(`http://localhost:${port}`, { jar });
// simulate request retry
t.assert(config.httpsAgent instanceof http_1.HttpsCookieAgent);
const { data } = await axios_1.default.get(`http://localhost:${port}`, { httpsAgent: config.httpsAgent, jar });
t.assert(data === 'success', '');
});
t.plan(3);
server.close();
});
ava_1.default.serial('should work when config.httpAgent is instance of http.Agent (node-native)', async (t) => {
const { port, server } = await (0, helpers_1.createTestServer)([
(_req, res) => {
res.end();
},
(_req, res) => {
res.write('success');
res.end();
}
]);
const jar = new tough_cookie_1.CookieJar();
await t.notThrowsAsync(async () => {
const { config } = await axios_1.default.get(`http://localhost:${port}`, {
jar,
httpAgent: new node_http_1.default.Agent({
keepAlive: true,
timeout: 100000
})
});
// simulate request retry
const { data } = await axios_1.default.get(`http://localhost:${port}`, { httpAgent: config.httpAgent, jar });
t.assert(data === 'success', '');
});
t.plan(2);
server.close();
});
ava_1.default.serial('should work when config.httpsAgent is instance of https.Agent (node-native)', async (t) => {
const { port, server } = await (0, helpers_1.createTestServer)([
(_req, res) => {
res.end();
},
(_req, res) => {
res.write('success');
res.end();
}
]);
const jar = new tough_cookie_1.CookieJar();
await t.notThrowsAsync(async () => {
const { config } = await axios_1.default.get(`http://localhost:${port}`, {
jar,
httpsAgent: new node_https_1.default.Agent({
rejectUnauthorized: false,
keepAlive: true,
timeout: 100000
})
});
// simulate request retry
const { data } = await axios_1.default.get(`http://localhost:${port}`, { httpsAgent: config.httpsAgent, jar });
t.assert(data === 'success', '');
});
t.plan(2);
server.close();
});