-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_all.py
150 lines (121 loc) · 3.97 KB
/
test_all.py
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
import re
import cherrypy
from cherrypy.test import helper
import cherrypy_cors
class CORSRequests:
def test_bare_request(self):
self.getPage('/')
self.assertBody('hello')
self.assertNoHeader('Access-Control-Allow-Origin')
def test_CORS_request(self):
headers = list(
{
'Origin': 'https://example.com',
}.items()
)
self.getPage('/', headers=headers)
self.assertBody('hello')
self.assertHeader('Access-Control-Allow-Origin', 'https://example.com')
class CORSSimpleServerTests(CORSRequests, helper.CPWebCase):
@staticmethod
def setup_server():
class Root:
@cherrypy.expose
def index(self):
return "hello"
config = {
'/': {
'cors.expose.on': True,
},
}
cherrypy.tree.mount(Root(), config=config)
cherrypy_cors.install()
class CORSSimpleDecoratorTests(CORSRequests, helper.CPWebCase):
@staticmethod
def setup_server():
class Root:
@cherrypy.expose
@cherrypy_cors.tools.expose()
def index(self):
return "hello"
cherrypy.tree.mount(Root())
cherrypy_cors.install()
class OriginRequests:
trusted_origin = 'https://example.com'
untrusted_origin = 'http://attacker.com'
def test_bare_request(self):
self.getPage('/')
self.assertBody('hello')
self.assertNoHeader('Access-Control-Allow-Origin')
def test_matching_origin_request(self):
headers = list(
{
'Origin': self.trusted_origin,
}.items()
)
self.getPage('/', headers=headers)
self.assertBody('hello')
self.assertHeader('Access-Control-Allow-Origin', self.trusted_origin)
def test_non_matching_origin_request(self):
headers = list(
{
'Origin': self.untrusted_origin,
}.items()
)
self.getPage('/', headers=headers)
self.assertBody('hello')
self.assertNoHeader('Access-Control-Allow-Origin')
def test_preflight_request(self):
headers = list(
{
'Origin': self.trusted_origin,
}.items()
)
self.getPage('/', method='OPTIONS', headers=headers)
self.assertBody('hello')
self.assertHeader('Access-Control-Allow-Origin', self.trusted_origin)
def test_non_matching_preflight_request(self):
headers = list(
{
'Origin': self.untrusted_origin,
}.items()
)
self.getPage('/', method='OPTIONS', headers=headers)
self.assertBody('hello')
self.assertNoHeader('Access-Control-Allow-Origin')
class CORSOriginServerTests(OriginRequests, helper.CPWebCase):
@staticmethod
def setup_server():
class Root:
@cherrypy.expose
def index(self):
return "hello"
origins = 'http://example.com', 'https://example.com'
config = {
'/': {
'cors.expose.on': True,
'cors.expose.origins': origins,
'cors.preflight.origins': origins,
},
}
cherrypy.tree.mount(Root(), config=config)
cherrypy_cors.install()
class CORSOriginRegexTests(OriginRequests, helper.CPWebCase):
trusted_origin = 'https://svr5.example.com'
untrusted_origin = 'http://example.com'
@staticmethod
def setup_server():
class Root:
@cherrypy.expose
def index(self):
return "hello"
pattern = re.compile(r'(http|https)://svr[1-9]\.example\.com')
config = {
'/': {
'cors.expose.on': True,
'cors.expose.origins': [pattern],
'cors.preflight.origins': [pattern],
},
}
cherrypy.tree.mount(Root(), config=config)
cherrypy_cors.install()