Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unintuitive Set-Cookie behavior #413

Closed
jaygood opened this issue Apr 21, 2017 · 5 comments
Closed

Unintuitive Set-Cookie behavior #413

jaygood opened this issue Apr 21, 2017 · 5 comments

Comments

@jaygood
Copy link

jaygood commented Apr 21, 2017

const request = require('supertest');

const app = require('express')()
  .get('/cookies', (req, res, next) => {
    res.cookie('one', '1');
    res.cookie('two', '2');
    res.json(res._headers['set-cookie']);
  });

describe('/cookies', () => {
  it('should have correct anatomy', () =>
    request.agent(app)
      .get('/cookies')
      .expect(200, ['one=1; Path=/', 'two=2; Path=/']) // This is what I expect below
      .then(res => {
        expect(res.headers['set-cookie'].length).toBe(1); // should be 2
        expect(res.headers['set-cookie']).toEqual(['one=1; Path=/,two=2; Path=/']); // why is this a single string?
      })
  );
});

No?

@mikelax
Copy link
Contributor

mikelax commented Jun 6, 2017

Hello, I just tried your example using supertest version 2 and 3 and both are working for what you expect. ie this is true:
.expect(200, ['one=1; Path=/', 'two=2; Path=/']) // This is what I expect below

What version of express are you using?

Mike@Michaels-MacBook-Pro:exp-supertest $ npm list --depth=0
[email protected] /Users/Mike/git/exp-supertest
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

@mikelax mikelax closed this as completed Jun 6, 2017
@jaygood
Copy link
Author

jaygood commented Jun 7, 2017

Hey @mikelax

I wasn't very clear above... but that's not the line that is giving me problems.

These two lines are the issue:

expect(res.headers['set-cookie'].length).toBe(1); // should be 2
expect(res.headers['set-cookie']).toEqual(['one=1; Path=/,two=2; Path=/']); // why is this a single string?

Yes, they pass... because I put the passing code into the snippet. My concerns are in the comments. I expect the cookies to be in an array just like

expect(200, ['one=1; Path=/', 'two=2; Path=/']) // This is what I expect below

but it's not.

@mikelax
Copy link
Contributor

mikelax commented Jun 8, 2017

hmm, weird I just tried two different scripts and in both cases I am seeing the cookie be an array of two values, ie. what you are expecting. Here are the scripts:

Server

const express = require('express');
const supertest = require('supertest');
const compression = require('compression');
const bodyParser = require('body-parser');
const path = require('path');

const app = express();
app.use(compression());
app.use(bodyParser.json());

app.get('/cookie', (req, res) => {
  // res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });
  res.cookie('one', '1');
  res.cookie('two', '2');
  res.json(res._headers['set-cookie']);
});

Mocha Test 1

  it.only('Get cookie header', done => {
    supertest.agent(app)
      .get('/cookie')
      .expect(200)
      .end((err, res) => {
        console.log(res.headers['set-cookie']);
        console.log(res.headers['set-cookie'].length);
        console.log(res.body);
        if (err) return done(err);
        done();
      });
  });

Mocha Test 2 (using .agent)

  it.only('Get cookie header', done => {
    supertest.agent(app)
      .get('/cookie')
      .expect(200)
      .then(res => {
        console.log(res.headers['set-cookie']);
        console.log(res.headers['set-cookie'].length);
        console.log(res.body);
        done();
      });
  });

Output for both cases:

  Test gzip with file upload
[ 'one=1; Path=/', 'two=2; Path=/' ]
2
[ 'one=1; Path=/', 'two=2; Path=/' ]
    ✓ Get cookie header (88ms)


  1 passing (102ms)

@jaygood
Copy link
Author

jaygood commented Jun 8, 2017

Oh that is odd. I just tried with mocha and am getting

[ 'one=1; Path=/', 'two=2; Path=/' ]

whereas jest is getting

[ 'one=1; Path=/,two=2; Path=/' ]

I'll have to dig into what they're doing...

Thanks

@jbinto
Copy link

jbinto commented Jul 13, 2017

👍, looks like I'm hitting this exact issue too. Also running jest.

For people coming here via google, here is a workaround: jestjs/jest#3547 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants