11"""Tests for Flask-WeasyPrint."""
22
3+ from urllib .error import HTTPError
4+
35import pytest
46from flask import Flask , json , jsonify , redirect , request
57from weasyprint import __version__ as weasyprint_version
6- from werkzeug . test import ClientRedirectError
8+ from weasyprint . urls import URLFetcher , URLFetcherResponse
79
810from flask_weasyprint import CSS , HTML , make_url_fetcher , render_pdf
911
@@ -19,25 +21,25 @@ def test_url_fetcher():
1921 with app .test_request_context (base_url = 'http://example.org/bar/' ):
2022 fetcher = make_url_fetcher ()
2123
22- result = fetcher ('http://example.org/bar/' )
23- assert result [ 'string' ] .strip ().startswith (b'<html>' )
24- assert result [ 'mime_type' ] == 'text/html'
25- assert result [ 'encoding' ] == 'utf-8'
26- assert result [ 'redirected_url' ] == 'http://example.org/bar/foo/'
24+ response = fetcher ('http://example.org/bar/' )
25+ assert response . read () .strip ().startswith (b'<html>' )
26+ assert response . content_type == 'text/html'
27+ assert response . charset == 'utf-8'
28+ assert response . url == 'http://example.org/bar/foo/'
2729
28- result = fetcher ('http://example.org/bar/foo/graph?data=1&labels=A' )
29- assert result [ 'string' ] .strip ().startswith (b'<svg xmlns=' )
30- assert result [ 'mime_type' ] == 'image/svg+xml'
30+ response = fetcher ('http://example.org/bar/foo/graph?data=1&labels=A' )
31+ assert response . read () .strip ().startswith (b'<svg xmlns=' )
32+ assert response . content_type == 'image/svg+xml'
3133
3234 # Also works with a custom dispatcher
3335 def custom_dispatcher (url_string ):
3436 return app , 'http://example.org/bar/' , '/foo/graph?data=1&labels=A'
3537 with app .test_request_context (base_url = 'http://example.org/bar/' ):
3638 fetcher = make_url_fetcher (dispatcher = custom_dispatcher )
3739
38- result = fetcher ('test://' )
39- assert result [ 'string' ] .strip ().startswith (b'<svg xmlns=' )
40- assert result [ 'mime_type' ] == 'image/svg+xml'
40+ response = fetcher ('test://' )
41+ assert response . read () .strip ().startswith (b'<svg xmlns=' )
42+ assert response . content_type == 'image/svg+xml'
4143
4244
4345def test_wrappers ():
@@ -103,12 +105,14 @@ def add_redirect(old_url, new_url):
103105
104106 with app .test_request_context ():
105107 fetcher = make_url_fetcher ()
106- result = fetcher ('http://localhost/a' )
107- assert result ['string' ] == b'Ok'
108- assert result ['redirected_url' ] == 'http://localhost/d'
109- with pytest .raises (ClientRedirectError ):
108+ response = fetcher ('http://localhost/a' )
109+ assert response .read () == b'Ok'
110+ assert response .url == 'http://localhost/d'
111+ # TODO: keep HTTPError with WeasyPrint > 68.1, see #2686.
112+ # with pytest.raises(HTTPError, match='infinite loop'):
113+ with pytest .raises ((HTTPError , RecursionError )):
110114 fetcher ('http://localhost/1' )
111- with pytest .raises (ValueError , match = '404' ):
115+ with pytest .raises (HTTPError , match = '404' ):
112116 fetcher ('http://localhost/nonexistent' )
113117
114118
@@ -125,21 +129,22 @@ def catchall(subdomain='', path=None):
125129 app = [subdomain , request .script_root , request .path , query_string ]
126130 return jsonify (app = app )
127131
128- def dummy_fetcher (url ):
129- return {'string' : 'dummy ' + url }
132+ class DummyFetcher (URLFetcher ):
133+ def fetch (self , url , headers = None ):
134+ return URLFetcherResponse (url , f'dummy { url } ' )
130135
131136 def assert_app (url , host , script_root , path , query_string = '' ):
132137 """The URL was dispatched to the app with these parameters."""
133- assert json .loads (dispatcher (url )[ 'string' ] ) == {
138+ assert json .loads (dispatcher (url ). read () ) == {
134139 'app' : [host , script_root , path , query_string ]}
135140
136141 def assert_dummy (url ):
137142 """The URL was not dispatched, the default fetcher was used."""
138- assert dispatcher (url )[ 'string' ] == 'dummy ' + url
143+ assert dispatcher (url ). read () == f 'dummy { url } ' . encode ()
139144
140145 # No SERVER_NAME config, default port
141146 with app .test_request_context (base_url = 'http://a.net/b/' ):
142- dispatcher = make_url_fetcher (next_fetcher = dummy_fetcher )
147+ dispatcher = make_url_fetcher (next_fetcher = DummyFetcher )
143148 assert_app ('http://a.net/b' , '' , '/b' , '/' )
144149 assert_app ('http://a.net/b/' , '' , '/b' , '/' )
145150 assert_app ('http://a.net/b/' , '' , '/b' , '/' )
@@ -153,7 +158,7 @@ def assert_dummy(url):
153158
154159 # No SERVER_NAME config, explicit default port
155160 with app .test_request_context (base_url = 'http://a.net:80/b/' ):
156- dispatcher = make_url_fetcher (next_fetcher = dummy_fetcher )
161+ dispatcher = make_url_fetcher (next_fetcher = DummyFetcher )
157162 assert_app ('http://a.net/b' , '' , '/b' , '/' )
158163 assert_app ('http://a.net/b/' , '' , '/b' , '/' )
159164 assert_app ('http://a.net/b/c/d?e' , '' , '/b' , '/c/d' , 'e' )
@@ -166,7 +171,7 @@ def assert_dummy(url):
166171
167172 # Change the context’s port number
168173 with app .test_request_context (base_url = 'http://a.net:8888/b/' ):
169- dispatcher = make_url_fetcher (next_fetcher = dummy_fetcher )
174+ dispatcher = make_url_fetcher (next_fetcher = DummyFetcher )
170175 assert_app ('http://a.net:8888/b' , '' , '/b' , '/' )
171176 assert_app ('http://a.net:8888/b/' , '' , '/b' , '/' )
172177 assert_app ('http://a.net:8888/b/cd?e' , '' , '/b' , '/cd' , 'e' )
@@ -181,7 +186,7 @@ def assert_dummy(url):
181186 # Add a SERVER_NAME config
182187 app .config ['SERVER_NAME' ] = 'a.net'
183188 with app .test_request_context ():
184- dispatcher = make_url_fetcher (next_fetcher = dummy_fetcher )
189+ dispatcher = make_url_fetcher (next_fetcher = DummyFetcher )
185190 assert_app ('http://a.net' , '' , '' , '/' )
186191 assert_app ('http://a.net/' , '' , '' , '/' )
187192 assert_app ('http://a.net/b/c/d?e' , '' , '' , '/b/c/d' , 'e' )
@@ -195,7 +200,7 @@ def assert_dummy(url):
195200 # SERVER_NAME with a port number
196201 app .config ['SERVER_NAME' ] = 'a.net:8888'
197202 with app .test_request_context ():
198- dispatcher = make_url_fetcher (next_fetcher = dummy_fetcher )
203+ dispatcher = make_url_fetcher (next_fetcher = DummyFetcher )
199204 assert_app ('http://a.net:8888' , '' , '' , '/' )
200205 assert_app ('http://a.net:8888/' , '' , '' , '/' )
201206 assert_app ('http://a.net:8888/b/c/d?e' , '' , '' , '/b/c/d' , 'e' )
@@ -208,11 +213,9 @@ def assert_dummy(url):
208213
209214@pytest .mark .parametrize ('url' , [
210215 'http://example.net/Unïĉodé/pass !' ,
211- 'http://example.net/Unïĉodé/pass !' .encode (),
212216 'http://example.net/foo%20bar/p%61ss%C2%A0!' ,
213- b'http://example.net/foo%20bar/p%61ss%C2%A0!' ,
214217])
215218def test_funky_urls (url ):
216219 with app .test_request_context (base_url = 'http://example.net/' ):
217220 fetcher = make_url_fetcher ()
218- assert fetcher (url )[ 'string' ] == 'pass !' .encode ()
221+ assert fetcher (url ). read () == 'pass !' .encode ()
0 commit comments