| 1 |
# Copyright (c) 2013 Red Hat, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain 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, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 |
# implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
import requests as prequest |
| 19 |
from requests.packages.urllib3 import response |
| 20 |
|
| 21 |
from zaqarclient.tests import base |
| 22 |
from zaqarclient.tests.transport import api |
| 23 |
from zaqarclient.transport import http |
| 24 |
from zaqarclient.transport import request |
| 25 |
|
| 26 |
|
| 27 |
class TestHttpTransport(base.TestBase): |
| 28 |
|
| 29 |
"""Tests for the HTTP transport.""" |
| 30 |
|
| 31 |
def setUp(self): |
| 32 |
super().setUp() |
| 33 |
self.api = api.FakeApi() |
| 34 |
self.transport = http.HttpTransport(self.conf) |
| 35 |
|
| 36 |
@mock.patch.object(prequest.packages.urllib3.response.HTTPResponse, |
| 37 |
'stream') |
| 38 |
def test_basic_send(self, mock_stream): |
| 39 |
params = {'name': 'Test', |
| 40 |
'address': 'Outer space'} |
| 41 |
req = request.Request('http://example.org/', |
| 42 |
operation='test_operation', |
| 43 |
params=params) |
| 44 |
|
| 45 |
with mock.patch.object(self.transport.client, 'request', |
| 46 |
autospec=True) as request_method: |
| 47 |
|
| 48 |
resp = prequest.Response() |
| 49 |
raw = response.HTTPResponse() |
| 50 |
resp.raw = raw |
| 51 |
request_method.return_value = resp |
| 52 |
|
| 53 |
# NOTE(flaper87): Bypass the API |
| 54 |
# loading step by setting the _api |
| 55 |
# attribute |
| 56 |
req._api = self.api |
| 57 |
self.transport.send(req) |
| 58 |
|
| 59 |
final_url = 'http://example.org/v2/test/Test' |
| 60 |
final_params = {'address': 'Outer space'} |
| 61 |
final_headers = {'content-type': 'application/json'} |
| 62 |
|
| 63 |
request_method.assert_called_with('GET', url=final_url, |
| 64 |
params=final_params, |
| 65 |
headers=final_headers, |
| 66 |
data=None, |
| 67 |
verify=True) |
| 68 |
|
| 69 |
@mock.patch.object(prequest.packages.urllib3.response.HTTPResponse, |
| 70 |
'stream') |
| 71 |
def test_send_without_api(self, mock_stream): |
| 72 |
params = {'name': 'Test', |
| 73 |
'address': 'Outer space'} |
| 74 |
req = request.Request('http://example.org/', |
| 75 |
operation='test_operation', |
| 76 |
params=params) |
| 77 |
|
| 78 |
with mock.patch.object(self.transport.client, 'request', |
| 79 |
autospec=True) as request_method: |
| 80 |
|
| 81 |
resp = prequest.Response() |
| 82 |
raw = response.HTTPResponse() |
| 83 |
resp.raw = raw |
| 84 |
request_method.return_value = resp |
| 85 |
self.transport.send(req) |
| 86 |
|
| 87 |
final_url = 'http://example.org/' |
| 88 |
final_headers = {'content-type': 'application/json'} |
| 89 |
|
| 90 |
request_method.assert_called_with('GET', url=final_url, |
| 91 |
params=params, |
| 92 |
headers=final_headers, |
| 93 |
data=None, |
| 94 |
verify=True) |
| 95 |
|
| 96 |
@mock.patch.object(prequest.packages.urllib3.response.HTTPResponse, |
| 97 |
'stream') |
| 98 |
def test_error_handling(self, mock_stream): |
| 99 |
params = {'name': 'Opportunity', |
| 100 |
'address': 'NASA'} |
| 101 |
req = request.Request('http://example.org/', |
| 102 |
operation='test_operation', |
| 103 |
params=params) |
| 104 |
|
| 105 |
with mock.patch.object(self.transport.client, 'request', |
| 106 |
autospec=True) as request_method: |
| 107 |
|
| 108 |
exception_iterator = self.transport.http_to_zaqar.items() |
| 109 |
|
| 110 |
for response_code, exception in exception_iterator: |
| 111 |
|
| 112 |
resp = prequest.Response() |
| 113 |
raw = response.HTTPResponse() |
| 114 |
resp.raw = raw |
| 115 |
resp.status_code = response_code |
| 116 |
request_method.return_value = resp |
| 117 |
self.assertRaises(exception, lambda: self.transport.send(req)) |