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

Server sets two cookies during a request, supertest-session persists only one #38

Open
max-l opened this issue Jul 2, 2019 · 2 comments

Comments

@max-l
Copy link

max-l commented Jul 2, 2019

I have the followin test routes, the first sets a cookie, the second retrieves it.

    server.get('/t2/:z',  (req, res) => {
        req.session = {v: req.params.z}
        res.send(req.session)
    })

    server.get('/t3',  (req, res) => {
        logger.debug("cookie header: %s", req.headers.cookie)
        res.send(req.session)
    })

I am using cookie-session: https://github.com/expressjs/cookie-session
to handle sessions

When I test the above routes with a browser, it works as expected, and I can see that cookie-session sets two cookies, one for the session object, and one for the signature (session.sig), as the log shows:

cookie header: _ga=GA1.1.984665860.1553382018; session=eyJ2IjoicXEifQ==; session.sig=kJH4iEj4NkYR4HwYyo0QI42fF9A

When I use supertest-session, only one cookie gets set, as show by the log :

cookie header: session=eyJ2IjoicXdlcnR5In0=

Which causes cookie-session to not recognize the session, since it has no signature cookie.

The jest / supertest-session code is below.

Is there a way to tell supertest-session to persist all cookies set during a request ?

import cookieSession from 'cookie-session'
import session from 'supertest-session'
import express from 'express'

const server = createRoutes(
    express()
    .use(express.json())
    .use(cookieSession({
        name: 'session',
        signed: true,
        secret: process.env.COOKIE_SIGNING_SECRET,
        maxAge: 30 * 60 * 1000
      }))
      .use((req, res, next) => {
        req.session.now = Date.now()
        next()
      })
)


    test.only("test session-cookie", async done => {

        const testSession = session(server)

        const res1 = await testSession.get('/t2/qwerty').send()

        expect(res1.status).toEqual(200)
        expect(res1.body.v).toEqual("qwerty")

        const res2 = await testSession.get('/t3').send()

        expect(res2.status).toEqual(200)
        expect(res2.body.v).toEqual("qwerty")
    })
@ctavan
Copy link
Collaborator

ctavan commented Jul 3, 2019

@max-l thanks for the report! Unfortunately I have a hard time reproducing the issue with just the code you pasted above.

Would you mind creating a failing test case (e.g. in https://github.com/rjz/supertest-session/blob/master/test/main_spec.js) and submitting it as a pull request so that we can dig deeper into this issue?

@AnisBelahcene
Copy link

have you find the problem ? i am having the same req.session.infoUser is undefined for me with jest but when i use postman it works

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