Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Executes the given request (``req``), with the expected
``status``. Generally ``.get()`` and ``.post()`` are used
instead.
"""
__tracebackhide__ = True
errors = StringIO()
req.environ['wsgi.errors'] = errors
if self.cookies:
c = BaseCookie()
for name, value in self.cookies.items():
c[name] = value
req.environ['HTTP_COOKIE'] = str(c).split(': ', 1)[1]
req.environ['paste.testing'] = True
req.environ['paste.testing_variables'] = {}
app = lint.middleware(self.app)
old_stdout = sys.stdout
out = CaptureStdout(old_stdout)
try:
sys.stdout = out
start_time = time.time()
## FIXME: should it be an option to not catch exc_info?
res = req.get_response(app, catch_exc_info=True)
end_time = time.time()
finally:
sys.stdout = old_stdout
sys.stderr.write(out.getvalue())
res.app = app
res.test_app = self
# We do this to make sure the app_iter is exausted:
res.body
res.errors = errors.getvalue()
Note you can pass any keyword arguments to
``TestRequest.blank()``, which will be set on the request.
These can be arguments like ``content_type``, ``accept``, etc.
"""
__tracebackhide__ = True
errors = StringIO()
req.environ['wsgi.errors'] = errors
if self.cookies:
cookie_header = ''.join([
'%s="%s"; ' % (name, cookie_quote(value))
for name, value in self.cookies.items()])
req.environ['HTTP_COOKIE'] = cookie_header
req.environ['paste.testing'] = True
req.environ['paste.testing_variables'] = {}
app = lint.middleware(self.app)
old_stdout = sys.stdout
out = CaptureStdout(old_stdout)
try:
sys.stdout = out
start_time = time.time()
## FIXME: should it be an option to not catch exc_info?
res = req.get_response(app, catch_exc_info=True)
end_time = time.time()
finally:
sys.stdout = old_stdout
res.app = app
res.test_app = self
# We do this to make sure the app_iter is exausted:
res.body
res.errors = errors.getvalue()
total_time = end_time - start_time
def test_lint_iterator_returned(self):
linter = middleware(lambda x, y: None) # None is not an iterator
msg = "The application must return an iterator, if only an empty list"
with self.assertRaisesRegexp(AssertionError, msg):
linter({'wsgi.input': 'foo', 'wsgi.errors': 'foo'}, 'foo')
errors = StringIO()
req.environ['wsgi.errors'] = errors
script_name = req.environ.get('SCRIPT_NAME', '')
if script_name and req.path_info.startswith(script_name):
req.path_info = req.path_info[len(script_name):]
# set framework hooks
req.environ['paste.testing'] = True
req.environ['paste.testing_variables'] = {}
# set request cookies
self.cookiejar.add_cookie_header(utils._RequestCookieAdapter(req))
# verify wsgi compatibility
app = lint.middleware(self.app) if self.lint else self.app
# FIXME: should it be an option to not catch exc_info?
res = req.get_response(app, catch_exc_info=True)
# be sure to decode the content
res.decode_content()
# set a few handy attributes
res._use_unicode = self.use_unicode
res.request = req
res.app = app
res.test_app = self
# We do this to make sure the app_iter is exhausted:
try:
res.body
def do_request(app, expect_status=500):
app = lint.middleware(app)
app = ErrorMiddleware(app, {}, debug=True)
app = clear_middleware(app)
testapp = TestApp(app)
res = testapp.get('', status=expect_status,
expect_errors=True)
return res