简体   繁体   中英

Using url_for in flask for external URLs returns werkzeug.routing.BuildError

I have written a flask server which in some cases redirects the user to external sites. I wrote some unit tests using python unittest module. For some of them which are testing the redirect part, I get werkzeug.routing.BuildError . Here is the code for one of the test cases:

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'))

And this is the full stack trace:

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

I have also following line in the setUp method:

app.config['PREFERRED_URL_SCHEME'] = 'https'

I tried patching url_for method as described in this question ; but it didn't change my result.

I also tried the _force_https method explained here and saw no change.

I printed out the app.config['wsgi.url_scheme'] when I read this page and it was https .

I am using python 3.6 on an Ubuntu system. Can anyone help me fix this error?

Use directly string with url without url_for()

self.assertEqual(response.location, 'https://www.linkedin.com/in/zeinab-abbasimazar-0327aa47')

Normally url_for() also creates string with url but it do it only for function names in your code - url_for(function_name) - not for urls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM