Executes the given webob Request (``req``), with the expected ``status``. Generally :meth:`~webtest.TestApp.get` and :meth:`~webtest.TestApp.post` are used instead. To use this:: req = webtest.TestRequest.blank('url', ...args...) resp = app
(self, req, status=None, expect_errors=None)
| 589 | ) |
| 590 | |
| 591 | def do_request(self, req, status=None, expect_errors=None): |
| 592 | """ |
| 593 | Executes the given webob Request (``req``), with the expected |
| 594 | ``status``. Generally :meth:`~webtest.TestApp.get` and |
| 595 | :meth:`~webtest.TestApp.post` are used instead. |
| 596 | |
| 597 | To use this:: |
| 598 | |
| 599 | req = webtest.TestRequest.blank('url', ...args...) |
| 600 | resp = app.do_request(req) |
| 601 | |
| 602 | .. note:: |
| 603 | |
| 604 | You can pass any keyword arguments to |
| 605 | ``TestRequest.blank()``, which will be set on the request. |
| 606 | These can be arguments like ``content_type``, ``accept``, etc. |
| 607 | |
| 608 | """ |
| 609 | |
| 610 | errors = StringIO() |
| 611 | req.environ['wsgi.errors'] = errors |
| 612 | script_name = req.environ.get('SCRIPT_NAME', '') |
| 613 | if script_name and req.path_info.startswith(script_name): |
| 614 | req.path_info = req.path_info[len(script_name):] |
| 615 | |
| 616 | # set framework hooks |
| 617 | req.environ['paste.testing'] = True |
| 618 | req.environ['paste.testing_variables'] = {} |
| 619 | |
| 620 | # set request cookies |
| 621 | self.cookiejar.add_cookie_header(utils._RequestCookieAdapter(req)) |
| 622 | |
| 623 | # verify wsgi compatibility |
| 624 | app = lint.middleware(self.app) if self.lint else self.app |
| 625 | |
| 626 | # FIXME: should it be an option to not catch exc_info? |
| 627 | res = req.get_response(app, catch_exc_info=True) |
| 628 | |
| 629 | # be sure to decode the content |
| 630 | res.decode_content() |
| 631 | |
| 632 | # set a few handy attributes |
| 633 | res._use_unicode = self.use_unicode |
| 634 | res.request = req |
| 635 | res.app = app |
| 636 | res.test_app = self |
| 637 | |
| 638 | # We do this to make sure the app_iter is exhausted: |
| 639 | try: |
| 640 | res.body |
| 641 | except TypeError: # pragma: no cover |
| 642 | pass |
| 643 | res.errors = errors.getvalue() |
| 644 | |
| 645 | for name, value in req.environ['paste.testing_variables'].items(): |
| 646 | if hasattr(res, name): |
| 647 | raise ValueError( |
| 648 | "paste.testing_variables contains the variable %r, but " |