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

Using express flash does not trigger a session update #66

Closed
zbyte64 opened this issue Nov 1, 2016 · 16 comments
Closed

Using express flash does not trigger a session update #66

zbyte64 opened this issue Nov 1, 2016 · 16 comments
Assignees

Comments

@zbyte64
Copy link

zbyte64 commented Nov 1, 2016

The following happens in express 4:

req.flash('error', error.toString());
assert(req.session.isChanged == false)
req.session.foo = req.session.flash;
assert(req.session.isChanged == true)

But even doing that doesn't persist the data, both foo and flash get serialized as empty arrays. But if I set foo manually to an array or object then that does work.

@dougwilson
Copy link
Contributor

Is it possible to provide a full test app I can run to reproduce the issue? That way I can attach a debugger to see what's happening. Otherwise, you're welcome to make a PR :)

@zbyte64
Copy link
Author

zbyte64 commented Nov 1, 2016

I will whip up a failing test.

@dougwilson
Copy link
Contributor

Cool, also would be helpful if you provided the following version in the meantime to help me work in parallel: version of Node.js, version of express flash, version of this module.

@zbyte64
Copy link
Author

zbyte64 commented Nov 1, 2016

Forked this project and wrote an integration test using connect-flash:

https://github.com/zbyte64/cookie-session/blob/flash-message-fail/test/flash_test.js

I think I made some mistake because I can see in the view that properly retrieves the prior flash messages but weird stuff happens when I try to send it back to the client (empty response when sending a non-empty string, 500 if I don't stringify).

The package.json of the project experiencing the issue: https://github.com/zbyte64/project-cms-backend/blob/postgres/package.json ; using node v6.4

@dougwilson
Copy link
Contributor

I'm not seeing any issue with this module in your test, unless I'm missing something?

@zbyte64
Copy link
Author

zbyte64 commented Nov 1, 2016

The test fails:

  1) Cookie Session with flash message should persist:

      Uncaught AssertionError: {} deepEqual { info: [ 'hello world' ] }
      + expected - actual

      -{}
      +{
      +  "info": [
      +    "hello world"
      +  ]
      +}

@dougwilson
Copy link
Contributor

Yea, but res.body has nothing to do with this module. What if you move the assert into the server side and test what is in req.session?

@dougwilson
Copy link
Contributor

If you can please put together a pull request with a potential fix to this module, that would probably be ideal, because I'm not sure I see any issue based on that test, but again, I may be missing something :(

@zbyte64
Copy link
Author

zbyte64 commented Nov 1, 2016

You're right, this is an issue outside of cookie-session (connect?).

@zbyte64 zbyte64 closed this as completed Nov 1, 2016
@dougwilson
Copy link
Contributor

So to add, I had to heavily modify your test just to get it to run for me. Just cloning it, npm install + npm test gave me the following error:

$ npm test

> cookie-session@2.0.0-alpha.1 test C:\cs-test\cookie-session
> mocha --check-leaks --reporter spec --bail test/



  Cookie Session with flash message
    1) should Set-Cookie


  0 passing (25ms)
  1 failing

  1) Cookie Session with flash message should Set-Cookie:
     Uncaught TypeError: Cannot read property 'header' of undefined
      at Test.<anonymous> (C:\cs-test\cookie-session\test\flash_test.js:25:21)
      at Test.assert (C:\cs-test\cookie-session\node_modules\supertest\lib\test.js:156:6)
      at Server.assert (C:\cs-test\cookie-session\node_modules\supertest\lib\test.js:127:12)
      at net.js:1277:10



npm ERR! Test failed.  See above for more details.

@dougwilson
Copy link
Contributor

On https://github.com/jaredhanson/connect-flash I only see mentions about using it on Express 3.x. Are you sure it even works on Connect?

@zbyte64
Copy link
Author

zbyte64 commented Nov 1, 2016

Hmm, title does mention connect at the top, but given it hasn't been updated in 4 years....
(Flash message middleware for Connect and Express.)

@dougwilson
Copy link
Contributor

@zbyte64 does it sound like you are experiencing this bug? jaredhanson/connect-flash#22

@zbyte64
Copy link
Author

zbyte64 commented Nov 1, 2016

Possibly, but not even error type is working for me.

@dougwilson
Copy link
Contributor

Hi @zbyte64 your test you have above passes without issue if you add res.setHeader('Content-Type', 'application/json'); between lines 38 and 39. The reason your req.body was {} is because supertest didn't know it was JSON to parse without a Content-Type header.

@zbyte64
Copy link
Author

zbyte64 commented Nov 1, 2016

Thank you.

I ended up using req-flash ( maximilianschmitt/req-flash ) and it works.

@dougwilson dougwilson self-assigned this Nov 1, 2016
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

2 participants