File size: 1,265 Bytes
328e22b cd0bfbf 328e22b f1b9623 328e22b 82321d6 3fe716b 328e22b 3fe716b 8d2dc0d 328e22b 82321d6 3fe716b 328e22b 82321d6 328e22b 4068aa2 82321d6 328e22b 82321d6 328e22b 82321d6 328e22b 82321d6 f1b9623 |
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 |
# -*- coding: utf-8 -*-
import os
from unittest import mock
import pytest
from pytube import request
@mock.patch("pytube.request.urlopen")
def test_streaming(mock_urlopen):
fake_stream_binary = [
iter(os.urandom(8 * 1024)),
iter(os.urandom(8 * 1024)),
iter(os.urandom(8 * 1024)),
None,
]
response = mock.Mock()
response.read.side_effect = fake_stream_binary
mock_urlopen.return_value = response
response = request.stream("http://fakeassurl.gov")
call_count = len(list(response))
assert call_count == 3
@mock.patch("pytube.request.urlopen")
def test_headers(mock_urlopen):
response = mock.Mock()
response.info.return_value = {"content-length": "16384"}
mock_urlopen.return_value = response
response = request.head("http://fakeassurl.gov")
assert response == {"content-length": "16384"}
@mock.patch("pytube.request.urlopen")
def test_get(mock_urlopen):
response = mock.Mock()
response.read.return_value = "<html></html>".encode("utf-8")
mock_urlopen.return_value = response
response = request.get("http://fakeassurl.gov")
assert response == "<html></html>"
def test_get_non_http():
with pytest.raises(ValueError):
request.get("file://bad")
|