/glance/tests/unit/test_wsgi.py

https://github.com/athanatos/glance · Python · 184 lines · 136 code · 33 blank · 15 comment · 0 complexity · bdc66708f3d7b9cc5fbc006a8c9c5f83 MD5 · raw file

  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2. # Copyright 2010-2011 OpenStack, LLC
  3. # All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16. import unittest
  17. import webob
  18. from glance.common import wsgi
  19. from glance.common import exception
  20. class RequestTest(unittest.TestCase):
  21. def test_content_type_missing(self):
  22. request = wsgi.Request.blank('/tests/123')
  23. self.assertRaises(exception.InvalidContentType,
  24. request.get_content_type, ('application/xml'))
  25. def test_content_type_unsupported(self):
  26. request = wsgi.Request.blank('/tests/123')
  27. request.headers["Content-Type"] = "text/html"
  28. self.assertRaises(exception.InvalidContentType,
  29. request.get_content_type, ('application/xml'))
  30. def test_content_type_with_charset(self):
  31. request = wsgi.Request.blank('/tests/123')
  32. request.headers["Content-Type"] = "application/json; charset=UTF-8"
  33. result = request.get_content_type(('application/json'))
  34. self.assertEqual(result, "application/json")
  35. def test_content_type_from_accept_xml(self):
  36. request = wsgi.Request.blank('/tests/123')
  37. request.headers["Accept"] = "application/xml"
  38. result = request.best_match_content_type()
  39. self.assertEqual(result, "application/json")
  40. def test_content_type_from_accept_json(self):
  41. request = wsgi.Request.blank('/tests/123')
  42. request.headers["Accept"] = "application/json"
  43. result = request.best_match_content_type()
  44. self.assertEqual(result, "application/json")
  45. def test_content_type_from_accept_xml_json(self):
  46. request = wsgi.Request.blank('/tests/123')
  47. request.headers["Accept"] = "application/xml, application/json"
  48. result = request.best_match_content_type()
  49. self.assertEqual(result, "application/json")
  50. def test_content_type_from_accept_json_xml_quality(self):
  51. request = wsgi.Request.blank('/tests/123')
  52. request.headers["Accept"] = \
  53. "application/json; q=0.3, application/xml; q=0.9"
  54. result = request.best_match_content_type()
  55. self.assertEqual(result, "application/json")
  56. def test_content_type_accept_default(self):
  57. request = wsgi.Request.blank('/tests/123.unsupported')
  58. request.headers["Accept"] = "application/unsupported1"
  59. result = request.best_match_content_type()
  60. self.assertEqual(result, "application/json")
  61. class ResourceTest(unittest.TestCase):
  62. def test_get_action_args(self):
  63. env = {
  64. 'wsgiorg.routing_args': [
  65. None,
  66. {
  67. 'controller': None,
  68. 'format': None,
  69. 'action': 'update',
  70. 'id': 12,
  71. },
  72. ],
  73. }
  74. expected = {'action': 'update', 'id': 12}
  75. actual = wsgi.Resource(None, None, None).get_action_args(env)
  76. self.assertEqual(actual, expected)
  77. def test_dispatch(self):
  78. class Controller(object):
  79. def index(self, shirt, pants=None):
  80. return (shirt, pants)
  81. resource = wsgi.Resource(None, None, None)
  82. actual = resource.dispatch(Controller(), 'index', 'on', pants='off')
  83. expected = ('on', 'off')
  84. self.assertEqual(actual, expected)
  85. def test_dispatch_default(self):
  86. class Controller(object):
  87. def default(self, shirt, pants=None):
  88. return (shirt, pants)
  89. resource = wsgi.Resource(None, None, None)
  90. actual = resource.dispatch(Controller(), 'index', 'on', pants='off')
  91. expected = ('on', 'off')
  92. self.assertEqual(actual, expected)
  93. def test_dispatch_no_default(self):
  94. class Controller(object):
  95. def show(self, shirt, pants=None):
  96. return (shirt, pants)
  97. resource = wsgi.Resource(None, None, None)
  98. self.assertRaises(AttributeError, resource.dispatch, Controller(),
  99. 'index', 'on', pants='off')
  100. class JSONResponseSerializerTest(unittest.TestCase):
  101. def test_to_json(self):
  102. fixture = {"key": "value"}
  103. expected = '{"key": "value"}'
  104. actual = wsgi.JSONResponseSerializer().to_json(fixture)
  105. self.assertEqual(actual, expected)
  106. def test_default(self):
  107. fixture = {"key": "value"}
  108. response = webob.Response()
  109. wsgi.JSONResponseSerializer().default(response, fixture)
  110. self.assertEqual(response.status_int, 200)
  111. self.assertEqual(response.content_type, 'application/json')
  112. self.assertEqual(response.body, '{"key": "value"}')
  113. class JSONRequestDeserializerTest(unittest.TestCase):
  114. def test_has_body_no_content_length(self):
  115. request = wsgi.Request.blank('/')
  116. request.method = 'POST'
  117. request.body = 'asdf'
  118. request.headers.pop('Content-Length')
  119. self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request))
  120. def test_has_body_zero_content_length(self):
  121. request = wsgi.Request.blank('/')
  122. request.method = 'POST'
  123. request.body = 'asdf'
  124. request.headers['Content-Length'] = 0
  125. self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request))
  126. def test_has_body_has_content_length(self):
  127. request = wsgi.Request.blank('/')
  128. request.method = 'POST'
  129. request.body = 'asdf'
  130. self.assertTrue('Content-Length' in request.headers)
  131. self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request))
  132. def test_no_body_no_content_length(self):
  133. request = wsgi.Request.blank('/')
  134. self.assertFalse(wsgi.JSONRequestDeserializer().has_body(request))
  135. def test_from_json(self):
  136. fixture = '{"key": "value"}'
  137. expected = {"key": "value"}
  138. actual = wsgi.JSONRequestDeserializer().from_json(fixture)
  139. self.assertEqual(actual, expected)
  140. def test_default_no_body(self):
  141. request = wsgi.Request.blank('/')
  142. actual = wsgi.JSONRequestDeserializer().default(request)
  143. expected = {}
  144. self.assertEqual(actual, expected)
  145. def test_default_with_body(self):
  146. request = wsgi.Request.blank('/')
  147. request.method = 'POST'
  148. request.body = '{"key": "value"}'
  149. actual = wsgi.JSONRequestDeserializer().default(request)
  150. expected = {"body": {"key": "value"}}
  151. self.assertEqual(actual, expected)