Skip to content

Commit

Permalink
tests - cookie redirect test
Browse files Browse the repository at this point in the history
  • Loading branch information
rumblefrog authored and KishanBagaria committed Nov 16, 2021
1 parent 7cf2051 commit 25f0107
Show file tree
Hide file tree
Showing 4 changed files with 396 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@types/jest": "^26.0.23",
"@types/tough-cookie": "^4.0.0",
"cargo-cp-artifact": "^0.1",
"express": "^4.17.1",
"form-data": "^4.0.0",
"jest": "^27.0.4",
"node-pre-gyp-github": "https://github.com/rumblefrog/node-pre-gyp-github.git",
Expand Down
2 changes: 1 addition & 1 deletion src/time_jar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub type NewCookies = (String, Vec<String>);

/// Time based jar.
///
/// The motivation behind is that we cannot set a dedicate jar for each request.
/// The motivation behind this is that we cannot set a dedicated jar for each request.
/// And recreating the client for each request seems wasteful.
///
/// Therefore we need to track cookies by time to fetch cookies since request time.
Expand Down
45 changes: 45 additions & 0 deletions tests/cookie-redirect.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { CookieJar } = require('tough-cookie');
const express = require('express');
const { Client } = require('../dist');

let client, server;

beforeAll(() => {
client = new Client({
connectTimeout: 5,
requestTimeout: 5,
redirectLimit: 5,
httpsOnly: false,
https2AdaptiveWindow: false,
});

let app = express();

app.get('/', (_req, res) => {
res.cookie('cookie-monster', 'redirect-persist').redirect('/done');
});

app.get('/done', (_req, res) => {
res.json({ ok: true });
})

server = app.listen(3005);
});

test('Cookie should persist after redirect', async () => {
let jar = new CookieJar();

let ret = await client.request('http://127.0.0.1:3005', {
cookieJar: jar,
});

expect(ret.statusCode).toBe(200);

const cookieStr = jar.getCookieStringSync('http://127.0.0.1:3000');

expect(cookieStr).toBe('cookie-monster=redirect-persist');
});

afterAll(() => {
server.close();
});
Loading

0 comments on commit 25f0107

Please sign in to comment.