File: test-wsgicors.py

package info (click to toggle)
wsgicors 0.4.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 124 kB
  • sloc: python: 278; makefile: 3
file content (197 lines) | stat: -rw-r--r-- 7,592 bytes parent folder | download | duplicates (3)
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
# -*- encoding: utf-8 -*-
#
# This file is part of wsgicors
#
# wsgicors is a WSGI middleware that answers CORS preflight requests
# 
# copyright 2014-2015 Norman Krämer
#
# 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 webob import Request, Response
from wsgicors import make_middleware as mw
from nose import with_setup

deny = {"policy":"deny"}

free = {"policy":"pol", 
        "pol_origin":"*", 
        "pol_methods":"*", 
        "pol_headers":"*",
        "pol_credentials":"true",
        "pol_maxage":"100"
        }

wildcard = {"policy":"pol", 
        "pol_origin":"example.com example?.com *.example.com", 
        "pol_methods":"*", 
        "pol_headers":"*",
        "pol_credentials":"true",
        "pol_maxage":"100"
        }

free_nocred = {"policy":"pol", 
        "pol_origin":"*", 
        "pol_methods":"*", 
        "pol_headers":"*",
        "pol_credentials":"false",
        "pol_maxage":"100"
        }

verbatim = {"policy":"pol", 
        "pol_origin":"example.com", 
        "pol_methods":"put,delete", 
        "pol_headers":"header1,header2",
        "pol_credentials":"true",
        "pol_maxage":"100"
        }

post2 = post = preflight = None

def setup():
    global preflight, deniedpreflight, allowedpreflight, post, post2, post3

    preflight = Request.blank("/")
    preflight.method="OPTIONS"
    preflight.headers["Access-Control-Request-Method"] = "post"
    preflight.headers["Access-Control-Request-Headers"] = "*"

    deniedpreflight = Request.blank("/")
    deniedpreflight.method="OPTIONS"
    deniedpreflight.headers["Access-Control-Request-Method"] = "post"
    deniedpreflight.headers["Access-Control-Request-Headers"] = "*"
    deniedpreflight.headers["Origin"] = "somedomain.com"

    allowedpreflight = Request.blank("/")
    allowedpreflight.method="OPTIONS"
    allowedpreflight.headers["Access-Control-Request-Method"] = "post"
    allowedpreflight.headers["Access-Control-Request-Headers"] = "*"
    allowedpreflight.headers["Origin"] = "sub.example.com"

    post = Request.blank("/")
    post.method="POST"
    post.headers["Origin"] = "example.com"

    post2 = Request.blank("/")
    post2.method="POST"
    post2.headers["Origin"] = "example2.com"

    post3 = Request.blank("/")
    post3.method="POST"
    post3.headers["Origin"] = "sub.example.com"

@with_setup(setup)
def testdeny():
    corsed = mw(Response(), deny)
    res = preflight.get_response(corsed)
    assert "Access-Control-Allow-Origin" not in res.headers
    assert "Access-Control-Allow-Credentials" not in res.headers
    assert "Access-Control-Allow-Methods" not in res.headers
    assert "Access-Control-Allow-Headers" not in res.headers
    assert "Access-Control-Max-Age" not in res.headers

    res = post.get_response(corsed)
    assert "Access-Control-Allow-Origin" not in res.headers
    assert "Access-Control-Allow-Credentials" not in res.headers

@with_setup(setup)
def testfree():
    corsed = mw(Response(), free)
    res = preflight.get_response(corsed)
    assert res.headers.get("Access-Control-Allow-Origin", "") == "*"
    assert res.headers.get("Access-Control-Allow-Credentials", "") == "true"
    assert res.headers.get("Access-Control-Allow-Methods", "") == "post"
    assert res.headers.get("Access-Control-Allow-Headers", "") == "*"
    assert res.headers.get("Access-Control-Max-Age", "0") == "100"

    res = post.get_response(corsed)
    assert res.headers.get("Access-Control-Allow-Origin", "") == "example.com"
    assert res.headers.get("Access-Control-Allow-Credentials", "") == "true"

