rjz / supertest-session

Persistent sessions for supertest
Other
89 stars 21 forks source link

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

Open max-l opened 5 years ago

max-l commented 5 years ago

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 commented 5 years ago

@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 commented 2 years ago

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