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
|
#--------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#--------------------------------------------------------------------------
import io
import asyncio
import json
import unittest
try:
from unittest import mock
except ImportError:
import mock
import sys
import pytest
import requests
from requests.adapters import HTTPAdapter
from oauthlib import oauth2
from msrest.async_client import ServiceClientAsync
from msrest.authentication import OAuthTokenAuthentication
from msrest.configuration import Configuration
from msrest import Configuration
from msrest.exceptions import ClientRequestError, TokenExpiredError
from msrest.universal_http import ClientRequest
from msrest.universal_http.async_requests import AsyncRequestsClientResponse
@unittest.skipIf(sys.version_info < (3, 5, 2), "Async tests only on 3.5.2 minimal")
class TestServiceClient(object):
@pytest.mark.asyncio
async def test_client_send(self):
cfg = Configuration("/")
cfg.headers = {'Test': 'true'}
cfg.credentials = mock.create_autospec(OAuthTokenAuthentication)
client = ServiceClientAsync(cfg)
req_response = requests.Response()
req_response._content = br'{"real": true}' # Has to be valid bytes JSON
req_response._content_consumed = True
req_response.status_code = 200
def side_effect(*args, **kwargs):
return req_response
session = mock.create_autospec(requests.Session)
session.request.side_effect = side_effect
session.adapters = {
"http://": HTTPAdapter(),
"https://": HTTPAdapter(),
}
# Be sure the mock does not trick me
assert not hasattr(session.resolve_redirects, 'is_msrest_patched')
client.config.pipeline._sender.driver.session = session
client.config.credentials.signed_session.return_value = session
client.config.credentials.refresh_session.return_value = session
request = ClientRequest('GET', '/')
await client.async_send(request, stream=False)
session.request.call_count = 0
session.request.assert_called_with(
'GET',
'/',
allow_redirects=True,
cert=None,
headers={
'User-Agent': cfg.user_agent,
'Test': 'true' # From global config
},
stream=False,
timeout=100,
verify=True
)
assert session.resolve_redirects.is_msrest_patched
request = client.get('/', headers={'id':'1234'}, content={'Test':'Data'})
await client.async_send(request, stream=False)
session.request.assert_called_with(
'GET',
'/',
data='{"Test": "Data"}',
allow_redirects=True,
cert=None,
headers={
'User-Agent': cfg.user_agent,
'Content-Length': '16',
'id':'1234',
'Accept': 'application/json',
'Test': 'true' # From global config
},
stream=False,
timeout=100,
verify=True
)
assert session.request.call_count == 1
session.request.call_count = 0
assert session.resolve_redirects.is_msrest_patched
request = client.get('/', headers={'id':'1234'}, content={'Test':'Data'})
session.request.side_effect = requests.RequestException("test")
with pytest.raises(ClientRequestError):
await client.async_send(request, test='value', stream=False)
session.request.assert_called_with(
'GET',
'/',
data='{"Test": "Data"}',
allow_redirects=True,
cert=None,
headers={
'User-Agent': cfg.user_agent,
'Content-Length': '16',
'id':'1234',
'Accept': 'application/json',
'Test': 'true' # From global config
},
stream=False,
timeout=100,
verify=True
)
assert session.request.call_count == 1
session.request.call_count = 0
assert session.resolve_redirects.is_msrest_patched
session.request.side_effect = oauth2.rfc6749.errors.InvalidGrantError("test")
with pytest.raises(TokenExpiredError):
await client.async_send(request, headers={'id':'1234'}, content={'Test':'Data'}, test='value')
assert session.request.call_count == 2
session.request.call_count = 0
session.request.side_effect = ValueError("test")
with pytest.raises(ValueError):
await client.async_send(request, headers={'id':'1234'}, content={'Test':'Data'}, test='value')
@pytest.mark.asyncio
async def test_client_stream_download(self):
req_response = requests.Response()
req_response._content = "abc"
req_response._content_consumed = True
req_response.status_code = 200
client_response = AsyncRequestsClientResponse(
None,
req_response
)
def user_callback(chunk, response):
assert response is req_response
assert chunk in ["a", "b", "c"]
async_iterator = client_response.stream_download(1, user_callback)
result = ""
async for value in async_iterator:
result += value
assert result == "abc"
if __name__ == '__main__':
unittest.main()
|