@with_setup(setup)
def testwildcard():
    corsed = mw(Response(), wildcard)
    res = deniedpreflight.get_response(corsed)
    assert res.headers.get("Access-Control-Allow-Origin", "") == ""
    assert res.headers.get("Access-Control-Allow-Credentials", "") == "true"
    assert res.headers.get("Access-Control-Allow-Methods", "") == "post"
    assert res.headers.get("Access-Control-Allow-Headers", "") == "*"
    assert res.headers.get("Access-Control-Max-Age", "0") == "100"

    res = allowedpreflight.get_response(corsed)
    assert res.headers.get("Access-Control-Allow-Origin", "") == "sub.example.com"
    assert res.headers.get("Access-Control-Allow-Credentials", "") == "true"
    assert res.headers.get("Access-Control-Allow-Methods", "") == "post"
    assert res.headers.get("Access-Control-Allow-Headers", "") == "*"
    assert res.headers.get("Access-Control-Max-Age", "0") == "100"

    res = post.get_response(corsed)
    assert res.headers.get("Access-Control-Allow-Origin", "") == "example.com"
    assert res.headers.get("Access-Control-Allow-Credentials", "") == "true"

    res = post3.get_response(corsed)
    assert res.headers.get("Access-Control-Allow-Origin", "") == "sub.example.com"
    assert res.headers.get("Access-Control-Allow-Credentials", "") == "true"


@with_setup(setup)
def testfree_nocred():
    """
    similar to free, but the actual request will be answered 
    with a '*' for allowed origin
    """

    corsed = mw(Response(), free_nocred)
    res = preflight.get_response(corsed)
    assert res.headers.get("Access-Control-Allow-Origin", "") == "*"
    assert res.headers.get("Access-Control-Allow-Credentials", None) == None
    assert res.headers.get("Access-Control-Allow-Methods", "") == "post"
    assert res.headers.get("Access-Control-Allow-Headers", "") == "*"
    assert res.headers.get("Access-Control-Max-Age", "0") == "100"

    res = post.get_response(corsed)
    assert res.headers.get("Access-Control-Allow-Origin", "") == "*"
    assert res.headers.get("Access-Control-Allow-Credentials", None) == None

@with_setup(setup)
def testverbatim():

    corsed = mw(Response(), verbatim)
    res = preflight.get_response(corsed)
    assert res.headers.get("Access-Control-Allow-Origin", "") == "example.com"
    assert res.headers.get("Access-Control-Allow-Credentials", "") == "true"
    assert res.headers.get("Access-Control-Allow-Methods", "") == "put,delete"
    assert res.headers.get("Access-Control-Allow-Headers", "") == "header1,header2"
    assert res.headers.get("Access-Control-Max-Age", "0") == "100"

    res = post.get_response(corsed)
    assert res.headers.get("Access-Control-Allow-Origin", "") == "example.com"
    assert res.headers.get("Access-Control-Allow-Credentials", "") == "true"

@with_setup(setup)
def test_req_origin_no_match():
    "sending a post from a disallowed host => no allow headers will be returned"

    corsed = mw(Response(), verbatim)
    res = preflight.get_response(corsed)
    assert res.headers.get("Access-Control-Allow-Origin", "") == "example.com"
    assert res.headers.get("Access-Control-Allow-Credentials", "") == "true"
    assert res.headers.get("Access-Control-Allow-Methods", "") == "put,delete"
    assert res.headers.get("Access-Control-Allow-Headers", "") == "header1,header2"
    assert res.headers.get("Access-Control-Max-Age", "0") == "100"

    res = post2.get_response(corsed)
    assert "Access-Control-Allow-Origin" not in res.headers
    assert "Access-Control-Allow-Credentials" not in res.headers