-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decompress content while loading gzipped playlists
- Loading branch information
1 parent
e537b2d
commit f6b25e5
Showing
2 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import gzip | ||
import unittest | ||
from http.client import HTTPResponse | ||
from unittest.mock import Mock, patch | ||
|
||
from m3u8.httpclient import DefaultHTTPClient | ||
|
||
|
||
class MockHeaders: | ||
def __init__(self, encoding=None): | ||
self.encoding = encoding | ||
|
||
def get_content_charset(self, failobj="utf-8"): | ||
return self.encoding or failobj | ||
|
||
|
||
class TestDefaultHTTPClient(unittest.TestCase): | ||
@patch("urllib.request.OpenerDirector.open") | ||
def test_download_normal_content(self, mock_open): | ||
client = DefaultHTTPClient() | ||
mock_response = Mock(spec=HTTPResponse) | ||
mock_response.read.return_value = b"playlist content" | ||
mock_response.info.return_value = {} | ||
mock_response.geturl.return_value = "http://example.com/index.m3u8" | ||
mock_response.headers = MockHeaders() | ||
mock_open.return_value = mock_response | ||
|
||
content, base_uri = client.download("http://example.com/index.m3u8") | ||
|
||
self.assertEqual(content, "playlist content") | ||
self.assertEqual(base_uri, "http://example.com/") | ||
|
||
@patch("urllib.request.OpenerDirector.open") | ||
def test_download_gzipped_content(self, mock_open): | ||
client = DefaultHTTPClient() | ||
original_content = "playlist gzipped content" | ||
gzipped_content = gzip.compress(original_content.encode("utf-8")) | ||
mock_response = Mock(spec=HTTPResponse) | ||
mock_response.read.return_value = gzipped_content | ||
mock_response.info.return_value = {"Content-Encoding": "gzip"} | ||
mock_response.geturl.return_value = "http://example.com/index.m3u8" | ||
mock_response.headers = MockHeaders("utf-8") | ||
mock_open.return_value = mock_response | ||
|
||
content, base_uri = client.download("http://example.com/index.m3u8") | ||
|
||
self.assertEqual(content, original_content) | ||
self.assertEqual(base_uri, "http://example.com/") | ||
|
||
@patch("urllib.request.OpenerDirector.open") | ||
def test_download_with_proxy(self, mock_open): | ||
client = DefaultHTTPClient(proxies={"http": "http://proxy.example.com"}) | ||
mock_response = Mock(spec=HTTPResponse) | ||
mock_response.read.return_value = b"playlist proxied content" | ||
mock_response.info.return_value = {} | ||
mock_response.geturl.return_value = "http://example.com/index.m3u8" | ||
mock_response.headers = MockHeaders() | ||
mock_open.return_value = mock_response | ||
|
||
content, base_uri = client.download("http://example.com/index.m3u8") | ||
|
||
self.assertEqual(content, "playlist proxied content") | ||
self.assertEqual(base_uri, "http://example.com/") |