@@ -32,37 +32,37 @@ def test_get_origin_vary_by_default(self):
32
32
assert resp ["vary" ] == "origin"
33
33
34
34
def test_get_invalid_origin (self ):
35
- resp = self .client .get ("/" , HTTP_ORIGIN = "http ://example.com]" )
35
+ resp = self .client .get ("/" , HTTP_ORIGIN = "https ://example.com]" )
36
36
assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp
37
37
38
- @override_settings (CORS_ALLOWED_ORIGINS = ["http ://example.com" ])
38
+ @override_settings (CORS_ALLOWED_ORIGINS = ["https ://example.com" ])
39
39
def test_get_not_in_allowed_origins (self ):
40
- resp = self .client .get ("/" , HTTP_ORIGIN = "http ://example.org" )
40
+ resp = self .client .get ("/" , HTTP_ORIGIN = "https ://example.org" )
41
41
assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp
42
42
43
- @override_settings (CORS_ALLOWED_ORIGINS = ["https ://example.org" ])
43
+ @override_settings (CORS_ALLOWED_ORIGINS = ["http ://example.org" ])
44
44
def test_get_not_in_allowed_origins_due_to_wrong_scheme (self ):
45
- resp = self .client .get ("/" , HTTP_ORIGIN = "http ://example.org" )
45
+ resp = self .client .get ("/" , HTTP_ORIGIN = "https ://example.org" )
46
46
assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp
47
47
48
48
@override_settings (
49
- CORS_ALLOWED_ORIGINS = ["http ://example.com" , "http ://example.org" ]
49
+ CORS_ALLOWED_ORIGINS = ["https ://example.com" , "https ://example.org" ]
50
50
)
51
51
def test_get_in_allowed_origins (self ):
52
- resp = self .client .get ("/" , HTTP_ORIGIN = "http ://example.org" )
53
- assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "http ://example.org"
52
+ resp = self .client .get ("/" , HTTP_ORIGIN = "https ://example.org" )
53
+ assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "https ://example.org"
54
54
55
- @override_settings (CORS_ALLOWED_ORIGINS = ["http ://example.org" ])
55
+ @override_settings (CORS_ALLOWED_ORIGINS = ["https ://example.org" ])
56
56
async def test_async_get_in_allowed_origins (self ):
57
- resp = await self .async_client .get ("/async/" , origin = "http ://example.org" )
58
- assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "http ://example.org"
57
+ resp = await self .async_client .get ("/async/" , origin = "https ://example.org" )
58
+ assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "https ://example.org"
59
59
60
- @override_settings (CORS_ALLOWED_ORIGINS = ["http ://example.com" , "null" ])
60
+ @override_settings (CORS_ALLOWED_ORIGINS = ["https ://example.com" , "null" ])
61
61
def test_null_in_allowed_origins (self ):
62
62
resp = self .client .get ("/" , HTTP_ORIGIN = "null" )
63
63
assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "null"
64
64
65
- @override_settings (CORS_ALLOWED_ORIGINS = ["http ://example.com" , "file://" ])
65
+ @override_settings (CORS_ALLOWED_ORIGINS = ["https ://example.com" , "file://" ])
66
66
def test_file_in_allowed_origins (self ):
67
67
"""
68
68
'file://' should be allowed as an origin since Chrome on Android
@@ -76,22 +76,22 @@ def test_file_in_allowed_origins(self):
76
76
CORS_EXPOSE_HEADERS = ["accept" , "content-type" ],
77
77
)
78
78
def test_get_expose_headers (self ):
79
- resp = self .client .get ("/" , HTTP_ORIGIN = "http ://example.com" )
79
+ resp = self .client .get ("/" , HTTP_ORIGIN = "https ://example.com" )
80
80
assert resp [ACCESS_CONTROL_EXPOSE_HEADERS ] == "accept, content-type"
81
81
82
82
@override_settings (CORS_ALLOW_ALL_ORIGINS = True )
83
83
def test_get_dont_expose_headers (self ):
84
- resp = self .client .get ("/" , HTTP_ORIGIN = "http ://example.com" )
84
+ resp = self .client .get ("/" , HTTP_ORIGIN = "https ://example.com" )
85
85
assert ACCESS_CONTROL_EXPOSE_HEADERS not in resp
86
86
87
87
@override_settings (CORS_ALLOW_CREDENTIALS = True , CORS_ALLOW_ALL_ORIGINS = True )
88
88
def test_get_allow_credentials (self ):
89
- resp = self .client .get ("/" , HTTP_ORIGIN = "http ://example.com" )
89
+ resp = self .client .get ("/" , HTTP_ORIGIN = "https ://example.com" )
90
90
assert resp [ACCESS_CONTROL_ALLOW_CREDENTIALS ] == "true"
91
91
92
92
@override_settings (CORS_ALLOW_ALL_ORIGINS = True )
93
93
def test_get_dont_allow_credentials (self ):
94
- resp = self .client .get ("/" , HTTP_ORIGIN = "http ://example.com" )
94
+ resp = self .client .get ("/" , HTTP_ORIGIN = "https ://example.com" )
95
95
assert ACCESS_CONTROL_ALLOW_CREDENTIALS not in resp
96
96
97
97
@override_settings (
@@ -103,7 +103,7 @@ def test_get_dont_allow_credentials(self):
103
103
def test_options_allowed_origin (self ):
104
104
resp = self .client .options (
105
105
"/" ,
106
- HTTP_ORIGIN = "http ://example.com" ,
106
+ HTTP_ORIGIN = "https ://example.com" ,
107
107
HTTP_ACCESS_CONTROL_REQUEST_METHOD = "GET" ,
108
108
)
109
109
assert resp .status_code == HTTPStatus .OK
@@ -120,7 +120,7 @@ def test_options_allowed_origin(self):
120
120
async def test_async_options_allowed_origin (self ):
121
121
resp = await self .async_client .options (
122
122
"/async/" ,
123
- origin = "http ://example.com" ,
123
+ origin = "https ://example.com" ,
124
124
access_control_request_method = "GET" ,
125
125
)
126
126
assert resp .status_code == HTTPStatus .OK
@@ -137,23 +137,23 @@ async def test_async_options_allowed_origin(self):
137
137
def test_options_no_max_age (self ):
138
138
resp = self .client .options (
139
139
"/" ,
140
- HTTP_ORIGIN = "http ://example.com" ,
140
+ HTTP_ORIGIN = "https ://example.com" ,
141
141
HTTP_ACCESS_CONTROL_REQUEST_METHOD = "GET" ,
142
142
)
143
143
assert resp [ACCESS_CONTROL_ALLOW_HEADERS ] == "content-type"
144
144
assert resp [ACCESS_CONTROL_ALLOW_METHODS ] == "GET, OPTIONS"
145
145
assert ACCESS_CONTROL_MAX_AGE not in resp
146
146
147
147
@override_settings (
148
- CORS_ALLOWED_ORIGINS = ["http ://localhost:9000" ],
148
+ CORS_ALLOWED_ORIGINS = ["https ://localhost:9000" ],
149
149
)
150
150
def test_options_allowed_origins_with_port (self ):
151
151
resp = self .client .options (
152
152
"/" ,
153
- HTTP_ORIGIN = "http ://localhost:9000" ,
153
+ HTTP_ORIGIN = "https ://localhost:9000" ,
154
154
HTTP_ACCESS_CONTROL_REQUEST_METHOD = "GET" ,
155
155
)
156
- assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "http ://localhost:9000"
156
+ assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "https ://localhost:9000"
157
157
158
158
@override_settings (
159
159
CORS_ALLOWED_ORIGIN_REGEXES = [r"^https://\w+\.example\.com$" ],
@@ -196,7 +196,7 @@ def test_options_doesnt_add_origin_when_domain_not_found_in_allowed_regexes(
196
196
def test_options_empty_request_method (self ):
197
197
resp = self .client .options (
198
198
"/" ,
199
- HTTP_ORIGIN = "http ://example.com" ,
199
+ HTTP_ORIGIN = "https ://example.com" ,
200
200
HTTP_ACCESS_CONTROL_REQUEST_METHOD = "" ,
201
201
)
202
202
assert resp .status_code == 200
@@ -209,22 +209,22 @@ def test_options_no_headers(self):
209
209
def test_allow_all_origins_get (self ):
210
210
resp = self .client .get (
211
211
"/" ,
212
- HTTP_ORIGIN = "http ://example.com" ,
212
+ HTTP_ORIGIN = "https ://example.com" ,
213
213
HTTP_ACCESS_CONTROL_REQUEST_METHOD = "GET" ,
214
214
)
215
215
assert resp .status_code == 200
216
- assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "http ://example.com"
216
+ assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "https ://example.com"
217
217
assert resp ["vary" ] == "origin"
218
218
219
219
@override_settings (CORS_ALLOW_CREDENTIALS = True , CORS_ALLOW_ALL_ORIGINS = True )
220
220
def test_allow_all_origins_options (self ):
221
221
resp = self .client .options (
222
222
"/" ,
223
- HTTP_ORIGIN = "http ://example.com" ,
223
+ HTTP_ORIGIN = "https ://example.com" ,
224
224
HTTP_ACCESS_CONTROL_REQUEST_METHOD = "GET" ,
225
225
)
226
226
assert resp .status_code == 200
227
- assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "http ://example.com"
227
+ assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "https ://example.com"
228
228
assert resp ["vary" ] == "origin"
229
229
230
230
@override_settings (CORS_ALLOW_CREDENTIALS = True , CORS_ALLOW_ALL_ORIGINS = True )
@@ -237,9 +237,9 @@ def test_non_200_headers_still_set(self):
237
237
place to preserve this behaviour. See `ExceptionMiddleware` mention here:
238
238
https://docs.djangoproject.com/en/3.0/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware # noqa: E501
239
239
"""
240
- resp = self .client .get ("/test-401/" , HTTP_ORIGIN = "http ://example.com" )
240
+ resp = self .client .get ("/test-401/" , HTTP_ORIGIN = "https ://example.com" )
241
241
assert resp .status_code == 401
242
- assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "http ://example.com"
242
+ assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "https ://example.com"
243
243
244
244
@override_settings (CORS_ALLOW_CREDENTIALS = True , CORS_ALLOW_ALL_ORIGINS = True )
245
245
def test_auth_view_options (self ):
@@ -249,11 +249,11 @@ def test_auth_view_options(self):
249
249
"""
250
250
resp = self .client .options (
251
251
"/test-401/" ,
252
- HTTP_ORIGIN = "http ://example.com" ,
252
+ HTTP_ORIGIN = "https ://example.com" ,
253
253
HTTP_ACCESS_CONTROL_REQUEST_METHOD = "GET" ,
254
254
)
255
255
assert resp .status_code == 200
256
- assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "http ://example.com"
256
+ assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "https ://example.com"
257
257
assert resp ["Content-Length" ] == "0"
258
258
259
259
def test_signal_handler_that_returns_false (self ):
@@ -263,7 +263,7 @@ def handler(*args, **kwargs):
263
263
with temporary_check_request_hander (handler ):
264
264
resp = self .client .options (
265
265
"/" ,
266
- HTTP_ORIGIN = "http ://example.com" ,
266
+ HTTP_ORIGIN = "https ://example.com" ,
267
267
HTTP_ACCESS_CONTROL_REQUEST_METHOD = "GET" ,
268
268
)
269
269
@@ -277,35 +277,35 @@ def handler(*args, **kwargs):
277
277
with temporary_check_request_hander (handler ):
278
278
resp = self .client .options (
279
279
"/" ,
280
- HTTP_ORIGIN = "http ://example.com" ,
280
+ HTTP_ORIGIN = "https ://example.com" ,
281
281
HTTP_ACCESS_CONTROL_REQUEST_METHOD = "GET" ,
282
282
)
283
283
assert resp .status_code == 200
284
- assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "http ://example.com"
284
+ assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "https ://example.com"
285
285
286
- @override_settings (CORS_ALLOWED_ORIGINS = ["http ://example.com" ])
286
+ @override_settings (CORS_ALLOWED_ORIGINS = ["https ://example.com" ])
287
287
def test_signal_handler_allow_some_urls_to_everyone (self ):
288
288
def allow_api_to_all (sender , request , ** kwargs ):
289
289
return request .path .startswith ("/api/" )
290
290
291
291
with temporary_check_request_hander (allow_api_to_all ):
292
292
resp = self .client .options (
293
293
"/" ,
294
- HTTP_ORIGIN = "http ://example.org" ,
294
+ HTTP_ORIGIN = "https ://example.org" ,
295
295
HTTP_ACCESS_CONTROL_REQUEST_METHOD = "GET" ,
296
296
)
297
297
assert resp .status_code == 200
298
298
assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp
299
299
300
300
resp = self .client .options (
301
301
"/api/something/" ,
302
- HTTP_ORIGIN = "http ://example.org" ,
302
+ HTTP_ORIGIN = "https ://example.org" ,
303
303
HTTP_ACCESS_CONTROL_REQUEST_METHOD = "GET" ,
304
304
)
305
305
assert resp .status_code == 200
306
- assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "http ://example.org"
306
+ assert resp [ACCESS_CONTROL_ALLOW_ORIGIN ] == "https ://example.org"
307
307
308
- @override_settings (CORS_ALLOWED_ORIGINS = ["http ://example.com" ])
308
+ @override_settings (CORS_ALLOWED_ORIGINS = ["https ://example.com" ])
309
309
def test_signal_called_once_during_normal_flow (self ):
310
310
calls = 0
311
311
@@ -315,69 +315,69 @@ def allow_all(sender, request, **kwargs):
315
315
return True
316
316
317
317
with temporary_check_request_hander (allow_all ):
318
- self .client .get ("/" , HTTP_ORIGIN = "http ://example.org" )
318
+ self .client .get ("/" , HTTP_ORIGIN = "https ://example.org" )
319
319
320
320
assert calls == 1
321
321
322
- @override_settings (CORS_ALLOWED_ORIGINS = ["http ://example.com" ])
323
- @prepend_middleware ("tests.test_middleware .ShortCircuitMiddleware" )
322
+ @override_settings (CORS_ALLOWED_ORIGINS = ["https ://example.com" ])
323
+ @prepend_middleware (f" { __name__ } .ShortCircuitMiddleware" )
324
324
def test_get_short_circuit (self ):
325
325
"""
326
326
Test a scenario when a middleware that returns a response is run before
327
327
the ``CorsMiddleware``. In this case
328
328
``CorsMiddleware.process_response()`` should ignore the request if
329
329
MIDDLEWARE setting is used (new mechanism in Django 1.10+).
330
330
"""
331
- resp = self .client .get ("/" , HTTP_ORIGIN = "http ://example.com" )
331
+ resp = self .client .get ("/" , HTTP_ORIGIN = "https ://example.com" )
332
332
assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp
333
333
334
334
@override_settings (
335
- CORS_ALLOWED_ORIGINS = ["http ://example.com" ], CORS_URLS_REGEX = r"^/foo/$"
335
+ CORS_ALLOWED_ORIGINS = ["https ://example.com" ], CORS_URLS_REGEX = r"^/foo/$"
336
336
)
337
- @prepend_middleware (__name__ + " .ShortCircuitMiddleware" )
337
+ @prepend_middleware (f" { __name__ } .ShortCircuitMiddleware" )
338
338
def test_get_short_circuit_should_be_ignored (self ):
339
- resp = self .client .get ("/" , HTTP_ORIGIN = "http ://example.com" )
339
+ resp = self .client .get ("/" , HTTP_ORIGIN = "https ://example.com" )
340
340
assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp
341
341
342
342
@override_settings (
343
- CORS_ALLOWED_ORIGINS = ["http ://example.com" ], CORS_URLS_REGEX = r"^/foo/$"
343
+ CORS_ALLOWED_ORIGINS = ["https ://example.com" ], CORS_URLS_REGEX = r"^/foo/$"
344
344
)
345
345
def test_get_regex_matches (self ):
346
- resp = self .client .get ("/foo/" , HTTP_ORIGIN = "http ://example.com" )
346
+ resp = self .client .get ("/foo/" , HTTP_ORIGIN = "https ://example.com" )
347
347
assert ACCESS_CONTROL_ALLOW_ORIGIN in resp
348
348
349
349
@override_settings (
350
- CORS_ALLOWED_ORIGINS = ["http ://example.com" ], CORS_URLS_REGEX = r"^/not-foo/$"
350
+ CORS_ALLOWED_ORIGINS = ["https ://example.com" ], CORS_URLS_REGEX = r"^/not-foo/$"
351
351
)
352
352
def test_get_regex_doesnt_match (self ):
353
- resp = self .client .get ("/foo/" , HTTP_ORIGIN = "http ://example.com" )
353
+ resp = self .client .get ("/foo/" , HTTP_ORIGIN = "https ://example.com" )
354
354
assert ACCESS_CONTROL_ALLOW_ORIGIN not in resp
355
355
356
356
@override_settings (
357
- CORS_ALLOWED_ORIGINS = ["http ://example.com" ], CORS_URLS_REGEX = r"^/foo/$"
357
+ CORS_ALLOWED_ORIGINS = ["https ://example.com" ], CORS_URLS_REGEX = r"^/foo/$"
358
358
)
359
359
def test_get_regex_matches_path_info (self ):
360
360
resp = self .client .get (
361
- "/foo/" , HTTP_ORIGIN = "http ://example.com" , SCRIPT_NAME = "/prefix/"
361
+ "/foo/" , HTTP_ORIGIN = "https ://example.com" , SCRIPT_NAME = "/prefix/"
362
362
)
363
363
assert ACCESS_CONTROL_ALLOW_ORIGIN in resp
364
364
365
- @override_settings (CORS_ALLOWED_ORIGINS = ["http ://example.com" ])
365
+ @override_settings (CORS_ALLOWED_ORIGINS = ["https ://example.com" ])
366
366
def test_cors_enabled_is_attached_and_bool (self ):
367
367
"""
368
368
Ensure that request._cors_enabled is available - although a private API
369
369
someone might use it for debugging
370
370
"""
371
- resp = self .client .get ("/" , HTTP_ORIGIN = "http ://example.com" )
371
+ resp = self .client .get ("/" , HTTP_ORIGIN = "https ://example.com" )
372
372
request = resp .wsgi_request
373
373
assert isinstance (request ._cors_enabled , bool ) # type: ignore [attr-defined]
374
374
assert request ._cors_enabled # type: ignore [attr-defined]
375
375
376
- @override_settings (CORS_ALLOWED_ORIGINS = ["http ://example.com" ])
376
+ @override_settings (CORS_ALLOWED_ORIGINS = ["https ://example.com" ])
377
377
def test_works_if_view_deletes_cors_enabled (self ):
378
378
"""
379
379
Just in case something crazy happens in the view or other middleware,
380
380
check that get_response doesn't fall over if `_cors_enabled` is removed
381
381
"""
382
- resp = self .client .get ("/delete-is-enabled/" , HTTP_ORIGIN = "http ://example.com" )
382
+ resp = self .client .get ("/delete-is-enabled/" , HTTP_ORIGIN = "https ://example.com" )
383
383
assert ACCESS_CONTROL_ALLOW_ORIGIN in resp
0 commit comments