Skip to content

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

Description

@max-l

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")
    })

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions