2013-11-05 12:04:48 +00:00
|
|
|
|
2013-11-05 12:33:04 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-11-05 12:04:48 +00:00
|
|
|
from werkzeug.test import Client
|
|
|
|
from werkzeug.wrappers import Response
|
|
|
|
|
2013-11-05 12:33:04 +00:00
|
|
|
from isso.wsgi import CORSMiddleware
|
2013-11-09 14:18:21 +00:00
|
|
|
from isso.utils import origin
|
2013-11-05 12:04:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def hello_world(environ, start_response):
|
|
|
|
start_response('200 OK', [('Content-Type', 'text/html')])
|
|
|
|
return ["Hello, World."]
|
|
|
|
|
|
|
|
|
|
|
|
def test_simple_CORS():
|
|
|
|
|
2013-11-09 14:18:21 +00:00
|
|
|
app = CORSMiddleware(hello_world, origin=origin([
|
2013-11-05 12:04:48 +00:00
|
|
|
"https://example.tld/",
|
|
|
|
"http://example.tld/",
|
|
|
|
"http://example.tld",
|
2013-11-09 14:18:21 +00:00
|
|
|
]))
|
2013-11-05 12:04:48 +00:00
|
|
|
|
|
|
|
client = Client(app, Response)
|
|
|
|
|
|
|
|
rv = client.get("/", headers={"ORIGIN": "https://example.tld"})
|
|
|
|
|
|
|
|
assert rv.headers["Access-Control-Allow-Origin"] == "https://example.tld"
|
|
|
|
assert rv.headers["Access-Control-Allow-Headers"] == "Origin, Content-Type"
|
|
|
|
assert rv.headers["Access-Control-Allow-Credentials"] == "true"
|
|
|
|
assert rv.headers["Access-Control-Allow-Methods"] == "GET, POST, PUT, DELETE"
|
|
|
|
assert rv.headers["Access-Control-Expose-Headers"] == "X-Set-Cookie"
|
|
|
|
|
2013-11-09 14:18:21 +00:00
|
|
|
a = client.get("/", headers={"ORIGIN": "http://example.tld"})
|
2013-11-05 12:04:48 +00:00
|
|
|
assert a.headers["Access-Control-Allow-Origin"] == "http://example.tld"
|
|
|
|
|
|
|
|
b = client.get("/", headers={"ORIGIN": "http://example.tld"})
|
2013-11-09 14:18:21 +00:00
|
|
|
assert b.headers["Access-Control-Allow-Origin"] == "http://example.tld"
|
2013-11-05 12:04:48 +00:00
|
|
|
|
2013-11-09 14:18:21 +00:00
|
|
|
c = client.get("/", headers={"ORIGIN": "http://foo.other"})
|
|
|
|
assert c.headers["Access-Control-Allow-Origin"] == "https://example.tld"
|
2013-11-05 12:04:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_preflight_CORS():
|
|
|
|
|
2013-11-09 14:18:21 +00:00
|
|
|
app = CORSMiddleware(hello_world, origin=origin(["http://example.tld"]))
|
2013-11-05 12:04:48 +00:00
|
|
|
client = Client(app, Response)
|
|
|
|
|
|
|
|
rv = client.open(method="OPTIONS", path="/", headers={"ORIGIN": "http://example.tld"})
|
|
|
|
assert rv.status_code == 200
|
|
|
|
|
|
|
|
for hdr in ("Origin", "Headers", "Credentials", "Methods"):
|
|
|
|
assert "Access-Control-Allow-%s" % hdr in rv.headers
|
|
|
|
|
|
|
|
assert rv.headers["Access-Control-Allow-Origin"] == "http://example.tld"
|