File: test_streaming.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (211 lines) | stat: -rw-r--r-- 9,639 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
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
# --------------------------------------------------------------------------
#
# 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 pytest
from azure.core.pipeline.transport import RequestsTransport
from azure.core import PipelineClient
from azure.core.exceptions import DecodeError, HttpResponseError
from azure.core.pipeline.transport import RequestsTransport
from utils import HTTP_REQUESTS

@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_decompress_plain_no_header(http_request):
    # expect plain text
    account_name = "coretests"
    account_url = "https://{}.blob.core.windows.net".format(account_name)
    url = "https://{}.blob.core.windows.net/tests/test.txt".format(account_name)
    client = PipelineClient(account_url)
    request = http_request("GET", url)
    pipeline_response = client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=True)
    content = b"".join(list(data))
    decoded = content.decode('utf-8')
    assert decoded == "test"

@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_compress_plain_no_header_offline(port, http_request):
    # cspell:disable-next-line
    # thanks to Daisy Cisneros for this test!
    # expect plain text
    request = http_request(method="GET", url="http://localhost:{}/streams/string".format(port))
    with RequestsTransport() as sender:
        response = sender.send(request, stream=True)
        response.raise_for_status()
        data = response.stream_download(sender, decompress=False)
        content = b"".join(list(data))
        decoded = content.decode('utf-8')
        assert decoded == "test"

@pytest.mark.live_test_only
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_compress_plain_no_header(http_request):
    # expect plain text
    account_name = "coretests"
    account_url = "https://{}.blob.core.windows.net".format(account_name)
    url = "https://{}.blob.core.windows.net/tests/test.txt".format(account_name)
    client = PipelineClient(account_url)
    request = http_request("GET", url)
    pipeline_response = client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=False)
    content = b"".join(list(data))
    decoded = content.decode('utf-8')
    assert decoded == "test"

@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_decompress_compressed_no_header(http_request):
    # expect compressed text
    account_name = "coretests"
    account_url = "https://{}.blob.core.windows.net".format(account_name)
    url = "https://{}.blob.core.windows.net/tests/test.tar.gz".format(account_name)
    client = PipelineClient(account_url)
    request = http_request("GET", url)
    pipeline_response = client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=True)
    content = b"".join(list(data))
    try:
        decoded = content.decode('utf-8')
        assert False
    except UnicodeDecodeError:
        pass

@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_compress_compressed_no_header_offline(port, http_request):
    # expect compressed text
    client = PipelineClient("")
    request = http_request(method="GET", url="http://localhost:{}/streams/compressed_no_header".format(port))
    pipeline_response = client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=False)
    content = b"".join(list(data))
    with pytest.raises(UnicodeDecodeError):
        decoded = content.decode('utf-8')

@pytest.mark.live_test_only
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_compress_compressed_no_header(http_request):
    # expect compressed text
    account_name = "coretests"
    account_url = "https://{}.blob.core.windows.net".format(account_name)
    url = "https://{}.blob.core.windows.net/tests/test.tar.gz".format(account_name)
    client = PipelineClient(account_url)
    request = http_request("GET", url)
    pipeline_response = client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=False)
    content = b"".join(list(data))
    try:
        decoded = content.decode('utf-8')
        assert False
    except UnicodeDecodeError:
        pass

@pytest.mark.live_test_only
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_decompress_plain_header(http_request):
    # expect error
    account_name = "coretests"
    account_url = "https://{}.blob.core.windows.net".format(account_name)
    url = "https://{}.blob.core.windows.net/tests/test_with_header.txt".format(account_name)
    client = PipelineClient(account_url)
    request = http_request("GET", url)
    pipeline_response = client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=True)
    with pytest.raises(DecodeError):
        list(data)

@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_decompress_plain_header_offline(port, http_request):
    request = http_request(method="GET", url="http://localhost:{}/streams/compressed".format(port))
    with RequestsTransport() as sender:
        response = sender.send(request, stream=True)
        response.raise_for_status()
        data = response.stream_download(sender, decompress=True)
        with pytest.raises(DecodeError):
            list(data)

@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_compress_plain_header(http_request):
    # expect plain text
    account_name = "coretests"
    account_url = "https://{}.blob.core.windows.net".format(account_name)
    url = "https://{}.blob.core.windows.net/tests/test_with_header.txt".format(account_name)
    client = PipelineClient(account_url)
    request = http_request("GET", url)
    pipeline_response = client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=False)
    content = b"".join(list(data))
    decoded = content.decode('utf-8')
    assert decoded == "test"

@pytest.mark.live_test_only
@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_decompress_compressed_header(http_request):
    # expect plain text
    account_name = "coretests"
    account_url = "https://{}.blob.core.windows.net".format(account_name)
    url = "https://{}.blob.core.windows.net/tests/test_with_header.tar.gz".format(account_name)
    client = PipelineClient(account_url)
    request = http_request("GET", url)
    pipeline_response = client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=True)
    content = b"".join(list(data))
    decoded = content.decode('utf-8')
    assert decoded == "test"

@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_decompress_compressed_header_offline(port, http_request):
    client = PipelineClient("")
    request = http_request(method="GET", url="http://localhost:{}/streams/decompress_header".format(port))
    with RequestsTransport() as sender:
        response = client._pipeline.run(request, stream=True).http_response
        response.raise_for_status()
        data = response.stream_download(sender, decompress=True)
        content = b"".join(list(data))
        decoded = content.decode('utf-8')
        assert decoded == "test"

@pytest.mark.parametrize("http_request", HTTP_REQUESTS)
def test_compress_compressed_header(http_request):
    # expect compressed text
    account_name = "coretests"
    account_url = "https://{}.blob.core.windows.net".format(account_name)
    url = "https://{}.blob.core.windows.net/tests/test_with_header.tar.gz".format(account_name)
    client = PipelineClient(account_url)
    request = http_request("GET", url)
    pipeline_response = client._pipeline.run(request, stream=True)
    response = pipeline_response.http_response
    data = response.stream_download(client._pipeline, decompress=False)
    content = b"".join(list(data))
    try:
        decoded = content.decode('utf-8')
        assert False
    except UnicodeDecodeError:
        pass