|
7 | 7 | from typing import Optional
|
8 | 8 | import pytest
|
9 | 9 | import responses
|
| 10 | +import requests |
10 | 11 | import jwt
|
11 | 12 | from ibm_cloud_sdk_core import BaseService, DetailedResponse
|
12 | 13 | from ibm_cloud_sdk_core import ApiException
|
@@ -80,6 +81,13 @@ def head_request(self) -> DetailedResponse:
|
80 | 81 | response = self.send(request)
|
81 | 82 | return response
|
82 | 83 |
|
| 84 | + def get_document_as_stream(self) -> DetailedResponse: |
| 85 | + params = {'version': self.version} |
| 86 | + url = '/v1/streamjson' |
| 87 | + request = self.prepare_request(method='GET', url=url, params=params) |
| 88 | + response = self.send(request, stream=True) |
| 89 | + return response |
| 90 | + |
83 | 91 |
|
84 | 92 | def get_access_token() -> str:
|
85 | 93 | access_token_layout = {
|
@@ -135,6 +143,45 @@ def test_url_encoding():
|
135 | 143 | assert 'version=2017-07-07' in responses.calls[0].request.url
|
136 | 144 |
|
137 | 145 |
|
| 146 | +@responses.activate |
| 147 | +def test_stream_json_response(): |
| 148 | + service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator()) |
| 149 | + |
| 150 | + path = '/v1/streamjson' |
| 151 | + test_url = service.default_url + path |
| 152 | + |
| 153 | + expected_response = json.dumps({"id": 1, "rev": "v1", "content": "this is a document"}) |
| 154 | + |
| 155 | + # print("Expected response: ", expected_response) |
| 156 | + |
| 157 | + # Simulate a JSON response |
| 158 | + responses.add( |
| 159 | + responses.GET, |
| 160 | + test_url, |
| 161 | + status=200, |
| 162 | + body=expected_response, |
| 163 | + content_type='application/json') |
| 164 | + |
| 165 | + # Invoke the operation and receive an "iterable" as the response |
| 166 | + response = service.get_document_as_stream() |
| 167 | + |
| 168 | + assert response is not None |
| 169 | + assert len(responses.calls) == 1 |
| 170 | + |
| 171 | + # retrieve the requests.Response object from the DetailedResponse |
| 172 | + resp = response.get_result() |
| 173 | + assert isinstance(resp, requests.Response) |
| 174 | + assert hasattr(resp, "iter_content") |
| 175 | + |
| 176 | + # Retrieve the response body, one chunk at a time. |
| 177 | + actual_response = '' |
| 178 | + for chunk in resp.iter_content(chunk_size=3): |
| 179 | + actual_response += chunk.decode("utf-8") |
| 180 | + |
| 181 | + # print("Actual response: ", actual_response) |
| 182 | + assert actual_response == expected_response |
| 183 | + |
| 184 | + |
138 | 185 | @responses.activate
|
139 | 186 | def test_http_config():
|
140 | 187 | service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator())
|
|
0 commit comments