10
10
import wsgiref .handlers
11
11
from base64 import b64encode
12
12
from http .server import BaseHTTPRequestHandler
13
- from io import StringIO
14
13
from queue import Queue
15
14
from typing import TYPE_CHECKING
16
15
from unittest import mock
28
27
RateLimit ,
29
28
compile_linkcheck_allowed_redirects ,
30
29
)
31
- from sphinx .errors import ConfigError
32
30
from sphinx .testing .util import SphinxTestApp
33
31
from sphinx .util import requests
34
32
from sphinx .util ._pathlib import _StrPath
@@ -680,7 +678,7 @@ def check_headers(self):
680
678
assert content ['status' ] == 'working'
681
679
682
680
683
- def make_redirect_handler (* , support_head : bool ) -> type [BaseHTTPRequestHandler ]:
681
+ def make_redirect_handler (* , support_head : bool = True ) -> type [BaseHTTPRequestHandler ]:
684
682
class RedirectOnceHandler (BaseHTTPRequestHandler ):
685
683
protocol_version = 'HTTP/1.1'
686
684
@@ -712,17 +710,15 @@ def log_date_time_string(self):
712
710
'linkcheck' ,
713
711
testroot = 'linkcheck-localserver' ,
714
712
freshenv = True ,
713
+ confoverrides = {'linkcheck_allowed_redirects' : None },
715
714
)
716
715
def test_follows_redirects_on_HEAD (app , capsys ):
717
716
with serve_application (app , make_redirect_handler (support_head = True )) as address :
718
717
compile_linkcheck_allowed_redirects (app , app .config )
719
718
app .build ()
720
719
_stdout , stderr = capsys .readouterr ()
721
720
content = (app .outdir / 'output.txt' ).read_text (encoding = 'utf8' )
722
- assert content == (
723
- 'index.rst:1: [redirected with Found] '
724
- f'http://{ address } / to http://{ address } /?redirected=1\n '
725
- )
721
+ assert content == ''
726
722
assert stderr == textwrap .dedent (
727
723
"""\
728
724
127.0.0.1 - - [] "HEAD / HTTP/1.1" 302 -
@@ -736,17 +732,15 @@ def test_follows_redirects_on_HEAD(app, capsys):
736
732
'linkcheck' ,
737
733
testroot = 'linkcheck-localserver' ,
738
734
freshenv = True ,
735
+ confoverrides = {'linkcheck_allowed_redirects' : None },
739
736
)
740
737
def test_follows_redirects_on_GET (app , capsys ):
741
738
with serve_application (app , make_redirect_handler (support_head = False )) as address :
742
739
compile_linkcheck_allowed_redirects (app , app .config )
743
740
app .build ()
744
741
_stdout , stderr = capsys .readouterr ()
745
742
content = (app .outdir / 'output.txt' ).read_text (encoding = 'utf8' )
746
- assert content == (
747
- 'index.rst:1: [redirected with Found] '
748
- f'http://{ address } / to http://{ address } /?redirected=1\n '
749
- )
743
+ assert content == ''
750
744
assert stderr == textwrap .dedent (
751
745
"""\
752
746
127.0.0.1 - - [] "HEAD / HTTP/1.1" 405 -
@@ -757,25 +751,37 @@ def test_follows_redirects_on_GET(app, capsys):
757
751
assert app .warning .getvalue () == ''
758
752
759
753
754
+ @pytest .mark .sphinx (
755
+ 'linkcheck' ,
756
+ testroot = 'linkcheck-localserver' ,
757
+ freshenv = True ,
758
+ confoverrides = {'linkcheck_allowed_redirects' : {}}, # do not follow any redirects
759
+ )
760
+ def test_warns_redirects_on_GET (app , capsys ):
761
+ with serve_application (app , make_redirect_handler ()) as address :
762
+ compile_linkcheck_allowed_redirects (app , app .config )
763
+ app .build ()
764
+ _stdout , stderr = capsys .readouterr ()
765
+ content = (app .outdir / 'output.txt' ).read_text (encoding = 'utf8' )
766
+ assert content == (
767
+ 'index.rst:1: [redirected with Found] '
768
+ f'http://{ address } / to http://{ address } /?redirected=1\n '
769
+ )
770
+ assert stderr == textwrap .dedent (
771
+ """\
772
+ 127.0.0.1 - - [] "HEAD / HTTP/1.1" 302 -
773
+ 127.0.0.1 - - [] "HEAD /?redirected=1 HTTP/1.1" 204 -
774
+ """ ,
775
+ )
776
+ assert len (app .warning .getvalue ().splitlines ()) == 1
777
+
778
+
760
779
def test_linkcheck_allowed_redirects_config (
761
780
make_app : Callable [..., SphinxTestApp ], tmp_path : Path
762
781
) -> None :
763
782
tmp_path .joinpath ('conf.py' ).touch ()
764
783
tmp_path .joinpath ('index.rst' ).touch ()
765
784
766
- # ``linkcheck_allowed_redirects = None`` is rejected
767
- warning_stream = StringIO ()
768
- with pytest .raises (ConfigError ):
769
- make_app (
770
- 'linkcheck' ,
771
- srcdir = tmp_path ,
772
- confoverrides = {'linkcheck_allowed_redirects' : None },
773
- warning = warning_stream ,
774
- )
775
- assert strip_escape_sequences (warning_stream .getvalue ()).splitlines () == [
776
- "WARNING: The config value `linkcheck_allowed_redirects' has type `NoneType'; expected `dict'."
777
- ]
778
-
779
785
# ``linkcheck_allowed_redirects = {}`` is permitted
780
786
app = make_app (
781
787
'linkcheck' ,
0 commit comments