15
15
class RedirectTests (BaseDocServing ):
16
16
17
17
def test_root_url_no_slash (self ):
18
- r = self .client .get ('' , HTTP_HOST = ' project.dev.readthedocs.io' )
18
+ r = self .client .get ("" , secure = True , HTTP_HOST = " project.dev.readthedocs.io" )
19
19
self .assertEqual (r .status_code , 302 )
20
20
self .assertEqual (
21
21
r ['Location' ], 'https://project.dev.readthedocs.io/en/latest/' ,
22
22
)
23
23
24
24
def test_root_url (self ):
25
- r = self .client .get ('/' , HTTP_HOST = ' project.dev.readthedocs.io' )
25
+ r = self .client .get ("/" , secure = True , HTTP_HOST = " project.dev.readthedocs.io" )
26
26
self .assertEqual (r .status_code , 302 )
27
27
self .assertEqual (
28
28
r ['Location' ], 'https://project.dev.readthedocs.io/en/latest/' ,
@@ -53,18 +53,22 @@ def test_custom_domain_root_url_no_slash(self):
53
53
def test_single_version_root_url_doesnt_redirect (self ):
54
54
self .project .single_version = True
55
55
self .project .save ()
56
- r = self .client .get ('/' , HTTP_HOST = ' project.dev.readthedocs.io' )
56
+ r = self .client .get ("/" , secure = True , HTTP_HOST = " project.dev.readthedocs.io" )
57
57
self .assertEqual (r .status_code , 200 )
58
58
59
59
def test_subproject_root_url (self ):
60
- r = self .client .get ('/projects/subproject/' , HTTP_HOST = 'project.dev.readthedocs.io' )
60
+ r = self .client .get (
61
+ "/projects/subproject/" , secure = True , HTTP_HOST = "project.dev.readthedocs.io"
62
+ )
61
63
self .assertEqual (r .status_code , 302 )
62
64
self .assertEqual (
63
65
r ['Location' ], 'https://project.dev.readthedocs.io/projects/subproject/en/latest/' ,
64
66
)
65
67
66
68
def test_subproject_root_url_no_slash (self ):
67
- r = self .client .get ('/projects/subproject' , HTTP_HOST = 'project.dev.readthedocs.io' )
69
+ r = self .client .get (
70
+ "/projects/subproject" , secure = True , HTTP_HOST = "project.dev.readthedocs.io"
71
+ )
68
72
self .assertEqual (r .status_code , 302 )
69
73
self .assertEqual (
70
74
r ['Location' ], 'https://project.dev.readthedocs.io/projects/subproject/en/latest/' ,
@@ -73,44 +77,52 @@ def test_subproject_root_url_no_slash(self):
73
77
def test_single_version_subproject_root_url_no_slash (self ):
74
78
self .subproject .single_version = True
75
79
self .subproject .save ()
76
- r = self .client .get ('/projects/subproject' , HTTP_HOST = 'project.dev.readthedocs.io' )
80
+ r = self .client .get (
81
+ "/projects/subproject" , secure = True , HTTP_HOST = "project.dev.readthedocs.io"
82
+ )
77
83
self .assertEqual (r .status_code , 302 )
78
84
self .assertEqual (
79
85
r ['Location' ], 'https://project.dev.readthedocs.io/projects/subproject/' ,
80
86
)
81
87
82
88
def test_subproject_redirect (self ):
83
- r = self .client .get ('/' , HTTP_HOST = ' subproject.dev.readthedocs.io' )
89
+ r = self .client .get ("/" , secure = True , HTTP_HOST = " subproject.dev.readthedocs.io" )
84
90
self .assertEqual (r .status_code , 302 )
85
91
self .assertEqual (
86
92
r ["Location" ],
87
93
"https://project.dev.readthedocs.io/projects/subproject/" ,
88
94
)
89
95
90
96
r = self .client .get (
91
- "/projects/subproject/" , HTTP_HOST = "project.dev.readthedocs.io"
97
+ "/projects/subproject/" , secure = True , HTTP_HOST = "project.dev.readthedocs.io"
92
98
)
93
99
self .assertEqual (r .status_code , 302 )
94
100
self .assertEqual (
95
101
r ["Location" ],
96
102
"https://project.dev.readthedocs.io/projects/subproject/en/latest/" ,
97
103
)
98
104
99
- r = self .client .get ('/en/latest/' , HTTP_HOST = 'subproject.dev.readthedocs.io' )
105
+ r = self .client .get (
106
+ "/en/latest/" , secure = True , HTTP_HOST = "subproject.dev.readthedocs.io"
107
+ )
100
108
self .assertEqual (r .status_code , 302 )
101
109
self .assertEqual (
102
110
r ['Location' ], 'https://project.dev.readthedocs.io/projects/subproject/en/latest/' ,
103
111
)
104
112
105
- r = self .client .get ('/en/latest/foo/bar' , HTTP_HOST = 'subproject.dev.readthedocs.io' )
113
+ r = self .client .get (
114
+ "/en/latest/foo/bar" , secure = True , HTTP_HOST = "subproject.dev.readthedocs.io"
115
+ )
106
116
self .assertEqual (r .status_code , 302 )
107
117
self .assertEqual (
108
118
r ['Location' ], 'https://project.dev.readthedocs.io/projects/subproject/en/latest/foo/bar' ,
109
119
)
110
120
111
121
self .domain .canonical = True
112
122
self .domain .save ()
113
- r = self .client .get ('/en/latest/foo/bar' , HTTP_HOST = 'subproject.dev.readthedocs.io' )
123
+ r = self .client .get (
124
+ "/en/latest/foo/bar" , secure = True , HTTP_HOST = "subproject.dev.readthedocs.io"
125
+ )
114
126
self .assertEqual (r .status_code , 302 )
115
127
self .assertEqual (
116
128
r ['Location' ], 'https://docs1.example.com/projects/subproject/en/latest/foo/bar' ,
@@ -120,28 +132,34 @@ def test_single_version_subproject_redirect(self):
120
132
self .subproject .single_version = True
121
133
self .subproject .save ()
122
134
123
- r = self .client .get ('/' , HTTP_HOST = ' subproject.dev.readthedocs.io' )
135
+ r = self .client .get ("/" , secure = True , HTTP_HOST = " subproject.dev.readthedocs.io" )
124
136
self .assertEqual (r .status_code , 302 )
125
137
self .assertEqual (
126
138
r ['Location' ], 'https://project.dev.readthedocs.io/projects/subproject/' ,
127
139
)
128
140
129
- r = self .client .get ('/foo/bar/' , HTTP_HOST = 'subproject.dev.readthedocs.io' )
141
+ r = self .client .get (
142
+ "/foo/bar/" , secure = True , HTTP_HOST = "subproject.dev.readthedocs.io"
143
+ )
130
144
self .assertEqual (r .status_code , 302 )
131
145
self .assertEqual (
132
146
r ['Location' ], 'https://project.dev.readthedocs.io/projects/subproject/foo/bar/' ,
133
147
)
134
148
135
149
self .domain .canonical = True
136
150
self .domain .save ()
137
- r = self .client .get ('/foo/bar' , HTTP_HOST = 'subproject.dev.readthedocs.io' )
151
+ r = self .client .get (
152
+ "/foo/bar" , secure = True , HTTP_HOST = "subproject.dev.readthedocs.io"
153
+ )
138
154
self .assertEqual (r .status_code , 302 )
139
155
self .assertEqual (
140
156
r ['Location' ], 'https://docs1.example.com/projects/subproject/foo/bar' ,
141
157
)
142
158
143
159
def test_root_redirect_with_query_params (self ):
144
- r = self .client .get ('/?foo=bar' , HTTP_HOST = 'project.dev.readthedocs.io' )
160
+ r = self .client .get (
161
+ "/?foo=bar" , secure = True , HTTP_HOST = "project.dev.readthedocs.io"
162
+ )
145
163
self .assertEqual (r .status_code , 302 )
146
164
self .assertEqual (
147
165
r ['Location' ],
@@ -172,23 +190,29 @@ def test_canonicalize_public_domain_to_cname_redirect(self):
172
190
self .domain .canonical = True
173
191
self .domain .save ()
174
192
175
- r = self .client .get ('/' , HTTP_HOST = ' project.dev.readthedocs.io' )
193
+ r = self .client .get ("/" , secure = True , HTTP_HOST = " project.dev.readthedocs.io" )
176
194
self .assertEqual (r .status_code , 302 )
177
195
self .assertEqual (
178
196
r ['Location' ], f'https://{ self .domain .domain } /' ,
179
197
)
180
198
self .assertEqual (r ["X-RTD-Redirect" ], RedirectType .to_canonical_domain .name )
181
199
182
200
# We should redirect before 404ing
183
- r = self .client .get ('/en/latest/404after302' , HTTP_HOST = 'project.dev.readthedocs.io' )
201
+ r = self .client .get (
202
+ "/en/latest/404after302" ,
203
+ secure = True ,
204
+ HTTP_HOST = "project.dev.readthedocs.io" ,
205
+ )
184
206
self .assertEqual (r .status_code , 302 )
185
207
self .assertEqual (
186
208
r ['Location' ], f'https://{ self .domain .domain } /en/latest/404after302' ,
187
209
)
188
210
self .assertEqual (r ["X-RTD-Redirect" ], RedirectType .to_canonical_domain .name )
189
211
190
212
def test_translation_redirect (self ):
191
- r = self .client .get ('/' , HTTP_HOST = 'translation.dev.readthedocs.io' )
213
+ r = self .client .get (
214
+ "/" , secure = True , HTTP_HOST = "translation.dev.readthedocs.io"
215
+ )
192
216
self .assertEqual (r .status_code , 302 )
193
217
self .assertEqual (
194
218
r ['Location' ], f'https://project.dev.readthedocs.io/es/latest/' ,
@@ -262,6 +286,29 @@ def test_slash_redirect(self):
262
286
# The test client strips multiple slashes at the front of the URL
263
287
# Additional tests for this are in ``test_middleware:test_front_slash``
264
288
289
+ def test_https_public_domain_https_redirect (self ):
290
+ paths = ["/" , "/en/latest/" , "/not-found" ]
291
+ for path in paths :
292
+ r = self .client .get (
293
+ path , secure = False , HTTP_HOST = "project.dev.readthedocs.io"
294
+ )
295
+ self .assertEqual (r .status_code , 302 )
296
+ self .assertEqual (
297
+ r ["Location" ],
298
+ f"https://project.dev.readthedocs.io{ path } " ,
299
+ )
300
+ self .assertEqual (r ["X-RTD-Redirect" ], RedirectType .http_to_https .name )
301
+
302
+ @override_settings (PUBLIC_DOMAIN_USES_HTTPS = False )
303
+ def test_http_public_domain_https_redirect (self ):
304
+ r = self .client .get ("" , secure = False , HTTP_HOST = "project.dev.readthedocs.io" )
305
+ self .assertEqual (r .status_code , 302 )
306
+ self .assertEqual (
307
+ r ["Location" ],
308
+ "http://project.dev.readthedocs.io/en/latest/" ,
309
+ )
310
+ self .assertEqual (r ["X-RTD-Redirect" ], RedirectType .system .name )
311
+
265
312
266
313
class ProxitoV2RedirectTests (RedirectTests ):
267
314
# TODO: remove this class once the new implementation is the default.
0 commit comments