File: test_http.py

package info (click to toggle)
python-zaqarclient 4.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 824 kB
  • sloc: python: 4,417; makefile: 18; sh: 2
file content (117 lines) | stat: -rw-r--r-- 4,538 bytes parent folder | download
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
# Copyright (c) 2013 Red Hat, Inc.
#
# 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.

from unittest import mock

import requests as prequest
from requests.packages.urllib3 import response

from zaqarclient.tests import base
from zaqarclient.tests.transport import api
from zaqarclient.transport import http
from zaqarclient.transport import request


class TestHttpTransport(base.TestBase):

    """Tests for the HTTP transport."""

    def setUp(self):
        super(TestHttpTransport, self).setUp()
        self.api = api.FakeApi()
        self.transport = http.HttpTransport(self.conf)

    @mock.patch.object(prequest.packages.urllib3.response.HTTPResponse,
                       'stream')
    def test_basic_send(self, mock_stream):
        params = {'name': 'Test',
                  'address': 'Outer space'}
        req = request.Request('http://example.org/',
                              operation='test_operation',
                              params=params)

        with mock.patch.object(self.transport.client, 'request',
                               autospec=True) as request_method:

            resp = prequest.Response()
            raw = response.HTTPResponse()
            resp.raw = raw
            request_method.return_value = resp

            # NOTE(flaper87): Bypass the API
            # loading step by setting the _api
            # attribute
            req._api = self.api
            self.transport.send(req)

            final_url = 'http://example.org/v2/test/Test'
            final_params = {'address': 'Outer space'}
            final_headers = {'content-type': 'application/json'}

            request_method.assert_called_with('GET', url=final_url,
                                              params=final_params,
                                              headers=final_headers,
                                              data=None,
                                              verify=True)

    @mock.patch.object(prequest.packages.urllib3.response.HTTPResponse,
                       'stream')
    def test_send_without_api(self, mock_stream):
        params = {'name': 'Test',
                  'address': 'Outer space'}
        req = request.Request('http://example.org/',
                              operation='test_operation',
                              params=params)

        with mock.patch.object(self.transport.client, 'request',
                               autospec=True) as request_method:

            resp = prequest.Response()
            raw = response.HTTPResponse()
            resp.raw = raw
            request_method.return_value = resp
            self.transport.send(req)

            final_url = 'http://example.org/'
            final_headers = {'content-type': 'application/json'}

            request_method.assert_called_with('GET', url=final_url,
                                              params=params,
                                              headers=final_headers,
                                              data=None,
                                              verify=True)

    @mock.patch.object(prequest.packages.urllib3.response.HTTPResponse,
                       'stream')
    def test_error_handling(self, mock_stream):
        params = {'name': 'Opportunity',
                  'address': 'NASA'}
        req = request.Request('http://example.org/',
                              operation='test_operation',
                              params=params)

        with mock.patch.object(self.transport.client, 'request',
                               autospec=True) as request_method:

            exception_iterator = self.transport.http_to_zaqar.items()

            for response_code, exception in exception_iterator:

                resp = prequest.Response()
                raw = response.HTTPResponse()
                resp.raw = raw
                resp.status_code = response_code
                request_method.return_value = resp
                self.assertRaises(exception, lambda: self.transport.send(req))