Debian Codegraph @

neutronclient/tests/unit/test_http.py

raw
1 # Copyright (C) 2013 OpenStack Foundation.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import abc
17
18 from oslo_utils import uuidutils
19 import osprofiler.profiler
20 import osprofiler.web
21 from requests_mock.contrib import fixture as mock_fixture
22 import testtools
23
24 from neutronclient import client
25 from neutronclient.common import exceptions
26
27
28 AUTH_TOKEN = 'test_token'
29 END_URL = 'test_url'
30 METHOD = 'GET'
31 URL = 'http://test.test:1234/v2.0/test'
32 BODY = 'IAMFAKE'
33
34
35 class TestHTTPClientMixin(object, metaclass=abc.ABCMeta):
36
37 def setUp(self):
38 super(TestHTTPClientMixin, self).setUp()
39
40 self.requests = self.useFixture(mock_fixture.Fixture())
41 self.http = self.initialize()
42
43 @abc.abstractmethod
44 def initialize(self):
45 """Return client class, instance."""
46
47 def _test_headers(self, expected_headers, **kwargs):
48 # Test headers.
49 self.requests.register_uri(METHOD, URL,
50 request_headers=expected_headers)
51 self.http.request(URL, METHOD, **kwargs)
52 self.assertEqual(kwargs.get('body'), self.requests.last_request.body)
53
54 def test_headers_without_body(self):
55 self._test_headers({'Accept': 'application/json'})
56
57 def test_headers_with_body(self):
58 headers = {'Accept': 'application/json',
59 'Content-Type': 'application/json'}
60 self._test_headers(headers, body=BODY)
61
62 def test_headers_without_body_with_content_type(self):
63 headers = {'Accept': 'application/json'}
64 self._test_headers(headers, content_type='application/json')
65
66 def test_headers_with_body_with_content_type(self):
67 headers = {'Accept': 'application/json',
68 'Content-Type': 'application/json'}
69 self._test_headers(headers, body=BODY, content_type='application/json')
70
71 def test_headers_defined_in_headers(self):
72 headers = {'Accept': 'application/json',
73 'Content-Type': 'application/json'}
74 self._test_headers(headers, body=BODY, headers=headers)
75
76 def test_osprofiler_headers_are_injected(self):
77 osprofiler.profiler.init('SWORDFISH')
78 self.addCleanup(osprofiler.profiler.clean)
79
80 headers = {'Accept': 'application/json'}
81 headers.update(osprofiler.web.get_trace_id_headers())
82 self._test_headers(headers)
83
84
85 class TestHTTPClient(TestHTTPClientMixin, testtools.TestCase):
86
87 def initialize(self):
88 return client.HTTPClient(token=AUTH_TOKEN, endpoint_url=END_URL)
89
90 def test_request_error(self):
91 def cb(*args, **kwargs):
92 raise Exception('error msg')
93
94 self.requests.get(URL, body=cb)
95 self.assertRaises(
96 exceptions.ConnectionFailed,
97 self.http._cs_request,
98 URL, METHOD
99 )
100
101 def test_request_success(self):
102 text = 'test content'
103 self.requests.register_uri(METHOD, URL, text=text)
104
105 resp, resp_text = self.http._cs_request(URL, METHOD)
106 self.assertEqual(200, resp.status_code)
107 self.assertEqual(text, resp_text)
108
109 def test_request_unauthorized(self):
110 text = 'unauthorized message'
111 self.requests.register_uri(METHOD, URL, status_code=401, text=text)
112 e = self.assertRaises(exceptions.Unauthorized,
113 self.http._cs_request, URL, METHOD)
114 self.assertEqual(text, e.message)
115
116 def test_request_forbidden_is_returned_to_caller(self):
117 text = 'forbidden message'
118 self.requests.register_uri(METHOD, URL, status_code=403, text=text)
119
120 resp, resp_text = self.http._cs_request(URL, METHOD)
121 self.assertEqual(403, resp.status_code)
122 self.assertEqual(text, resp_text)
123
124 def test_do_request_success(self):
125 text = 'test content'
126 self.requests.register_uri(METHOD, END_URL + URL, text=text)
127
128 resp, resp_text = self.http.do_request(URL, METHOD)
129 self.assertEqual(200, resp.status_code)
130 self.assertEqual(text, resp_text)
131
132 def test_do_request_with_headers_success(self):
133 text = 'test content'
134 self.requests.register_uri(METHOD, END_URL + URL, text=text,
135 request_headers={'key': 'value'})
136
137 resp, resp_text = self.http.do_request(URL, METHOD,
138 headers={'key': 'value'})
139 self.assertEqual(200, resp.status_code)
140 self.assertEqual(text, resp_text)
141
142
143 class TestHTTPClientWithReqId(TestHTTPClientMixin, testtools.TestCase):
144 """Tests for when global_request_id is set."""
145
146 def initialize(self):
147 self.req_id = "req-%s" % uuidutils.generate_uuid()
148 return client.HTTPClient(token=AUTH_TOKEN, endpoint_url=END_URL,
149 global_request_id=self.req_id)
150
151 def test_request_success(self):
152 headers = {
153 'Accept': 'application/json',
154 'X-OpenStack-Request-ID': self.req_id
155 }
156 self.requests.register_uri(METHOD, URL, request_headers=headers)
157 self.http.request(URL, METHOD)