6
6
7
7
# NOTE: These tests are heavily inspired from the httpx test suite: https://github.com/encode/httpx/tree/master/tests
8
8
# Thank you httpx for your wonderful tests!
9
- import json
9
+ import functools
10
10
import pytest
11
11
import aiohttp
12
12
from azure .core .pipeline .transport import AioHttpTransport
13
13
from azure .core .rest import HttpRequest , AsyncHttpResponse
14
14
from azure .core .exceptions import HttpResponseError
15
15
from typing import Any , Dict
16
+ from testcase_async import _create_http_response
16
17
17
- async def _create_http_response (request = None ):
18
- internal_response = await AioHttpTransport ().send (request ._internal_request , stream = False )
19
- response = AsyncHttpResponse (
20
- request = request ,
21
- _internal_response = internal_response
22
- )
23
- return response
18
+ create_http_response = functools .partial (_create_http_response , stream = False )
24
19
25
20
@pytest .mark .asyncio
26
21
async def test_response ():
27
- response = await _create_http_response (
22
+ response = await create_http_response (
28
23
request = HttpRequest ("GET" , "http://localhost:3000/basic/helloWorld/string" ),
29
24
)
30
25
@@ -40,7 +35,7 @@ async def test_response():
40
35
41
36
@pytest .mark .asyncio
42
37
async def test_response_content ():
43
- response = await _create_http_response (
38
+ response = await create_http_response (
44
39
request = HttpRequest ("GET" , "http://localhost:3000/basic/helloWorld/bytes" ),
45
40
)
46
41
@@ -54,7 +49,7 @@ async def test_response_content():
54
49
55
50
@pytest .mark .asyncio
56
51
async def test_response_text ():
57
- response = await _create_http_response (
52
+ response = await create_http_response (
58
53
request = HttpRequest ("GET" , "http://localhost:3000/basic/helloWorld/text" ),
59
54
)
60
55
@@ -70,7 +65,7 @@ async def test_response_text():
70
65
71
66
@pytest .mark .asyncio
72
67
async def test_response_html ():
73
- response = await _create_http_response (
68
+ response = await create_http_response (
74
69
request = HttpRequest ("GET" , "http://localhost:3000/basic/helloWorld/html" ),
75
70
)
76
71
@@ -83,19 +78,19 @@ async def test_response_html():
83
78
84
79
@pytest .mark .asyncio
85
80
async def test_raise_for_status ():
86
- response = await _create_http_response (
81
+ response = await create_http_response (
87
82
request = HttpRequest ("GET" , "http://localhost:3000/basic/helloWorld/text" ),
88
83
)
89
84
response .raise_for_status ()
90
85
91
- response = await _create_http_response (
86
+ response = await create_http_response (
92
87
request = HttpRequest ("GET" , "http://localhost:3000/errors/403" ),
93
88
)
94
89
assert response .status_code == 403
95
90
with pytest .raises (HttpResponseError ):
96
91
response .raise_for_status ()
97
92
98
- response = await _create_http_response (
93
+ response = await create_http_response (
99
94
request = HttpRequest ("GET" , "http://localhost:3000/errors/500" ),
100
95
)
101
96
assert response .status_code == 500
@@ -104,7 +99,7 @@ async def test_raise_for_status():
104
99
105
100
@pytest .mark .asyncio
106
101
async def test_response_repr ():
107
- response = await _create_http_response (
102
+ response = await create_http_response (
108
103
request = HttpRequest ("GET" , "http://localhost:3000/basic/helloWorld/text" )
109
104
)
110
105
assert repr (response ) == "<AsyncHttpResponse: 200 OK, Content-Type: text/plain; charset=utf-8>"
@@ -114,7 +109,7 @@ async def test_response_content_type_encoding():
114
109
"""
115
110
Use the charset encoding in the Content-Type header if possible.
116
111
"""
117
- response = await _create_http_response (
112
+ response = await create_http_response (
118
113
request = HttpRequest ("GET" , "http://localhost:3000/encoding/latin-1" )
119
114
)
120
115
await response .read ()
@@ -128,7 +123,7 @@ async def test_response_autodetect_encoding():
128
123
"""
129
124
Autodetect encoding if there is no Content-Type header.
130
125
"""
131
- response = await _create_http_response (
126
+ response = await create_http_response (
132
127
request = HttpRequest ("GET" , "http://localhost:3000/encoding/latin-1" )
133
128
)
134
129
await response .read ()
@@ -144,7 +139,7 @@ async def test_response_fallback_to_autodetect():
144
139
"""
145
140
Fallback to autodetection if we get an invalid charset in the Content-Type header.
146
141
"""
147
- response = await _create_http_response (
142
+ response = await create_http_response (
148
143
request = HttpRequest ("GET" , "http://localhost:3000/encoding/latin-1" )
149
144
)
150
145
await response .read ()
@@ -161,7 +156,7 @@ async def test_response_no_charset_with_ascii_content():
161
156
A response with ascii encoded content should decode correctly,
162
157
even with no charset specified.
163
158
"""
164
- response = await _create_http_response (
159
+ response = await create_http_response (
165
160
request = HttpRequest ("GET" , "http://localhost:3000/basic/helloWorld/string" ),
166
161
)
167
162
@@ -181,7 +176,7 @@ async def test_response_no_charset_with_iso_8859_1_content():
181
176
A response with ISO 8859-1 encoded content should decode correctly,
182
177
even with no charset specified.
183
178
"""
184
- response = await _create_http_response (
179
+ response = await create_http_response (
185
180
request = HttpRequest ("GET" , "http://localhost:3000/encoding/iso-8859-1" ),
186
181
)
187
182
content = await response .read ()
@@ -194,7 +189,7 @@ async def test_response_no_charset_with_iso_8859_1_content():
194
189
195
190
@pytest .mark .asyncio
196
191
async def test_response_set_explicit_encoding ():
197
- response = await _create_http_response (
192
+ response = await create_http_response (
198
193
request = HttpRequest ("GET" , "http://localhost:3000/encoding/latin-1-string-with-utf-8" ),
199
194
)
200
195
response .encoding = "latin-1"
@@ -205,15 +200,15 @@ async def test_response_set_explicit_encoding():
205
200
206
201
@pytest .mark .asyncio
207
202
async def test_json ():
208
- response = await _create_http_response (
203
+ response = await create_http_response (
209
204
request = HttpRequest ("GET" , "http://localhost:3000/basic/json" ),
210
205
)
211
206
await response .read ()
212
207
assert response .json () == {"greeting" : "hello" , "recipient" : "world" }
213
208
214
209
@pytest .mark .asyncio
215
210
async def test_json_with_specified_encoding ():
216
- response = await _create_http_response (
211
+ response = await create_http_response (
217
212
request = HttpRequest ("GET" , "http://localhost:3000/encoding/json" ),
218
213
)
219
214
await response .read ()
@@ -222,23 +217,23 @@ async def test_json_with_specified_encoding():
222
217
223
218
@pytest .mark .asyncio
224
219
async def test_emoji ():
225
- response = await _create_http_response (
220
+ response = await create_http_response (
226
221
request = HttpRequest ("GET" , "http://localhost:3000/encoding/emoji" ),
227
222
)
228
223
await response .read ()
229
224
assert response .text == "👩"
230
225
231
226
@pytest .mark .asyncio
232
227
async def test_emoji_family_with_skin_tone_modifier ():
233
- response = await _create_http_response (
228
+ response = await create_http_response (
234
229
request = HttpRequest ("GET" , "http://localhost:3000/encoding/emoji-family-skin-tone-modifier" ),
235
230
)
236
231
await response .read ()
237
232
assert response .text == "👩🏻👩🏽👧🏾👦🏿 SSN: 859-98-0987"
238
233
239
234
@pytest .mark .asyncio
240
235
async def test_korean_nfc ():
241
- response = await _create_http_response (
236
+ response = await create_http_response (
242
237
request = HttpRequest ("GET" , "http://localhost:3000/encoding/korean" ),
243
238
)
244
239
await response .read ()
0 commit comments