1from werkzeug.routing import BaseConverter 2 3from flask import request 4from flask import session 5from flask import url_for 6 7 8def test_custom_converters(app, client): 9 class ListConverter(BaseConverter): 10 def to_python(self, value): 11 return value.split(",") 12 13 def to_url(self, value): 14 base_to_url = super().to_url 15 return ",".join(base_to_url(x) for x in value) 16 17 app.url_map.converters["list"] = ListConverter 18 19 @app.route("/<list:args>") 20 def index(args): 21 return "|".join(args) 22 23 assert client.get("/1,2,3").data == b"1|2|3" 24 25 with app.test_request_context(): 26 assert url_for("index", args=[4, 5, 6]) == "/4,5,6" 27 28 29def test_context_available(app, client): 30 class ContextConverter(BaseConverter): 31 def to_python(self, value): 32 assert request is not None 33 assert session is not None 34 return value 35 36 app.url_map.converters["ctx"] = ContextConverter 37 38 @app.get("/<ctx:name>") 39 def index(name): 40 return name 41 42 assert client.get("/admin").data == b"admin" 43