summaryrefslogtreecommitdiffstats
path: root/tests/test_middleware.py
diff options
context:
space:
mode:
authorDolph Mathews <dolph.mathews@gmail.com>2012-02-10 14:52:13 -0600
committerDolph Mathews <dolph.mathews@gmail.com>2012-02-27 09:58:04 -0600
commit212489084fac8de20718bfccad2f77cbfa7ea3e2 (patch)
tree4cfada718772bb13e93be1f6c8f3b932064eb7ab /tests/test_middleware.py
parente23ecc6893db337671f75b6cc069d96a183940e8 (diff)
downloadkeystone-212489084fac8de20718bfccad2f77cbfa7ea3e2.tar.gz
keystone-212489084fac8de20718bfccad2f77cbfa7ea3e2.tar.xz
keystone-212489084fac8de20718bfccad2f77cbfa7ea3e2.zip
XML de/serialization (bug 928058)
Middleware rewrites incoming XML requests as JSON, and outgoing JSON as XML, per Accept and Content-Type headers. Tests assert that core API methods support WADL/XSD specs, and cover JSON content as well. Change-Id: I6897971dd745766cbc472fd6e5346b1b34d933b0
Diffstat (limited to 'tests/test_middleware.py')
-rw-r--r--tests/test_middleware.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/test_middleware.py b/tests/test_middleware.py
index 01d6c8ed..d46348a4 100644
--- a/tests/test_middleware.py
+++ b/tests/test_middleware.py
@@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.
+import json
+
import webob
from keystone import config
@@ -25,15 +27,23 @@ CONF = config.CONF
def make_request(**kwargs):
+ accept = kwargs.pop('accept', None)
method = kwargs.pop('method', 'GET')
body = kwargs.pop('body', None)
req = webob.Request.blank('/', **kwargs)
req.method = method
if body is not None:
req.body = body
+ if accept is not None:
+ req.accept = accept
return req
+def make_response(**kwargs):
+ body = kwargs.pop('body', None)
+ return webob.Response(body)
+
+
class TokenAuthMiddlewareTest(test.TestCase):
def test_request(self):
req = make_request()
@@ -97,3 +107,51 @@ class JsonBodyMiddlewareTest(test.TestCase):
middleware.JsonBodyMiddleware(None).process_request(req)
params = req.environ.get(middleware.PARAMS_ENV, {})
self.assertEqual(params, {})
+
+
+class XmlBodyMiddlewareTest(test.TestCase):
+ def test_client_wants_xml_back(self):
+ """Clients requesting XML should get what they ask for."""
+ body = '{"container": {"attribute": "value"}}'
+ req = make_request(body=body, method='POST', accept='application/xml')
+ middleware.XmlBodyMiddleware(None).process_request(req)
+ resp = make_response(body=body)
+ middleware.XmlBodyMiddleware(None).process_response(req, resp)
+ self.assertEqual(resp.content_type, 'application/xml')
+
+ def test_client_wants_json_back(self):
+ """Clients requesting JSON should definitely not get XML back."""
+ body = '{"container": {"attribute": "value"}}'
+ req = make_request(body=body, method='POST', accept='application/json')
+ middleware.XmlBodyMiddleware(None).process_request(req)
+ resp = make_response(body=body)
+ middleware.XmlBodyMiddleware(None).process_response(req, resp)
+ self.assertNotIn('application/xml', resp.content_type)
+
+ def test_client_fails_to_specify_accept(self):
+ """If client does not specify an Accept header, default to JSON."""
+ body = '{"container": {"attribute": "value"}}'
+ req = make_request(body=body, method='POST')
+ middleware.XmlBodyMiddleware(None).process_request(req)
+ resp = make_response(body=body)
+ middleware.XmlBodyMiddleware(None).process_response(req, resp)
+ self.assertNotIn('application/xml', resp.content_type)
+
+ def test_xml_replaced_by_json(self):
+ """XML requests should be replaced by JSON requests."""
+ req = make_request(
+ body='<container><element attribute="value" /></container>',
+ content_type='application/xml',
+ method='POST')
+ middleware.XmlBodyMiddleware(None).process_request(req)
+ self.assertTrue(req.content_type, 'application/json')
+ self.assertTrue(json.loads(req.body))
+
+ def test_json_unnaffected(self):
+ """JSON-only requests should be unnaffected by the XML middleware."""
+ content_type = 'application/json'
+ body = '{"container": {"attribute": "value"}}'
+ req = make_request(body=body, content_type=content_type, method='POST')
+ middleware.XmlBodyMiddleware(None).process_request(req)
+ self.assertEqual(req.body, body)
+ self.assertEqual(req.content_type, content_type)