summaryrefslogtreecommitdiff
path: root/tests/test_http.py
blob: 5cf84a5c7fd217d4a969c8fc57447673e3e9b0f6 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Copyright 2012 OpenStack LLC.
# 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 httplib
import socket
import StringIO
import urlparse

import mox
import testtools

from glanceclient import exc
import glanceclient
from glanceclient.common import http
from tests import utils


class TestClient(testtools.TestCase):

    def setUp(self):
        super(TestClient, self).setUp()
        self.mock = mox.Mox()
        self.mock.StubOutWithMock(httplib.HTTPConnection, 'request')
        self.mock.StubOutWithMock(httplib.HTTPConnection, 'getresponse')

        self.endpoint = 'http://example.com:9292'
        self.client = http.HTTPClient(self.endpoint, token=u'abc123')

    def tearDown(self):
        super(TestClient, self).tearDown()
        self.mock.UnsetStubs()

    def test_connection_refused(self):
        """
        Should receive a CommunicationError if connection refused.
        And the error should list the host and port that refused the
        connection
        """
        httplib.HTTPConnection.request(
            mox.IgnoreArg(),
            mox.IgnoreArg(),
            headers=mox.IgnoreArg(),
        ).AndRaise(socket.error())
        self.mock.ReplayAll()
        try:
            self.client.json_request('GET', '/v1/images/detail?limit=20')
            #NOTE(alaski) We expect exc.CommunicationError to be raised
            # so we should never reach this point.  try/except is used here
            # rather than assertRaises() so that we can check the body of
            # the exception.
            self.fail('An exception should have bypassed this line.')
        except glanceclient.exc.CommunicationError as comm_err:
            fail_msg = ("Exception message '%s' should contain '%s'" %
                       (comm_err.message, self.endpoint))
            self.assertTrue(self.endpoint in comm_err.message, fail_msg)

    def test_http_encoding(self):
        httplib.HTTPConnection.request(
            mox.IgnoreArg(),
            mox.IgnoreArg(),
            headers=mox.IgnoreArg())

        # Lets fake the response
        # returned by httplib
        expected_response = 'Ok'
        fake = utils.FakeResponse({}, StringIO.StringIO(expected_response))
        httplib.HTTPConnection.getresponse().AndReturn(fake)
        self.mock.ReplayAll()

        headers = {"test": u'ni\xf1o'}
        resp, body = self.client.raw_request('GET', '/v1/images/detail',
                                                    headers=headers)
        self.assertEqual(resp, fake)

    def test_headers_encoding(self):
        headers = {"test": u'ni\xf1o'}
        encoded = self.client.encode_headers(headers)
        self.assertEqual(encoded["test"], "ni\xc3\xb1o")

    def test_raw_request(self):
        " Verify the path being used for HTTP requests reflects accurately. "

        def check_request(method, path, **kwargs):
            self.assertEqual(method, 'GET')
            # NOTE(kmcdonald): See bug #1179984 for more details.
            self.assertEqual(path, '/v1/images/detail')

        httplib.HTTPConnection.request(
            mox.IgnoreArg(),
            mox.IgnoreArg(),
            headers=mox.IgnoreArg()).WithSideEffects(check_request)

        # fake the response returned by httplib
        fake = utils.FakeResponse({}, StringIO.StringIO('Ok'))
        httplib.HTTPConnection.getresponse().AndReturn(fake)
        self.mock.ReplayAll()

        resp, body = self.client.raw_request('GET', '/v1/images/detail')
        self.assertEqual(resp, fake)

    def test_connection_refused_raw_request(self):
        """
        Should receive a CommunicationError if connection refused.
        And the error should list the host and port that refused the
        connection
        """
        endpoint = 'http://example.com:9292'
        client = http.HTTPClient(endpoint, token=u'abc123')
        httplib.HTTPConnection.request(mox.IgnoreArg(), mox.IgnoreArg(),
                                       headers=mox.IgnoreArg()
                                       ).AndRaise(socket.error())
        self.mock.ReplayAll()
        try:
            client.raw_request('GET', '/v1/images/detail?limit=20')

            self.fail('An exception should have bypassed this line.')
        except exc.CommunicationError as comm_err:
            fail_msg = ("Exception message '%s' should contain '%s'" %
                        (comm_err.message, endpoint))
            self.assertTrue(endpoint in comm_err.message, fail_msg)

    def test_parse_endpoint(self):
        endpoint = 'http://example.com:9292'
        test_client = http.HTTPClient(endpoint, token=u'adc123')
        actual = test_client.parse_endpoint(endpoint)
        expected = urlparse.ParseResult(scheme='http',
                                        netloc='example.com:9292', path='',
                                        params='', query='', fragment='')
        self.assertEqual(expected, actual)

    def test_get_connection_class(self):
        endpoint = 'http://example.com:9292'
        test_client = http.HTTPClient(endpoint, token=u'adc123')
        actual = (test_client.get_connection_class('https'))
        self.assertEqual(actual, http.VerifiedHTTPSConnection)

    def test_get_connections_kwargs_http(self):
        endpoint = 'http://example.com:9292'
        test_client = http.HTTPClient(endpoint, token=u'adc123')
        actual = test_client.get_connection_kwargs('http', insecure=True)
        self.assertEqual({'timeout': 600.0}, actual)

    def test_get_connections_kwargs_https(self):
        endpoint = 'http://example.com:9292'
        test_client = http.HTTPClient(endpoint, token=u'adc123')
        actual = test_client.get_connection_kwargs('https', insecure=True)
        expected = {'cacert': None,
                    'cert_file': None,
                    'insecure': True,
                    'key_file': None,
                    'ssl_compression': True,
                    'timeout': 600.0}
        self.assertEqual(expected, actual)


class TestHostResolutionError(testtools.TestCase):

    def setUp(self):
        super(TestHostResolutionError, self).setUp()
        self.mock = mox.Mox()
        self.invalid_host = "example.com.incorrect_top_level_domain"

    def test_incorrect_domain_error(self):
        """
        Make sure that using a domain which does not resolve causes an
        exception which mentions that specific hostname as a reason for
        failure.
        """
        class FailingConnectionClass(object):
            def __init__(self, *args, **kwargs):
                pass

            def putrequest(self, *args, **kwargs):
                raise socket.gaierror(-2, "Name or service not known")

            def request(self, *args, **kwargs):
                raise socket.gaierror(-2, "Name or service not known")

        self.endpoint = 'http://%s:9292' % (self.invalid_host,)
        self.client = http.HTTPClient(self.endpoint, token=u'abc123')

        self.mock.StubOutWithMock(self.client, 'get_connection')
        self.client.get_connection().AndReturn(FailingConnectionClass())
        self.mock.ReplayAll()

        try:
            self.client.raw_request('GET', '/example/path')
            self.fail("gaierror should be raised")
        except exc.InvalidEndpoint as e:
            self.assertTrue(self.invalid_host in str(e),
                            "exception should contain the hostname")

    def tearDown(self):
        super(TestHostResolutionError, self).tearDown()
        self.mock.UnsetStubs()


class TestResponseBodyIterator(testtools.TestCase):
    def test_iter_default_chunk_size_64k(self):
        resp = utils.FakeResponse({}, StringIO.StringIO('X' * 98304))
        iterator = http.ResponseBodyIterator(resp)
        chunks = list(iterator)
        self.assertEqual(chunks, ['X' * 65536, 'X' * 32768])