forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helpers.py
240 lines (209 loc) · 7.24 KB
/
test_helpers.py
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from io import IOBase, UnsupportedOperation
from typing import Any, Dict, Optional
from typing_extensions import Self
from azure.core.pipeline.transport import (
HttpTransport,
RequestsTransport,
RequestsTransportResponse
)
from azure.core.rest import HttpRequest
from requests import Response
from urllib3 import HTTPResponse
class ProgressTracker:
def __init__(self, total: int, step: int):
self.total = total
self.step = step
self.current = 0
def assert_progress(self, current: int, total: Optional[int]):
if self.current != self.total:
self.current += self.step
if total:
assert self.total == total
assert self.current == current
def assert_complete(self):
assert self.total == self.current
class NonSeekableStream(IOBase):
def __init__(self, wrapped_stream):
self.wrapped_stream = wrapped_stream
def write(self, data):
return self.wrapped_stream.write(data)
def read(self, count):
return self.wrapped_stream.read(count)
def seek(self, *args, **kwargs):
raise UnsupportedOperation("boom!")
def tell(self):
return self.wrapped_stream.tell()
class MockClientResponse(Response):
def __init__(
self, url: str,
body_bytes: bytes,
headers: Dict[str, Any],
status: int = 200,
reason: str = "OK"
) -> None:
super(MockClientResponse).__init__()
self._url = url
self._body = body_bytes
self._content = body_bytes
self._cache = {}
self._loop = None
self._content_consumed = True
self.headers = headers
self.status_code = status
self.reason = reason
self.raw = HTTPResponse()
class MockLegacyTransport(HttpTransport):
"""
This transport returns legacy http response objects from azure core and is
intended only to test our backwards compatibility support.
"""
def send(self, request: HttpRequest, **kwargs: Any) -> RequestsTransportResponse:
if request.method == 'GET':
# download_blob
headers = {
"Content-Type": "application/octet-stream",
"Content-Range": "bytes 0-17/18",
"Content-Length": "18",
}
if "x-ms-range-get-content-md5" in request.headers:
headers["Content-MD5"] = "7Qdih1MuhjZehB6Sv8UNjA==" # cspell:disable-line
rest_response = RequestsTransportResponse(
request=request,
requests_response=MockClientResponse(
request.url,
b"Hello World!",
headers,
)
)
elif request.method == 'HEAD':
# get_blob_properties
rest_response = RequestsTransportResponse(
request=request,
requests_response=MockClientResponse(
request.url,
b"",
{
"Content-Type": "application/octet-stream",
"Content-Length": "1024",
},
)
)
elif request.method == 'PUT':
# upload_blob
rest_response = RequestsTransportResponse(
request=request,
requests_response=MockClientResponse(
request.url,
b"",
{
"Content-Length": "0",
},
201,
"Created"
)
)
elif request.method == 'DELETE':
# delete_blob
rest_response = RequestsTransportResponse(
request=request,
requests_response=MockClientResponse(
request.url,
b"",
{
"Content-Length": "0",
},
202,
"Accepted"
)
)
else:
raise ValueError("The request is not accepted as part of MockLegacyTransport.")
return rest_response
def __enter__(self) -> Self:
return self
def __exit__(self, *args: Any) -> None:
pass
def open(self) -> None:
pass
def close(self) -> None:
pass
class MockCoreTransport(RequestsTransport):
"""
This transport returns http response objects from azure core pipelines and is
intended only to test our backwards compatibility support.
"""
def send(self, request: HttpRequest, **kwargs: Any) -> RequestsTransportResponse:
if request.method == 'GET':
# download_blob
headers = {
"Content-Type": "application/octet-stream",
"Content-Range": "bytes 0-17/18",
"Content-Length": "18",
}
if "x-ms-range-get-content-md5" in request.headers:
headers["Content-MD5"] = "7Qdih1MuhjZehB6Sv8UNjA==" # cspell:disable-line
rest_response = RequestsTransportResponse(
request=request,
requests_response=MockClientResponse(
request.url,
b"Hello World!",
headers,
)
)
elif request.method == 'HEAD':
# get_blob_properties
rest_response = RequestsTransportResponse(
request=request,
requests_response=MockClientResponse(
request.url,
b"",
{
"Content-Type": "application/octet-stream",
"Content-Length": "1024",
},
)
)
elif request.method == 'PUT':
# upload_blob
rest_response = RequestsTransportResponse(
request=request,
requests_response=MockClientResponse(
request.url,
b"",
{
"Content-Length": "0",
},
201,
"Created"
)
)
elif request.method == 'DELETE':
# delete_blob
rest_response = RequestsTransportResponse(
request=request,
requests_response=MockClientResponse(
request.url,
b"",
{
"Content-Length": "0",
},
202,
"Accepted"
)
)
else:
raise ValueError("The request is not accepted as part of MockCoreTransport.")
return rest_response
def __enter__(self) -> Self:
return self
def __exit__(self, *args: Any) -> None:
pass
def open(self) -> None:
pass
def close(self) -> None:
pass