Skip to content

Commit

Permalink
Merge pull request #84 from cjwatson/fix-goBack
Browse files Browse the repository at this point in the history
Invalidate caches in Browser.goBack()
  • Loading branch information
cjwatson committed Nov 10, 2019
2 parents e0dd810 + 5dd7be0 commit 784e0c6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Expand Up @@ -5,7 +5,9 @@ CHANGES
5.4.1 (unreleased)
------------------

- Nothing changed yet.
- Fix a bug where ``browser.goBack()`` did not invalidate caches, so
subsequent queries could use data from the wrong response. See `issue 83
<https://github.com/zopefoundation/zope.testbrowser/issues/83>`_.


5.4.0 (2019-11-01)
Expand Down
4 changes: 2 additions & 2 deletions src/zope/testbrowser/browser.py
Expand Up @@ -310,6 +310,7 @@ def _submit(self, form, name=None, index=None, coord=None, **args):

def _setResponse(self, response):
self._response = response
self._changed()

def getLink(self, text=None, url=None, id=None, index=0):
"""See zope.testbrowser.interfaces.IBrowser"""
Expand Down Expand Up @@ -475,7 +476,6 @@ def _changed(self):
self._contents = None
self._controls = {}
self.__html = None
self._req_content_type = None

@contextmanager
def _preparedRequest(self, url):
Expand Down Expand Up @@ -510,7 +510,7 @@ def _preparedRequest(self, url):

yield kwargs

self._changed()
self._req_content_type = None
self.timer.stop()

def _absoluteUrl(self, url):
Expand Down
46 changes: 46 additions & 0 deletions src/zope/testbrowser/tests/test_browser.py
Expand Up @@ -425,6 +425,52 @@ def test_reload_after_post():
"""


def test_goBack_changes_cached_html(self):
"""
goBack() causes the browser to clear its cached parsed HTML, so queries
that rely on that use the correct response.
>>> app = TestApp()
>>> browser = Browser(wsgi_app=app)
>>> app.set_next_response(b'''\
... <html>
... <head><title>First page</title></head>
... <body><a href="foo">link</a></body>
... </html>
... ''')
>>> browser.open('http://localhost/')
GET / HTTP/1.1
...
>>> browser.title
'First page'
>>> browser.getLink('link').url
'http://localhost/foo'
>>> app.set_next_response(b'''\
... <html>
... <head><title>Second page</title></head>
... <body><a href="bar">link</a></body>
... </html>
... ''')
>>> browser.open('http://localhost/')
GET / HTTP/1.1
...
>>> browser.title
'Second page'
>>> browser.getLink('link').url
'http://localhost/bar'
After going back, queries once again return answers based on the first
response.
>>> browser.goBack()
>>> browser.title
'First page'
>>> browser.getLink('link').url
'http://localhost/foo'
"""


def test_button_without_name(self):
"""
This once blew up.
Expand Down

0 comments on commit 784e0c6

Please sign in to comment.