blob: ead112bfdab4beeb1f02286d41fe2f3fefb50599 [file] [log] [blame]
Matthew Treinisha33037e2013-12-05 23:16:39 +00001# Copyright 2013 IBM Corp.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import httplib2
16
17from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000018from tempest import config
Matthew Treinisha33037e2013-12-05 23:16:39 +000019from tempest import exceptions
20from tempest.openstack.common.fixture import mockpatch
21from tempest.tests import base
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000022from tempest.tests import fake_auth_provider
Matthew Treinisha33037e2013-12-05 23:16:39 +000023from tempest.tests import fake_config
24from tempest.tests import fake_http
25
26
27class BaseRestClientTestClass(base.TestCase):
28
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000029 def _get_region(self):
30 return 'fake region'
Matthew Treinisha33037e2013-12-05 23:16:39 +000031
32 def setUp(self):
33 super(BaseRestClientTestClass, self).setUp()
Matthew Treinish684d8992014-01-30 16:27:40 +000034 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakeConfig)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000035 self.rest_client = rest_client.RestClient(
36 fake_auth_provider.FakeAuthProvider())
Matthew Treinisha33037e2013-12-05 23:16:39 +000037 self.stubs.Set(httplib2.Http, 'request', self.fake_http.request)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000038 self.useFixture(mockpatch.PatchObject(self.rest_client, '_get_region',
39 side_effect=self._get_region()))
Matthew Treinisha33037e2013-12-05 23:16:39 +000040 self.useFixture(mockpatch.PatchObject(self.rest_client,
41 '_log_response'))
42
43
44class TestRestClientHTTPMethods(BaseRestClientTestClass):
45 def setUp(self):
46 self.fake_http = fake_http.fake_httplib2()
47 super(TestRestClientHTTPMethods, self).setUp()
48 self.useFixture(mockpatch.PatchObject(self.rest_client,
49 '_error_checker'))
50
51 def test_post(self):
52 __, return_dict = self.rest_client.post('fake_endpoint', {},
53 {})
54 self.assertEqual('POST', return_dict['method'])
55
56 def test_get(self):
57 __, return_dict = self.rest_client.get('fake_endpoint')
58 self.assertEqual('GET', return_dict['method'])
59
60 def test_delete(self):
61 __, return_dict = self.rest_client.delete('fake_endpoint')
62 self.assertEqual('DELETE', return_dict['method'])
63
64 def test_patch(self):
65 __, return_dict = self.rest_client.patch('fake_endpoint', {},
66 {})
67 self.assertEqual('PATCH', return_dict['method'])
68
69 def test_put(self):
70 __, return_dict = self.rest_client.put('fake_endpoint', {},
71 {})
72 self.assertEqual('PUT', return_dict['method'])
73
74 def test_head(self):
75 self.useFixture(mockpatch.PatchObject(self.rest_client,
76 'response_checker'))
77 __, return_dict = self.rest_client.head('fake_endpoint')
78 self.assertEqual('HEAD', return_dict['method'])
79
80 def test_copy(self):
81 __, return_dict = self.rest_client.copy('fake_endpoint')
82 self.assertEqual('COPY', return_dict['method'])
83
84
85class TestRestClientNotFoundHandling(BaseRestClientTestClass):
86 def setUp(self):
87 self.fake_http = fake_http.fake_httplib2(404)
88 super(TestRestClientNotFoundHandling, self).setUp()
89
90 def test_post(self):
91 self.assertRaises(exceptions.NotFound, self.rest_client.post,
92 'fake_endpoint', {}, {})