blob: ae6174cd3cbaf2958cda71cefbc57a7295ebcd09 [file] [log] [blame]
Matthew Treinisha33037e2013-12-05 23:16:39 +00001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2013 IBM Corp.
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
17import httplib2
18
19from tempest.common import rest_client
20from tempest import exceptions
21from tempest.openstack.common.fixture import mockpatch
22from tempest.tests import base
23from tempest.tests import fake_config
24from tempest.tests import fake_http
25
26
27class BaseRestClientTestClass(base.TestCase):
28
29 def _set_token(self):
30 self.rest_client.token = 'fake token'
31
32 def setUp(self):
33 super(BaseRestClientTestClass, self).setUp()
34 self.rest_client = rest_client.RestClient(fake_config.FakeConfig(),
35 'fake_user', 'fake_pass',
36 'http://fake_url/v2.0')
37 self.stubs.Set(httplib2.Http, 'request', self.fake_http.request)
38 self.useFixture(mockpatch.PatchObject(self.rest_client, '_set_auth',
39 side_effect=self._set_token()))
40 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', {}, {})