# HG changeset patch # User Sylvain Thénault # Date 1389946000 -3600 # Node ID 9c13ebd45cb6d7c72008ee70477bf664d2538024 # Parent 8e87f1e18a37edb2c71519e48b12bbbe3fff0824 [test api] extract a context manager to temporarily disable app.error_handler monkey patch diff -r 8e87f1e18a37 -r 9c13ebd45cb6 devtools/testlib.py --- a/devtools/testlib.py Thu Jan 02 18:30:31 2014 +0100 +++ b/devtools/testlib.py Fri Jan 17 09:06:40 2014 +0100 @@ -1,4 +1,4 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This file is part of CubicWeb. @@ -85,6 +85,28 @@ def parse_string(self, data): return json.loads(data) +@contextmanager +def real_error_handling(app): + """By default, CubicWebTC `app` attribute (ie the publisher) is monkey + patched so that unexpected error are raised rather than going through the + `error_handler` method. + + By using this context manager you disable this monkey-patching temporarily. + Hence when publishihng a request no error will be raised, you'll get + req.status_out set to an HTTP error status code and the generated page will + usually hold a traceback as HTML. + + >>> with real_error_handling(app): + >>> page = app.handle_request(req) + """ + # remove the monkey patched error handler + fake_error_handler = app.error_handler + del app.error_handler + # return the app + yield app + # restore + app.error_handler = fake_error_handler + # email handling, to test emails sent by an application ######################## MAILBOX = [] @@ -707,17 +729,14 @@ return self.ctrl_publish(req, ctrlid, rset) def http_publish(self, url, data=None): - """like `url_publish`, except this returns a http response, even in case of errors""" + """like `url_publish`, except this returns a http response, even in case + of errors. You may give form parameters using the `data` argument. + """ req = self.req_from_url(url) if data is not None: req.form.update(data) - # remove the monkey patched error handler - fake_error_handler = self.app.error_handler - del self.app.error_handler - try: + with real_error_handling(self.app): result = self.app_handle_request(req, req.relative_path(False)) - finally: - self.app.error_handler = fake_error_handler return result, req @staticmethod