-
Hi!
Below is a very simple test file I made with a bare falcon and nothing else (should work as is, granted falcon 3.0 is installed) from falcon import asgi
from falcon import testing
from falcon.middleware import CORSMiddleware
class DummyResource():
async def on_post(self, req, resp):
resp.media = {"msg": "test"}
async def on_get(self, req, resp):
resp.media = {"msg": "test"}
class BaseFalconTest(testing.TestCase):
def setUp(self):
super(BaseFalconTest, self).setUp()
self.middlewares = [CORSMiddleware(allow_origins="localhost", allow_credentials="localhost")]
self.app = asgi.App(cors_enable=True, middleware=self.middlewares)
self.app.add_route("/test", DummyResource())
class TestCORS(BaseFalconTest):
def test_origin_header(self):
response = self.simulate_get(path="/test", headers={"Origin": "localhost"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {"msg": "test"})
self.assertEqual(response.headers["Access-Control-Allow-Origin".lower()], "*")
self.assertNotIn("Access-Control-Allow-Credentials".lower(), response.headers)
def test_method_options(self):
response = self.simulate_options(
path="/test",
headers={
"Origin": "localhost",
"Access-Control-Request-Method": "POST",
},
)
self.assertEqual(response.status_code, 200)
# False, assertion fails. Access-Control-Allow-Methods is None
self.assertEqual(response.headers["Access-Control-Allow-Methods".lower()], "GET, POST") Would like to hear more about it 😁 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @Clement-O! Having tweaked your test slightly, it seems to pass as expected: from falcon import asgi
from falcon import testing
from falcon.middleware import CORSMiddleware
class DummyResource:
async def on_post(self, req, resp):
resp.media = {'msg': 'test'}
async def on_get(self, req, resp):
resp.media = {'msg': 'test'}
class BaseFalconTest(testing.TestCase):
def setUp(self):
super(BaseFalconTest, self).setUp()
self.middlewares = [
CORSMiddleware(
allow_origins='localhost', allow_credentials='localhost'
)
]
self.app = asgi.App(middleware=self.middlewares)
self.app.add_route('/test', DummyResource())
class TestCORS(BaseFalconTest):
def test_origin_header(self):
response = self.simulate_get(
path='/test', headers={'Origin': 'localhost'}
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json, {'msg': 'test'})
self.assertEqual(
response.headers['Access-Control-Allow-Origin'], 'localhost'
)
self.assertEqual(
response.headers['Access-Control-Allow-Credentials'], 'true'
)
def test_method_options(self):
response = self.simulate_options(
path='/test',
headers={
'Origin': 'localhost',
'Access-Control-Request-Method': 'POST',
},
)
self.assertEqual(response.status_code, 200)
# False, assertion fails. Access-Control-Allow-Methods is None
self.assertEqual(
response.headers['Access-Control-Allow-Methods'.lower()],
'GET, POST',
) The main change I made here is not passing |
Beta Was this translation helpful? Give feedback.
Hi @Clement-O!
I believe the framework is functioning as expected, at least from what I can see.
It is not a bug per se, but a usability bug, which is already tracked as #1977. I have rescheduled it for the upcoming 4.0 release since you're not the first one to get hit by this. (You are also welcome to contribute by tackling this one 🙂 )
Having tweaked your test slightly, it seems to pass as expected: