python - How to deliberately build bad URL with url_for() in unit tests? -
im writing unit tests api written in python flask. specifically, want test error handling routes work want deliberately build urls missing parameters. want avoid hard-coding using url_for() doesn't allow have missing parameters, how build bad urls ?
when comes url_for
generates 2 kinds of urls. if there named routes, like
@app.route('/favorite/color/<color>') def favorite_color(color): return color[::-1]
then url parameters required:
url_for('favorite_color') builderror: not build url endpoint 'favorite_color'. did forget specify values ['color']?
however, parameter not present in route converted querystring parameter:
print(url_for('favorite_color', color='blue', no='yellooooowwww!')) /favorite/color/blue?no=yellooooowwww%21
so when ask url missing parameter, you're asking url has no route. can't build url that, because flask trying build parameters endpoints exist.
the best can use parameter values out of bounds, e.g.
url_for('favorite_color', color='toaster')
toaster not real color, , should return 404
. missing parameters might make sense in different context, you'll have account that. if want have missing parameters, want use querystring arguments. if you're dead set on making sure urls don't exist on server don't exist on server, can this:
url_for('favorite_color', color='fnord').replace('/fnord', '')
Comments
Post a Comment