From 12b8c2e5136a04906ea402a435b22948641b4e5c Mon Sep 17 00:00:00 2001 From: Sean Creeley Date: Thu, 23 Feb 2012 09:58:58 -0500 Subject: [PATCH] RequestHander is set up to handle "OPTIONS" requests, but the SimpleAsyncHTTPClient does not allow "OPTIONS" requests. This is a pain when you are trying to write test cases for CORS. This simple patch adds "OPTIONS" to the supported _SUPPORTED_METHODS in _HTTPConnection and a test case to go along with it. --- tornado/simple_httpclient.py | 2 +- tornado/test/simple_httpclient_test.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 755c63ae0..ec3adf50d 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -121,7 +121,7 @@ class SimpleAsyncHTTPClient(AsyncHTTPClient): class _HTTPConnection(object): - _SUPPORTED_METHODS = set(["GET", "HEAD", "POST", "PUT", "DELETE"]) + _SUPPORTED_METHODS = set(["GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"]) def __init__(self, io_loop, client, request, release_callback, final_callback, max_buffer_size): diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index f2fc12d3b..d521c3f28 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -56,6 +56,12 @@ class HeadHandler(RequestHandler): self.set_header("Content-Length", "7") +class OptionsHandler(RequestHandler): + def options(self): + self.set_header("Access-Control-Allow-Origin", "*") + self.write("ok") + + class NoContentHandler(RequestHandler): def get(self): if self.get_argument("error", None): @@ -97,6 +103,7 @@ class SimpleHTTPClientTestCase(AsyncHTTPTestCase, LogTrapTestCase): url("/hello", HelloWorldHandler), url("/content_length", ContentLengthHandler), url("/head", HeadHandler), + url("/options", OptionsHandler), url("/no_content", NoContentHandler), url("/303_post", SeeOther303PostHandler), url("/303_get", SeeOther303GetHandler), @@ -235,6 +242,13 @@ class SimpleHTTPClientTestCase(AsyncHTTPTestCase, LogTrapTestCase): self.assertEqual(response.headers["content-length"], "7") self.assertFalse(response.body) + def test_options_request(self): + response = self.fetch("/options", method="OPTIONS") + self.assertEqual(response.code, 200) + self.assertEqual(response.headers["content-length"], "2") + self.assertEqual(response.headers["access-control-allow-origin"], "*") + self.assertEqual(response.body, "ok") + def test_no_content(self): response = self.fetch("/no_content") self.assertEqual(response.code, 204) -- 2.47.2