From 61bed33f242903e0964cc1e7d860bba128fd7ccb Mon Sep 17 00:00:00 2001 From: Rodrigo Chacon Date: Thu, 10 Jan 2013 03:56:24 -0200 Subject: [PATCH] Adding CORS support for all requests --- httpbin/core.py | 9 +++++++++ test_httpbin.py | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/httpbin/core.py b/httpbin/core.py index 358b0f1..3c51c00 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -38,6 +38,15 @@ app = Flask(__name__) sentry = Sentry(app) +# ----------- +# Middlewares +# ----------- +@app.after_request +def set_cors_headers(response): + response.headers['Access-Control-Allow-Origin'] = '*' + return response + + # ------ # Routes # ------ diff --git a/test_httpbin.py b/test_httpbin.py index 8d7149c..7504b8d 100755 --- a/test_httpbin.py +++ b/test_httpbin.py @@ -41,6 +41,10 @@ class HttpbinTestCase(unittest.TestCase): response = self.app.post('/post', data={"file": f}) self.assertEquals(response.status_code, 200) + def test_set_cors_headers_after_request(self): + response = self.app.get('/get') + self.assertEquals(response.headers.get('Access-Control-Allow-Origin'), '*') + if __name__ == '__main__': unittest.main()