~raxnetworking/nova/melange

« back to all changes in this revision

Viewing changes to melange/tests/unit/test_wsgi.py

  • Committer: Rajaram Mallya
  • Date: 2011-09-05 13:19:18 UTC
  • Revision ID: rajarammallya@gmail.com-20110905131918-impvi6rmge75qk1x
Rajaram/Vinkesh|fixed more imports to import  only modules and misc style improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
15
#    License for the specific language governing permissions and limitations
16
16
#    under the License.
 
17
 
17
18
import routes
18
19
import webob
19
 
from webob.exc import HTTPBadRequest
20
 
from webob.exc import HTTPNotFound
21
 
from webtest import TestApp
 
20
import webob.exc
 
21
import webtest
22
22
 
23
23
from melange.common import wsgi
24
 
from melange.tests import BaseTest
 
24
from melange import tests
25
25
 
26
26
 
27
27
class StubApp(object):
42
42
        super(StubUrlMap, self).__init__()
43
43
 
44
44
 
45
 
class VersionedURLMapTest(BaseTest):
 
45
class VersionedURLMapTest(tests.BaseTest):
46
46
 
47
47
    def setUp(self):
48
48
        self.v1_app = StubApp()
69
69
        self.assertTrue(self.urlmap.called)
70
70
 
71
71
    def test_delegates_to_urlmapper_for_std_accept_headers_with_version(self):
72
 
        environ = {'HTTP_ACCEPT': "application/json;version=1.0",
73
 
                   'PATH_INFO': "/resource"}
 
72
        environ = {
 
73
            'HTTP_ACCEPT': "application/json;version=1.0",
 
74
            'PATH_INFO': "/resource",
 
75
            }
74
76
 
75
77
        self.versioned_urlmap(environ=environ, start_response=None)
76
78
 
77
79
        self.assertTrue(self.urlmap.called)
78
80
 
79
81
    def test_delegates_to_urlmapper_for_nonexistant_version_of_app(self):
80
 
        environ = {'HTTP_ACCEPT': "application/vnd.openstack.melange+xml;"
81
 
                   "version=9.0", 'REQUEST_METHOD': "GET",
82
 
                   'PATH_INFO': "/resource.xml"}
 
82
        environ = {
 
83
            'HTTP_ACCEPT': "application/vnd.openstack.melange+xml;"
 
84
            "version=9.0", 'REQUEST_METHOD': "GET",
 
85
            'PATH_INFO': "/resource.xml",
 
86
            }
83
87
 
84
88
        def assert_status(status, *args):
85
89
            self.assertEqual(status, "406 Not Acceptable")
87
91
        self.versioned_urlmap(environ=environ, start_response=assert_status)
88
92
 
89
93
    def test_delegates_to_urlmapper_when_url_versioned(self):
90
 
        environ = {'HTTP_ACCEPT': "application/vnd.openstack.melange+xml;"
91
 
                   "version=2.0",
92
 
                   'PATH_INFO': "/v1.0/resource"}
 
94
        environ = {
 
95
            'HTTP_ACCEPT': "application/vnd.openstack.melange+xml;"
 
96
            "version=2.0",
 
97
            'PATH_INFO': "/v1.0/resource",
 
98
            }
93
99
 
94
100
        self.versioned_urlmap(environ=environ, start_response=None)
95
101
 
96
102
        self.assertTrue(self.urlmap.called)
97
103
 
98
104
 
99
 
class RequestTest(BaseTest):
 
105
class RequestTest(tests.BaseTest):
100
106
 
101
107
    def test_content_type_missing_defaults_to_json(self):
102
108
        request = wsgi.Request.blank('/tests/123')
251
257
        return {'fort': 'knox'}
252
258
 
253
259
 
254
 
class TestController(BaseTest):
 
260
class TestController(tests.BaseTest):
255
261
 
256
262
    def test_response_content_type_matches_accept_header(self):
257
 
        app = TestApp(DummyApp())
 
263
        app = webtest.TestApp(DummyApp())
258
264
 
259
265
        response = app.get("/resources", headers={'Accept': "application/xml"})
260
266
 
263
269
        self.assertEqual(response.xml.text.strip(), "knox")
264
270
 
265
271
    def test_response_content_type_matches_url_format_over_accept_header(self):
266
 
        app = TestApp(DummyApp())
 
272
        app = webtest.TestApp(DummyApp())
267
273
 
268
274
        response = app.get("/resources.json",
269
275
                           headers={'Accept': "application/xml"})
272
278
        self.assertEqual(response.json, {'fort': 'knox'})
273
279
 
274
280
    def test_returns_404_if_action_not_implemented(self):
275
 
        app = TestApp(DummyApp())
 
281
        app = webtest.TestApp(DummyApp())
276
282
 
277
283
        response = app.get("/resources/new", status='*')
278
284
 
279
285
        self.assertEqual(response.status_int, 404)
280
286
 
281
287
 
282
 
class TestFault(BaseTest):
 
288
class TestFault(tests.BaseTest):
283
289
 
284
290
    def test_fault_wraps_webob_exception(self):
285
 
        app = TestApp(wsgi.Fault(HTTPNotFound("some error")))
 
291
        app = webtest.TestApp(wsgi.Fault(webob.exc.HTTPNotFound("some error")))
286
292
        response = app.get("/", status="*")
287
293
        self.assertEqual(response.status_int, 404)
288
294
        self.assertEqual(response.content_type, "application/json")
292
298
                              detail="some error"))
293
299
 
294
300
    def test_fault_gives_back_xml(self):
295
 
        app = TestApp(wsgi.Fault(HTTPBadRequest("some error")))
 
301
        app = webtest.TestApp(wsgi.Fault(
 
302
            webob.exc.HTTPBadRequest("some error")))
296
303
        response = app.get("/x.xml", status="*")
297
304
        self.assertEqual(response.content_type, "application/xml")
298
305
        self.assertEqual(response.xml.tag, 'BadRequest')
301
308
                         'some error')
302
309
 
303
310
 
304
 
class TestResult(BaseTest):
 
311
class TestResult(tests.BaseTest):
305
312
 
306
313
    class TestData(object):
307
314