/usr/lib/python2.7/dist-packages/oslo_middleware/tests/test_cors.py is in python-oslo.middleware 3.34.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 | # Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import fixture
from oslotest import base as test_base
import webob
import webob.dec
import webob.exc as exc
from oslo_middleware import cors
@webob.dec.wsgify
def test_application(req):
if req.path_info == '/server_cors':
# Mirror back the origin in the request.
response = webob.Response(status=200)
response.headers['Access-Control-Allow-Origin'] = \
req.headers['Origin']
response.headers['X-Server-Generated-Response'] = '1'
return response
if req.path_info == '/server_cors_vary':
# Mirror back the origin in the request.
response = webob.Response(status=200)
response.headers['Vary'] = 'Custom-Vary'
return response
if req.path_info == '/server_no_cors':
# Send a response with no CORS headers.
response = webob.Response(status=200)
return response
if req.method == 'OPTIONS':
raise exc.HTTPNotFound()
return 'Hello World'
class CORSTestBase(test_base.BaseTestCase):
"""Base class for all CORS tests.
Sets up applications and helper methods.
"""
def setUp(self):
"""Setup the tests."""
super(CORSTestBase, self).setUp()
# Set up the config fixture.
self.config_fixture = self.useFixture(fixture.Config())
self.config = self.config_fixture.conf
def assertCORSResponse(self, response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None,
vary='Origin',
has_content_type=False):
"""Test helper for CORS response headers.
Assert all the headers in a given response. By default, we assume
the response is empty.
"""
# Assert response status.
self.assertEqual(status, response.status)
# Assert the Access-Control-Allow-Origin header.
self.assertHeader(response,
'Access-Control-Allow-Origin',
allow_origin)
# Assert the Access-Control-Max-Age header.
self.assertHeader(response,
'Access-Control-Max-Age',
max_age)
# Assert the Access-Control-Allow-Methods header.
self.assertHeader(response,
'Access-Control-Allow-Methods',
allow_methods)
# Assert the Access-Control-Allow-Headers header.
self.assertHeader(response,
'Access-Control-Allow-Headers',
allow_headers)
# Assert the Access-Control-Allow-Credentials header.
self.assertHeader(response,
'Access-Control-Allow-Credentials',
allow_credentials)
# Assert the Access-Control-Expose-Headers header.
self.assertHeader(response,
'Access-Control-Expose-Headers',
expose_headers)
# Assert no Content-Type added.
if not has_content_type:
self.assertHeader(response, 'Content-Type')
# If we're expecting an origin response, also assert that the
# Vary: Origin header is set, since this implementation of the CORS
# specification permits multiple origin domains.
if allow_origin:
self.assertHeader(response, 'Vary', vary)
def assertHeader(self, response, header, value=None):
if value:
self.assertIn(header, response.headers)
self.assertEqual(str(value),
response.headers[header])
else:
self.assertNotIn(header, response.headers)
class CORSTestDefaultOverrides(CORSTestBase):
def setUp(self):
super(CORSTestDefaultOverrides, self).setUp()
fixture = self.config_fixture # Line length accommodation
fixture.load_raw_values(group='cors',
allowed_origin='http://valid.example.com')
fixture.load_raw_values(group='cors.override_creds',
allowed_origin='http://creds.example.com',
allow_credentials='True')
fixture.load_raw_values(group='cors.override_headers',
allowed_origin='http://headers.example.com',
expose_headers='X-Header-1,X-Header-2',
allow_headers='X-Header-1,X-Header-2')
self.override_opts = {
'expose_headers': ['X-Header-1'],
'allow_headers': ['X-Header-2'],
'allow_methods': ['GET', 'DELETE'],
'allow_credentials': False,
'max_age': 10
}
def test_config_defaults(self):
"""Assert that using set_defaults overrides the appropriate values."""
cors.set_defaults(**self.override_opts)
for opt in cors.CORS_OPTS:
if opt.dest in self.override_opts:
self.assertEqual(self.override_opts[opt.dest], opt.default)
def test_invalid_default_option(self):
"""Assert that using set_defaults only permits valid options."""
self.assertRaises(AttributeError,
cors.set_defaults,
allowed_origin='test')
def test_cascading_override(self):
"""Assert that using set_defaults overrides cors.* config values."""
# set defaults
cors.set_defaults(**self.override_opts)
# Now that the config is set up, create our application.
self.application = cors.CORS(test_application, self.config)
# Check the global configuration for expected values:
gc = self.config.cors
self.assertEqual(['http://valid.example.com'], gc.allowed_origin)
self.assertEqual(self.override_opts['allow_credentials'],
gc.allow_credentials)
self.assertEqual(self.override_opts['expose_headers'],
gc.expose_headers)
self.assertEqual(10, gc.max_age)
self.assertEqual(self.override_opts['allow_methods'],
gc.allow_methods)
self.assertEqual(self.override_opts['allow_headers'],
gc.allow_headers)
# Check the child configuration for expected values:
cc = self.config['cors.override_creds']
self.assertEqual(['http://creds.example.com'], cc.allowed_origin)
self.assertTrue(cc.allow_credentials)
self.assertEqual(self.override_opts['expose_headers'],
cc.expose_headers)
self.assertEqual(10, cc.max_age)
self.assertEqual(self.override_opts['allow_methods'],
cc.allow_methods)
self.assertEqual(self.override_opts['allow_headers'],
cc.allow_headers)
# Check the other child configuration for expected values:
ec = self.config['cors.override_headers']
self.assertEqual(['http://headers.example.com'], ec.allowed_origin)
self.assertEqual(self.override_opts['allow_credentials'],
ec.allow_credentials)
self.assertEqual(['X-Header-1', 'X-Header-2'], ec.expose_headers)
self.assertEqual(10, ec.max_age)
self.assertEqual(self.override_opts['allow_methods'],
ec.allow_methods)
self.assertEqual(['X-Header-1', 'X-Header-2'], ec.allow_headers)
class CORSTestFilterFactory(CORSTestBase):
"""Test the CORS filter_factory method."""
def test_filter_factory(self):
self.config([])
# Test a valid filter.
filter = cors.filter_factory(None,
allowed_origin='http://valid.example.com',
allow_credentials='False',
max_age='',
expose_headers='',
allow_methods='GET',
allow_headers='')
application = filter(test_application)
self.assertIn('http://valid.example.com', application.allowed_origins)
config = application.allowed_origins['http://valid.example.com']
self.assertEqual(False, config['allow_credentials'])
self.assertIsNone(config['max_age'])
self.assertEqual([], config['expose_headers'])
self.assertEqual(['GET'], config['allow_methods'])
self.assertEqual([], config['allow_headers'])
def test_filter_factory_multiorigin(self):
self.config([])
# Test a valid filter.
filter = cors.filter_factory(None,
allowed_origin='http://valid.example.com,'
'http://other.example.com')
application = filter(test_application)
self.assertIn('http://valid.example.com', application.allowed_origins)
self.assertIn('http://other.example.com', application.allowed_origins)
def test_no_origin_fail(self):
'''Assert that a filter factory with no allowed_origin fails.'''
self.assertRaises(TypeError,
cors.filter_factory,
global_conf=None,
# allowed_origin=None, # Expected value.
allow_credentials='False',
max_age='',
expose_headers='',
allow_methods='GET',
allow_headers='')
def test_no_origin_but_oslo_config_project(self):
'''Assert that a filter factory with oslo_config_project succeed.'''
cors.filter_factory(global_conf=None, oslo_config_project='foobar')
def test_cor_config_sections_with_defaults(self):
'''Assert cors.* config sections with default values work.'''
# Set up the config fixture.
self.config_fixture.load_raw_values(group='cors.subdomain')
# Now that the config is set up, create our application.
self.application = cors.CORS(test_application, self.config)
class CORSRegularRequestTest(CORSTestBase):
"""CORS Specification Section 6.1
http://www.w3.org/TR/cors/#resource-requests
"""
# List of HTTP methods (other than OPTIONS) to test with.
methods = ['POST', 'PUT', 'DELETE', 'GET', 'TRACE', 'HEAD']
def setUp(self):
"""Setup the tests."""
super(CORSRegularRequestTest, self).setUp()
fixture = self.config_fixture # Line length accommodation
fixture.load_raw_values(group='cors',
allowed_origin='http://valid.example.com',
allow_credentials='False',
max_age='',
expose_headers='',
allow_methods='GET',
allow_headers='')
fixture.load_raw_values(group='cors.credentials',
allowed_origin='http://creds.example.com',
allow_credentials='True')
fixture.load_raw_values(group='cors.exposed-headers',
allowed_origin='http://headers.example.com',
expose_headers='X-Header-1,X-Header-2',
allow_headers='X-Header-1,X-Header-2')
fixture.load_raw_values(group='cors.cached',
allowed_origin='http://cached.example.com',
max_age='3600')
fixture.load_raw_values(group='cors.get-only',
allowed_origin='http://get.example.com',
allow_methods='GET')
fixture.load_raw_values(group='cors.all-methods',
allowed_origin='http://all.example.com',
allow_methods='GET,PUT,POST,DELETE,HEAD')
fixture.load_raw_values(group='cors.duplicate',
allowed_origin='http://domain1.example.com,'
'http://domain2.example.com')
# Now that the config is set up, create our application.
self.application = cors.CORS(test_application, self.config)
def test_config_overrides(self):
"""Assert that the configuration options are properly registered."""
# Confirm global configuration
gc = self.config.cors
self.assertEqual(['http://valid.example.com'], gc.allowed_origin)
self.assertEqual(False, gc.allow_credentials)
self.assertEqual([], gc.expose_headers)
self.assertIsNone(gc.max_age)
self.assertEqual(['GET'], gc.allow_methods)
self.assertEqual([], gc.allow_headers)
# Confirm credentials overrides.
cc = self.config['cors.credentials']
self.assertEqual(['http://creds.example.com'], cc.allowed_origin)
self.assertEqual(True, cc.allow_credentials)
self.assertEqual(gc.expose_headers, cc.expose_headers)
self.assertEqual(gc.max_age, cc.max_age)
self.assertEqual(gc.allow_methods, cc.allow_methods)
self.assertEqual(gc.allow_headers, cc.allow_headers)
# Confirm exposed-headers overrides.
ec = self.config['cors.exposed-headers']
self.assertEqual(['http://headers.example.com'], ec.allowed_origin)
self.assertEqual(gc.allow_credentials, ec.allow_credentials)
self.assertEqual(['X-Header-1', 'X-Header-2'], ec.expose_headers)
self.assertEqual(gc.max_age, ec.max_age)
self.assertEqual(gc.allow_methods, ec.allow_methods)
self.assertEqual(['X-Header-1', 'X-Header-2'], ec.allow_headers)
# Confirm cached overrides.
chc = self.config['cors.cached']
self.assertEqual(['http://cached.example.com'], chc.allowed_origin)
self.assertEqual(gc.allow_credentials, chc.allow_credentials)
self.assertEqual(gc.expose_headers, chc.expose_headers)
self.assertEqual(3600, chc.max_age)
self.assertEqual(gc.allow_methods, chc.allow_methods)
self.assertEqual(gc.allow_headers, chc.allow_headers)
# Confirm get-only overrides.
goc = self.config['cors.get-only']
self.assertEqual(['http://get.example.com'], goc.allowed_origin)
self.assertEqual(gc.allow_credentials, goc.allow_credentials)
self.assertEqual(gc.expose_headers, goc.expose_headers)
self.assertEqual(gc.max_age, goc.max_age)
self.assertEqual(['GET'], goc.allow_methods)
self.assertEqual(gc.allow_headers, goc.allow_headers)
# Confirm all-methods overrides.
ac = self.config['cors.all-methods']
self.assertEqual(['http://all.example.com'], ac.allowed_origin)
self.assertEqual(gc.allow_credentials, ac.allow_credentials)
self.assertEqual(gc.expose_headers, ac.expose_headers)
self.assertEqual(gc.max_age, ac.max_age)
self.assertEqual(['GET', 'PUT', 'POST', 'DELETE', 'HEAD'],
ac.allow_methods)
self.assertEqual(gc.allow_headers, ac.allow_headers)
# Confirm duplicate domains.
ac = self.config['cors.duplicate']
self.assertEqual(['http://domain1.example.com',
'http://domain2.example.com'],
ac.allowed_origin)
self.assertEqual(gc.allow_credentials, ac.allow_credentials)
self.assertEqual(gc.expose_headers, ac.expose_headers)
self.assertEqual(gc.max_age, ac.max_age)
self.assertEqual(gc.allow_methods, ac.allow_methods)
self.assertEqual(gc.allow_headers, ac.allow_headers)
def test_no_origin_header(self):
"""CORS Specification Section 6.1.1
If the Origin header is not present terminate this set of steps. The
request is outside the scope of this specification.
"""
for method in self.methods:
request = webob.Request.blank('/')
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None,
has_content_type=True)
def test_origin_headers(self):
"""CORS Specification Section 6.1.2
If the value of the Origin header is not a case-sensitive match for
any of the values in list of origins, do not set any additional
headers and terminate this set of steps.
"""
# Test valid origin header.
for method in self.methods:
request = webob.Request.blank('/')
request.method = method
request.headers['Origin'] = 'http://valid.example.com'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://valid.example.com',
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None,
has_content_type=True)
# Test origin header not present in configuration.
for method in self.methods:
request = webob.Request.blank('/')
request.method = method
request.headers['Origin'] = 'http://invalid.example.com'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None,
has_content_type=True)
# Test valid, but case-mismatched origin header.
for method in self.methods:
request = webob.Request.blank('/')
request.method = method
request.headers['Origin'] = 'http://VALID.EXAMPLE.COM'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None,
has_content_type=True)
# Test valid header from list of duplicates.
for method in self.methods:
request = webob.Request.blank('/')
request.method = method
request.headers['Origin'] = 'http://domain2.example.com'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://domain2.example.com',
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None,
has_content_type=True)
def test_supports_credentials(self):
"""CORS Specification Section 6.1.3
If the resource supports credentials add a single
Access-Control-Allow-Origin header, with the value of the Origin header
as value, and add a single Access-Control-Allow-Credentials header with
the case-sensitive string "true" as value.
Otherwise, add a single Access-Control-Allow-Origin header, with
either the value of the Origin header or the string "*" as value.
NOTE: We never use the "*" as origin.
"""
# Test valid origin header without credentials.
for method in self.methods:
request = webob.Request.blank('/')
request.method = method
request.headers['Origin'] = 'http://valid.example.com'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://valid.example.com',
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None,
has_content_type=True)
# Test valid origin header with credentials
for method in self.methods:
request = webob.Request.blank('/')
request.method = method
request.headers['Origin'] = 'http://creds.example.com'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://creds.example.com',
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials="true",
expose_headers=None,
has_content_type=True)
def test_expose_headers(self):
"""CORS Specification Section 6.1.4
If the list of exposed headers is not empty add one or more
Access-Control-Expose-Headers headers, with as values the header field
names given in the list of exposed headers.
"""
for method in self.methods:
request = webob.Request.blank('/')
request.method = method
request.headers['Origin'] = 'http://headers.example.com'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://headers.example.com',
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers='X-Header-1,X-Header-2',
has_content_type=True)
def test_application_options_response(self):
"""Assert that an application provided OPTIONS response is honored.
If the underlying application, via middleware or other, provides a
CORS response, its response should be honored.
"""
test_origin = 'http://creds.example.com'
request = webob.Request.blank('/server_cors')
request.method = "GET"
request.headers['Origin'] = test_origin
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
# If the regular CORS handling catches this request, it should set
# the allow credentials header. This makes sure that it doesn't.
self.assertNotIn('Access-Control-Allow-Credentials', response.headers)
self.assertEqual(response.headers['Access-Control-Allow-Origin'],
test_origin)
self.assertEqual(response.headers['X-Server-Generated-Response'],
'1')
def test_application_vary_respected(self):
"""Assert that an application's provided Vary header is persisted.
If the underlying application, via middleware or other, provides a
Vary header, its response should be honored.
"""
request = webob.Request.blank('/server_cors_vary')
request.method = "GET"
request.headers['Origin'] = 'http://valid.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://valid.example.com',
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None,
vary='Custom-Vary,Origin',
has_content_type=True)
class CORSPreflightRequestTest(CORSTestBase):
"""CORS Specification Section 6.2
http://www.w3.org/TR/cors/#resource-preflight-requests
"""
def setUp(self):
super(CORSPreflightRequestTest, self).setUp()
fixture = self.config_fixture # Line length accommodation
fixture.load_raw_values(group='cors',
allowed_origin='http://valid.example.com',
allow_credentials='False',
max_age='',
expose_headers='',
allow_methods='GET',
allow_headers='')
fixture.load_raw_values(group='cors.credentials',
allowed_origin='http://creds.example.com',
allow_credentials='True')
fixture.load_raw_values(group='cors.exposed-headers',
allowed_origin='http://headers.example.com',
expose_headers='X-Header-1,X-Header-2',
allow_headers='X-Header-1,X-Header-2')
fixture.load_raw_values(group='cors.cached',
allowed_origin='http://cached.example.com',
max_age='3600')
fixture.load_raw_values(group='cors.get-only',
allowed_origin='http://get.example.com',
allow_methods='GET')
fixture.load_raw_values(group='cors.all-methods',
allowed_origin='http://all.example.com',
allow_methods='GET,PUT,POST,DELETE,HEAD')
# Now that the config is set up, create our application.
self.application = cors.CORS(test_application, self.config)
def test_config_overrides(self):
"""Assert that the configuration options are properly registered."""
# Confirm global configuration
gc = self.config.cors
self.assertEqual(gc.allowed_origin, ['http://valid.example.com'])
self.assertEqual(gc.allow_credentials, False)
self.assertEqual(gc.expose_headers, [])
self.assertIsNone(gc.max_age)
self.assertEqual(gc.allow_methods, ['GET'])
self.assertEqual(gc.allow_headers, [])
# Confirm credentials overrides.
cc = self.config['cors.credentials']
self.assertEqual(['http://creds.example.com'], cc.allowed_origin)
self.assertEqual(True, cc.allow_credentials)
self.assertEqual(gc.expose_headers, cc.expose_headers)
self.assertEqual(gc.max_age, cc.max_age)
self.assertEqual(gc.allow_methods, cc.allow_methods)
self.assertEqual(gc.allow_headers, cc.allow_headers)
# Confirm exposed-headers overrides.
ec = self.config['cors.exposed-headers']
self.assertEqual(['http://headers.example.com'], ec.allowed_origin)
self.assertEqual(gc.allow_credentials, ec.allow_credentials)
self.assertEqual(['X-Header-1', 'X-Header-2'], ec.expose_headers)
self.assertEqual(gc.max_age, ec.max_age)
self.assertEqual(gc.allow_methods, ec.allow_methods)
self.assertEqual(['X-Header-1', 'X-Header-2'], ec.allow_headers)
# Confirm cached overrides.
chc = self.config['cors.cached']
self.assertEqual(['http://cached.example.com'], chc.allowed_origin)
self.assertEqual(gc.allow_credentials, chc.allow_credentials)
self.assertEqual(gc.expose_headers, chc.expose_headers)
self.assertEqual(3600, chc.max_age)
self.assertEqual(gc.allow_methods, chc.allow_methods)
self.assertEqual(gc.allow_headers, chc.allow_headers)
# Confirm get-only overrides.
goc = self.config['cors.get-only']
self.assertEqual(['http://get.example.com'], goc.allowed_origin)
self.assertEqual(gc.allow_credentials, goc.allow_credentials)
self.assertEqual(gc.expose_headers, goc.expose_headers)
self.assertEqual(gc.max_age, goc.max_age)
self.assertEqual(['GET'], goc.allow_methods)
self.assertEqual(gc.allow_headers, goc.allow_headers)
# Confirm all-methods overrides.
ac = self.config['cors.all-methods']
self.assertEqual(['http://all.example.com'], ac.allowed_origin)
self.assertEqual(gc.allow_credentials, ac.allow_credentials)
self.assertEqual(gc.expose_headers, ac.expose_headers)
self.assertEqual(gc.max_age, ac.max_age)
self.assertEqual(ac.allow_methods,
['GET', 'PUT', 'POST', 'DELETE', 'HEAD'])
self.assertEqual(gc.allow_headers, ac.allow_headers)
def test_no_origin_header(self):
"""CORS Specification Section 6.2.1
If the Origin header is not present terminate this set of steps. The
request is outside the scope of this specification.
"""
request = webob.Request.blank('/')
request.method = "OPTIONS"
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None)
def test_case_sensitive_origin(self):
"""CORS Specification Section 6.2.2
If the value of the Origin header is not a case-sensitive match for
any of the values in list of origins do not set any additional headers
and terminate this set of steps.
"""
# Test valid domain
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://valid.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://valid.example.com',
max_age=None,
allow_methods='GET',
allow_headers='',
allow_credentials=None,
expose_headers=None)
# Test invalid domain
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://invalid.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None)
# Test case-sensitive mismatch domain
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://VALID.EXAMPLE.COM'
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None)
def test_simple_header_response(self):
"""CORS Specification Section 3
A header is said to be a simple header if the header field name is an
ASCII case-insensitive match for Accept, Accept-Language, or
Content-Language or if it is an ASCII case-insensitive match for
Content-Type and the header field value media type (excluding
parameters) is an ASCII case-insensitive match for
application/x-www-form-urlencoded, multipart/form-data, or text/plain.
NOTE: We are not testing the media type cases.
"""
simple_headers = ','.join([
'accept',
'accept-language',
'content-language',
'content-type'
])
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://valid.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
request.headers['Access-Control-Request-Headers'] = simple_headers
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://valid.example.com',
max_age=None,
allow_methods='GET',
allow_headers=simple_headers,
allow_credentials=None,
expose_headers=None)
def test_no_request_method(self):
"""CORS Specification Section 6.2.3
If there is no Access-Control-Request-Method header or if parsing
failed, do not set any additional headers and terminate this set of
steps. The request is outside the scope of this specification.
"""
# Test valid domain, valid method.
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://get.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://get.example.com',
max_age=None,
allow_methods='GET',
allow_headers=None,
allow_credentials=None,
expose_headers=None)
# Test valid domain, invalid HTTP method.
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://valid.example.com'
request.headers['Access-Control-Request-Method'] = 'TEAPOT'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None)
# Test valid domain, no HTTP method.
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://valid.example.com'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None)
def test_invalid_method(self):
"""CORS Specification Section 6.2.3
If method is not a case-sensitive match for any of the values in
list of methods do not set any additional headers and terminate this
set of steps.
"""
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://get.example.com'
request.headers['Access-Control-Request-Method'] = 'get'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None)
def test_no_parse_request_headers(self):
"""CORS Specification Section 6.2.4
If there are no Access-Control-Request-Headers headers let header
field-names be the empty list.
If parsing failed do not set any additional headers and terminate
this set of steps. The request is outside the scope of this
specification.
"""
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://headers.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
request.headers['Access-Control-Request-Headers'] = 'value with spaces'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None)
def test_no_request_headers(self):
"""CORS Specification Section 6.2.4
If there are no Access-Control-Request-Headers headers let header
field-names be the empty list.
"""
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://headers.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
request.headers['Access-Control-Request-Headers'] = ''
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://headers.example.com',
max_age=None,
allow_methods='GET',
allow_headers=None,
allow_credentials=None,
expose_headers=None)
def test_request_headers(self):
"""CORS Specification Section 6.2.4
Let header field-names be the values as result of parsing the
Access-Control-Request-Headers headers.
If there are no Access-Control-Request-Headers headers let header
field-names be the empty list.
"""
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://headers.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
request.headers['Access-Control-Request-Headers'] = 'X-Header-1,' \
'X-Header-2'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://headers.example.com',
max_age=None,
allow_methods='GET',
allow_headers='X-Header-1,X-Header-2',
allow_credentials=None,
expose_headers=None)
def test_request_headers_not_permitted(self):
"""CORS Specification Section 6.2.4, 6.2.6
If there are no Access-Control-Request-Headers headers let header
field-names be the empty list.
If any of the header field-names is not a ASCII case-insensitive
match for any of the values in list of headers do not set any
additional headers and terminate this set of steps.
"""
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://headers.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
request.headers['Access-Control-Request-Headers'] = 'X-Not-Exposed,' \
'X-Never-Exposed'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None)
def test_credentials(self):
"""CORS Specification Section 6.2.7
If the resource supports credentials add a single
Access-Control-Allow-Origin header, with the value of the Origin header
as value, and add a single Access-Control-Allow-Credentials header with
the case-sensitive string "true" as value.
Otherwise, add a single Access-Control-Allow-Origin header, with either
the value of the Origin header or the string "*" as value.
NOTE: We never use the "*" as origin.
"""
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://creds.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://creds.example.com',
max_age=None,
allow_methods='GET',
allow_headers=None,
allow_credentials="true",
expose_headers=None)
def test_optional_max_age(self):
"""CORS Specification Section 6.2.8
Optionally add a single Access-Control-Max-Age header with as value
the amount of seconds the user agent is allowed to cache the result of
the request.
"""
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://cached.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://cached.example.com',
max_age=3600,
allow_methods='GET',
allow_headers=None,
allow_credentials=None,
expose_headers=None)
def test_allow_methods(self):
"""CORS Specification Section 6.2.9
Add one or more Access-Control-Allow-Methods headers consisting of
(a subset of) the list of methods.
Since the list of methods can be unbounded, simply returning the method
indicated by Access-Control-Request-Method (if supported) can be
enough.
"""
for method in ['GET', 'PUT', 'POST', 'DELETE']:
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://all.example.com'
request.headers['Access-Control-Request-Method'] = method
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://all.example.com',
max_age=None,
allow_methods=method,
allow_headers=None,
allow_credentials=None,
expose_headers=None)
for method in ['PUT', 'POST', 'DELETE']:
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://get.example.com'
request.headers['Access-Control-Request-Method'] = method
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin=None,
max_age=None,
allow_methods=None,
allow_headers=None,
allow_credentials=None,
expose_headers=None)
def test_allow_headers(self):
"""CORS Specification Section 6.2.10
Add one or more Access-Control-Allow-Headers headers consisting of
(a subset of) the list of headers.
If each of the header field-names is a simple header and none is
Content-Type, this step may be skipped.
If a header field name is a simple header and is not Content-Type, it
is not required to be listed. Content-Type is to be listed as only a
subset of its values makes it qualify as simple header.
"""
requested_headers = 'Content-Type,X-Header-1,Cache-Control,Expires,' \
'Last-Modified,Pragma'
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://headers.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
request.headers['Access-Control-Request-Headers'] = requested_headers
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://headers.example.com',
max_age=None,
allow_methods='GET',
allow_headers=requested_headers,
allow_credentials=None,
expose_headers=None)
def test_application_options_response(self):
"""Assert that an application provided OPTIONS response is honored.
If the underlying application, via middleware or other, provides a
CORS response, its response should be honored.
"""
test_origin = 'http://creds.example.com'
request = webob.Request.blank('/server_cors')
request.method = "OPTIONS"
request.headers['Origin'] = test_origin
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
# If the regular CORS handling catches this request, it should set
# the allow credentials header. This makes sure that it doesn't.
self.assertNotIn('Access-Control-Allow-Credentials', response.headers)
self.assertEqual(test_origin,
response.headers['Access-Control-Allow-Origin'])
self.assertEqual('1',
response.headers['X-Server-Generated-Response'])
# If the application returns an OPTIONS response without CORS
# headers, assert that we apply headers.
request = webob.Request.blank('/server_no_cors')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://get.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://get.example.com',
max_age=None,
allow_methods='GET',
allow_headers=None,
allow_credentials=None,
expose_headers=None,
has_content_type=True)
class CORSTestWildcard(CORSTestBase):
"""Test the CORS wildcard specification."""
def setUp(self):
super(CORSTestWildcard, self).setUp()
fixture = self.config_fixture # Line length accommodation
fixture.load_raw_values(group='cors',
allowed_origin='http://default.example.com',
allow_credentials='True',
max_age='',
expose_headers='',
allow_methods='GET,PUT,POST,DELETE,HEAD',
allow_headers='')
fixture.load_raw_values(group='cors.wildcard',
allowed_origin='*',
allow_methods='GET')
# Now that the config is set up, create our application.
self.application = cors.CORS(test_application, self.config)
def test_config_overrides(self):
"""Assert that the configuration options are properly registered."""
# Confirm global configuration
gc = self.config.cors
self.assertEqual(['http://default.example.com'], gc.allowed_origin)
self.assertEqual(True, gc.allow_credentials)
self.assertEqual([], gc.expose_headers)
self.assertIsNone(gc.max_age)
self.assertEqual(['GET', 'PUT', 'POST', 'DELETE', 'HEAD'],
gc.allow_methods)
self.assertEqual([], gc.allow_headers)
# Confirm all-methods overrides.
ac = self.config['cors.wildcard']
self.assertEqual(['*'], ac.allowed_origin)
self.assertEqual(True, gc.allow_credentials)
self.assertEqual(gc.expose_headers, ac.expose_headers)
self.assertEqual(gc.max_age, ac.max_age)
self.assertEqual(['GET'], ac.allow_methods)
self.assertEqual(gc.allow_headers, ac.allow_headers)
def test_wildcard_domain(self):
"""CORS Specification, Wildcards
If the configuration file specifies CORS settings for the wildcard '*'
domain, it should return those for all origin domains except for the
overrides.
"""
# Test valid domain
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://default.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://default.example.com',
max_age=None,
allow_methods='GET',
allow_headers='',
allow_credentials='true',
expose_headers=None)
# Test valid domain
request = webob.Request.blank('/')
request.method = "GET"
request.headers['Origin'] = 'http://default.example.com'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='http://default.example.com',
max_age=None,
allow_headers='',
allow_credentials='true',
expose_headers=None,
has_content_type=True)
# Test invalid domain
request = webob.Request.blank('/')
request.method = "OPTIONS"
request.headers['Origin'] = 'http://invalid.example.com'
request.headers['Access-Control-Request-Method'] = 'GET'
response = request.get_response(self.application)
self.assertCORSResponse(response,
status='200 OK',
allow_origin='*',
max_age=None,
allow_methods='GET',
allow_headers='',
allow_credentials='true',
expose_headers=None,
has_content_type=True)
|