summaryrefslogtreecommitdiff
path: root/nova/tests/api/openstack/compute/test_urlmap.py
blob: 58c41506cdf036a01ef4cdf81b988a99157e0c0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Copyright 2011 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import webob

from nova.openstack.common import jsonutils
from nova import test
from nova.tests.api.openstack import fakes
import nova.tests.image.fake


class UrlmapTest(test.NoDBTestCase):
    def setUp(self):
        super(UrlmapTest, self).setUp()
        fakes.stub_out_rate_limiting(self.stubs)
        nova.tests.image.fake.stub_out_image_service(self.stubs)

    def tearDown(self):
        super(UrlmapTest, self).tearDown()
        nova.tests.image.fake.FakeImageService_reset()

    def test_path_version_v1_1(self):
        # Test URL path specifying v1.1 returns v2 content.
        req = webob.Request.blank('/v1.1/')
        req.accept = "application/json"
        res = req.get_response(fakes.wsgi_app(init_only=('versions',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['version']['id'], 'v2.0')

    def test_content_type_version_v1_1(self):
        # Test Content-Type specifying v1.1 returns v2 content.
        req = webob.Request.blank('/')
        req.content_type = "application/json;version=1.1"
        req.accept = "application/json"
        res = req.get_response(fakes.wsgi_app(init_only=('versions',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['version']['id'], 'v2.0')

    def test_accept_version_v1_1(self):
        # Test Accept header specifying v1.1 returns v2 content.
        req = webob.Request.blank('/')
        req.accept = "application/json;version=1.1"
        res = req.get_response(fakes.wsgi_app(init_only=('versions',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['version']['id'], 'v2.0')

    def test_path_version_v2(self):
        # Test URL path specifying v2 returns v2 content.
        req = webob.Request.blank('/v2/')
        req.accept = "application/json"
        res = req.get_response(fakes.wsgi_app(init_only=('versions',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['version']['id'], 'v2.0')

    def test_content_type_version_v2(self):
        # Test Content-Type specifying v2 returns v2 content.
        req = webob.Request.blank('/')
        req.content_type = "application/json;version=2"
        req.accept = "application/json"
        res = req.get_response(fakes.wsgi_app(init_only=('versions',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['version']['id'], 'v2.0')

    def test_accept_version_v2(self):
        # Test Accept header specifying v2 returns v2 content.
        req = webob.Request.blank('/')
        req.accept = "application/json;version=2"
        res = req.get_response(fakes.wsgi_app(init_only=('versions',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['version']['id'], 'v2.0')

    def test_path_content_type(self):
        # Test URL path specifying JSON returns JSON content.
        url = '/v2/fake/images/cedef40a-ed67-4d10-800e-17455edce175.json'
        req = webob.Request.blank(url)
        req.accept = "application/xml"
        res = req.get_response(fakes.wsgi_app(init_only=('images',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['image']['id'],
                         'cedef40a-ed67-4d10-800e-17455edce175')

    def test_accept_content_type(self):
        # Test Accept header specifying JSON returns JSON content.
        url = '/v2/fake/images/cedef40a-ed67-4d10-800e-17455edce175'
        req = webob.Request.blank(url)
        req.accept = "application/xml;q=0.8, application/json"
        res = req.get_response(fakes.wsgi_app(init_only=('images',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['image']['id'],
                         'cedef40a-ed67-4d10-800e-17455edce175')

    def test_path_version_v3(self):
        # Test URL path specifying v3 returns v3 content.
        req = webob.Request.blank('/v3/')
        req.accept = "application/json"
        res = req.get_response(fakes.wsgi_app_v3(init_only=('versions',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['version']['id'], 'v3.0')

    def test_content_type_version_v3(self):
        # Test Content-Type specifying v3 returns v3 content.
        req = webob.Request.blank('/')
        req.content_type = "application/json;version=3"
        req.accept = "application/json"
        res = req.get_response(fakes.wsgi_app_v3(init_only=('versions',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['version']['id'], 'v3.0')

    def test_accept_version_v3(self):
        # Test Accept header specifying v3 returns v3 content.
        req = webob.Request.blank('/')
        req.accept = "application/json;version=3"
        res = req.get_response(fakes.wsgi_app_v3(init_only=('versions',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['version']['id'], 'v3.0')

    def test_path_content_type_v3(self):
        # Test URL path specifying JSON returns JSON content.
        url = '/v3/extensions/extensions.json'
        req = webob.Request.blank(url)
        req.accept = "application/xml"
        res = req.get_response(fakes.wsgi_app_v3())
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['extension']['name'], 'extensions')

    def test_accept_content_type_v3(self):
        # Test Accept header specifying JSON returns JSON content.
        url = '/v3/extensions/extensions'
        req = webob.Request.blank(url)
        req.accept = "application/xml;q=0.8, application/json"
        res = req.get_response(fakes.wsgi_app_v3(init_only=('extensions',)))
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, "application/json")
        body = jsonutils.loads(res.body)
        self.assertEqual(body['extension']['name'], 'extensions')