我已经编写了一个flask服务器,它在某些情况下将用户重定向到外部站点。我使用pythonunittest
模块编写了一些单元测试。对于其中一些正在测试重定向部分的人,我得到了werkzeug.routing.BuildError
。以下是其中一个测试用例的代码:
with app.app_context(), app.test_request_context():
response = self.app.get('/{0}'.format(test_url), follow_redirects=False)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.location, url_for('https://www.linkedin.com/in/zeinab-abbasimazar-0327aa47', _external=True, _scheme='https'))
这是完整的堆栈跟踪:
Ran 1 test in 3.211s
FAILED (errors=1)
Error
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
yield
File "/usr/lib/python3.6/unittest/case.py", line 605, in run
testMethod()
File "/home/zeinab/PycharmProjects/url_shortener/tests.py", line 167, in test_get_long_existing_url
self.assertEqual(response.location, url_for(long_url, _external=True, _scheme='https'))
File "/home/zeinab/.local/lib/python3.6/site-packages/flask/helpers.py", line 370, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "/home/zeinab/.local/lib/python3.6/site-packages/flask/app.py", line 2215, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "/home/zeinab/.local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/zeinab/.local/lib/python3.6/site-packages/flask/helpers.py", line 358, in url_for
endpoint, values, method=method, force_external=external
File "/home/zeinab/.local/lib/python3.6/site-packages/werkzeug/routing.py", line 2020, in build
raise BuildError(endpoint, values, method, self)
werkzeug.routing.BuildError: Could not build url for endpoint 'https://www.linkedin.com/in/zeinab-abbasimazar-0327aa47'. Did you mean 'static' instead?
Assertion failed
在setUp
方法中,我还有以下几行:
app.config['PREFERRED_URL_SCHEME'] = 'https'
我尝试了修补url_for
方法,如this question中所述;但这并没有改变我的结果
我还尝试了_force_https
方法来解释here,但没有看到任何变化
当我读到this page时,我打印出了app.config['wsgi.url_scheme']
,它是https
我正在Ubuntu系统上使用Python3.6。有人能帮我纠正这个错误吗
直接与url一起使用字符串,而不使用
url_for()
通常
url_for()
也会创建带有url的字符串,但它只为代码中的函数名url_for(function_name)
而不是url相关问题 更多 >
编程相关推荐