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

treq.testing.StubTreq does not handle twisted.web.server.Session objects correctly. #327

Closed
bennr01 opened this issue Apr 12, 2021 · 1 comment
Labels
Milestone

Comments

@bennr01
Copy link
Contributor

bennr01 commented Apr 12, 2021

Hi,

I observed a bug when using StubTreq for testing a twisted.web.Resource using request.getSession() to store data between requests. Below is a rather dry report and my proposal for fixing this bug.

Expected behavior

When using treq.testing.StubTreq(some_resource) to make requests to the wrapped resource, the wrapped resource should be able to obtain the same twisted.web.server.Session instance every time request.getSession() is called in one of its render*(request) methods (provided that the same cookies parameter is supplied for every request).

Observed behavior

Even with proper cookies being set, calling request.getSession() in the wrapped resource returns a twisted.web.server.Session instance with a different uid. I confirmed that the TWISTED_SESSION cookie was used for both requests.

Debugging result

Each twisted.web.server.Session instance is stored in the twisted.web.server.Site instance. treq.testing.StubTreq, or more precisely treq.testing.RequestTraversalAgent creates a new twisted.web.server.Site instance for each request. Thus, the sessions aren't stored between requests.

Fix

Changing treq.testing.RequestTraversalAgent so that the serverFactory variable is moved from .request() to .__init__() and made an attribute of the treq.testing.RequestTraversalAgent shared between requests seems to fix this. Of course, the subsequent references to serverFactory must also be changed to the new attribute instead.

However, this introduces a new semi-problem: When using StubTreq for testing (which it likely will be used for) in combination with sessions makes it easy to leave a dirty reactor behind. This is caused by the DelayedCalls to Session.expire(), thus manual cleanup is needed. Changing StubTreq to make the _agent also a permanent attribute allows manual cleanup by directly expiring the sessions on tearDown() . This, however, is a rather 'ugly' affair. Thus, I want to propose to also add a cleanSessions() (or a more generic clean()) method to treq.testing.StubTreq to offer the user a simple way of cleaning the reactor when sessions were used.

bennr01 added a commit to bennr01/treq that referenced this issue Apr 13, 2021
Before this commit, twisted.web.server.Session objects were not persisted properly between requests.
Thus, two requests with the same cookies would result in two different sessions.
See twisted#327 for details.
@twm twm added the bug label Apr 17, 2021
@twm twm added this to the Treq NEXT milestone Apr 17, 2021
@bennr01 bennr01 closed this as completed May 24, 2021
@twm
Copy link
Contributor

twm commented May 24, 2021

Released in treq 21.5.0

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

No branches or pull requests

2 participants