def test_file_with_new_api(): # check we use streaming mode to fetch files fp = io.BytesIO(b"") fp.mode = "b" file_content = b"hello world" web_url = "http://example.com/download_url/" api_url = "http://example.com/upload_url/" def fake_get(url, stream): raw = MagicMock() src = io.BytesIO(file_content) raw.read = src.read if url == web_url: raise UnauthorizedException() else: res = FakeResponse(200, {}) res.raw = raw res.headers = {'Content-Length': str(len(file_content))} return res with patch.object(File, "_get", side_effect=fake_get) as mock_get: f = File({}) f._download_url = web_url f._upload_url = api_url f.write_to(fp) fp.seek(0) assert file_content == fp.read() expected = call('http://example.com/download_url/', stream=True) assert expected in mock_get.mock_calls
def test_file_uses_streaming_request_without_content_length(): # check we use streaming mode to fetch files fp = io.BytesIO(b"") fp.mode = "b" file_content = b"hello world" def fake_get(url, stream): raw = MagicMock() src = io.BytesIO(file_content) raw.read = src.read res = FakeResponse(200, {}) res.raw = raw res.headers = {} return res with patch.object(File, "_get", side_effect=fake_get) as mock_get: f = File({}) f._download_url = "http://example.com/download_url/" f.write_to(fp) fp.seek(0) assert file_content == fp.read() expected = call('http://example.com/download_url/', stream=True) assert expected in mock_get.mock_calls