def test_cors(self): class Cors(Controller): def POST(self): pass res = Response() req = Request() c = Cors(req, res) self.assertTrue(c.OPTIONS) self.assertFalse('Access-Control-Allow-Origin' in c.response.headers) req.set_header('Origin', 'http://example.com') c = Cors(req, res) self.assertEqual(req.get_header('Origin'), c.response.get_header('Access-Control-Allow-Origin')) req.set_header('Access-Control-Request-Method', 'POST') req.set_header('Access-Control-Request-Headers', 'xone, xtwo') c = Cors(req, res) c.OPTIONS() self.assertEqual(req.get_header('Origin'), c.response.get_header('Access-Control-Allow-Origin')) self.assertEqual(req.get_header('Access-Control-Request-Method'), c.response.get_header('Access-Control-Allow-Methods')) self.assertEqual(req.get_header('Access-Control-Request-Headers'), c.response.get_header('Access-Control-Allow-Headers')) c = Cors(req, res) c.POST() self.assertEqual(req.get_header('Origin'), c.response.get_header('Access-Control-Allow-Origin'))
def test_get_header(self): r = Request() r.set_headers({ 'foo': 'bar', 'Content-Type': 'application/json', 'Happy-days': 'are-here-again' }) v = r.get_header('foo', 'che') self.assertEqual('bar', v) v = r.get_header('Foo', 'che') self.assertEqual('bar', v) v = r.get_header('FOO', 'che') self.assertEqual('bar', v) v = r.get_header('che', 'che') self.assertEqual('che', v) v = r.get_header('che') self.assertEqual(None, v) v = r.get_header('content-type') self.assertEqual('application/json', v) v = r.get_header('happy-days') self.assertEqual('are-here-again', v)