diff --git a/common/djangoapps/terrain/stubs/tests/test_video.py b/common/djangoapps/terrain/stubs/tests/test_video.py new file mode 100644 index 0000000000..de7324bf4d --- /dev/null +++ b/common/djangoapps/terrain/stubs/tests/test_video.py @@ -0,0 +1,44 @@ +""" +Unit tests for Video stub server implementation. +""" +import unittest +import requests +from terrain.stubs.video_source import VideoSourceHttpService +from django.conf import settings + +HLS_MANIFEST_TEXT = """ +#EXTM3U +#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=264787,RESOLUTION=1280x720 +history_264kbit/history_264kbit.m3u8 +#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=328415,RESOLUTION=1920x1080 +history_328kbit/history_328kbit.m3u8 +#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=70750,RESOLUTION=640x360 +history_70kbit/history_70kbit.m3u8 +#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=148269,RESOLUTION=960x540 +history_148kbit/history_148kbit.m3u8 +#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=41276,RESOLUTION=640x360 +history_41kbit/history_41kbit.m3u8 +""" + + +class StubVideoServiceTest(unittest.TestCase): + """ + Test cases for the video stub service. + """ + def setUp(self): + """ + Start the stub server. + """ + super(StubVideoServiceTest, self).setUp() + self.server = VideoSourceHttpService() + self.server.config['root_dir'] = '{}/data/video'.format(settings.TEST_ROOT) + self.addCleanup(self.server.shutdown) + + def test_get_hls_manifest(self): + """ + Verify that correct hls manifest is received. + """ + response = requests.get("http://127.0.0.1:{port}/hls/history.m3u8".format(port=self.server.port)) + self.assertTrue(response.ok) + self.assertEqual(response.text, HLS_MANIFEST_TEXT.lstrip()) + self.assertEqual(response.headers['Access-Control-Allow-Origin'], '*') diff --git a/common/djangoapps/terrain/stubs/video_source.py b/common/djangoapps/terrain/stubs/video_source.py index fa48834546..1265ba286d 100644 --- a/common/djangoapps/terrain/stubs/video_source.py +++ b/common/djangoapps/terrain/stubs/video_source.py @@ -24,6 +24,13 @@ class VideoSourceRequestHandler(SimpleHTTPRequestHandler): path = '{}{}'.format(root_dir, path) return path.split('?')[0] + def end_headers(self): + """ + This is required by hls.js to play hls videos. + """ + self.send_header('Access-Control-Allow-Origin', '*') + SimpleHTTPRequestHandler.end_headers(self) + class VideoSourceHttpService(StubHttpService): """ diff --git a/test_root/data/video/hls/history.m3u8 b/test_root/data/video/hls/history.m3u8 new file mode 100644 index 0000000000..1742dca8d2 --- /dev/null +++ b/test_root/data/video/hls/history.m3u8 @@ -0,0 +1,11 @@ +#EXTM3U +#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=264787,RESOLUTION=1280x720 +history_264kbit/history_264kbit.m3u8 +#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=328415,RESOLUTION=1920x1080 +history_328kbit/history_328kbit.m3u8 +#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=70750,RESOLUTION=640x360 +history_70kbit/history_70kbit.m3u8 +#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=148269,RESOLUTION=960x540 +history_148kbit/history_148kbit.m3u8 +#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=41276,RESOLUTION=640x360 +history_41kbit/history_41kbit.m3u8 diff --git a/test_root/data/video/hls/history_148kbit/XXXXXXXXT114-V015600_2_0.ts b/test_root/data/video/hls/history_148kbit/XXXXXXXXT114-V015600_2_0.ts new file mode 100644 index 0000000000..66ef4a9459 Binary files /dev/null and b/test_root/data/video/hls/history_148kbit/XXXXXXXXT114-V015600_2_0.ts differ diff --git a/test_root/data/video/hls/history_148kbit/history_148kbit.m3u8 b/test_root/data/video/hls/history_148kbit/history_148kbit.m3u8 new file mode 100644 index 0000000000..685110ac3e --- /dev/null +++ b/test_root/data/video/hls/history_148kbit/history_148kbit.m3u8 @@ -0,0 +1,7 @@ +#EXTM3U +#EXT-X-VERSION:3 +#EXT-X-TARGETDURATION:11 +#EXT-X-MEDIA-SEQUENCE:0 +#EXTINF:9.576244, +XXXXXXXXT114-V015600_2_0.ts +#EXT-X-ENDLIST diff --git a/test_root/data/video/hls/history_264kbit/XXXXXXXXT114-V015600_1_0.ts b/test_root/data/video/hls/history_264kbit/XXXXXXXXT114-V015600_1_0.ts new file mode 100644 index 0000000000..780d3209a5 Binary files /dev/null and b/test_root/data/video/hls/history_264kbit/XXXXXXXXT114-V015600_1_0.ts differ diff --git a/test_root/data/video/hls/history_264kbit/history_264kbit.m3u8 b/test_root/data/video/hls/history_264kbit/history_264kbit.m3u8 new file mode 100644 index 0000000000..55bf48b9ae --- /dev/null +++ b/test_root/data/video/hls/history_264kbit/history_264kbit.m3u8 @@ -0,0 +1,7 @@ +#EXTM3U +#EXT-X-VERSION:3 +#EXT-X-TARGETDURATION:11 +#EXT-X-MEDIA-SEQUENCE:0 +#EXTINF:9.576244, +XXXXXXXXT114-V015600_1_0.ts +#EXT-X-ENDLIST diff --git a/test_root/data/video/hls/history_328kbit/XXXXXXXXT114-V015600_0_0.ts b/test_root/data/video/hls/history_328kbit/XXXXXXXXT114-V015600_0_0.ts new file mode 100644 index 0000000000..9bcd84bffa Binary files /dev/null and b/test_root/data/video/hls/history_328kbit/XXXXXXXXT114-V015600_0_0.ts differ diff --git a/test_root/data/video/hls/history_328kbit/history_328kbit.m3u8 b/test_root/data/video/hls/history_328kbit/history_328kbit.m3u8 new file mode 100644 index 0000000000..696833516e --- /dev/null +++ b/test_root/data/video/hls/history_328kbit/history_328kbit.m3u8 @@ -0,0 +1,7 @@ +#EXTM3U +#EXT-X-VERSION:3 +#EXT-X-TARGETDURATION:11 +#EXT-X-MEDIA-SEQUENCE:0 +#EXTINF:9.576244, +XXXXXXXXT114-V015600_0_0.ts +#EXT-X-ENDLIST diff --git a/test_root/data/video/hls/history_41kbit/XXXXXXXXT114-V015600_4_0.ts b/test_root/data/video/hls/history_41kbit/XXXXXXXXT114-V015600_4_0.ts new file mode 100644 index 0000000000..e445515c18 Binary files /dev/null and b/test_root/data/video/hls/history_41kbit/XXXXXXXXT114-V015600_4_0.ts differ diff --git a/test_root/data/video/hls/history_41kbit/history_41kbit.m3u8 b/test_root/data/video/hls/history_41kbit/history_41kbit.m3u8 new file mode 100644 index 0000000000..7f32d6ed9c --- /dev/null +++ b/test_root/data/video/hls/history_41kbit/history_41kbit.m3u8 @@ -0,0 +1,7 @@ +#EXTM3U +#EXT-X-VERSION:3 +#EXT-X-TARGETDURATION:11 +#EXT-X-MEDIA-SEQUENCE:0 +#EXTINF:9.609611, +XXXXXXXXT114-V015600_4_0.ts +#EXT-X-ENDLIST diff --git a/test_root/data/video/hls/history_70kbit/XXXXXXXXT114-V015600_3_0.ts b/test_root/data/video/hls/history_70kbit/XXXXXXXXT114-V015600_3_0.ts new file mode 100644 index 0000000000..a2936b83b0 Binary files /dev/null and b/test_root/data/video/hls/history_70kbit/XXXXXXXXT114-V015600_3_0.ts differ diff --git a/test_root/data/video/hls/history_70kbit/history_70kbit.m3u8 b/test_root/data/video/hls/history_70kbit/history_70kbit.m3u8 new file mode 100644 index 0000000000..a417112a85 --- /dev/null +++ b/test_root/data/video/hls/history_70kbit/history_70kbit.m3u8 @@ -0,0 +1,7 @@ +#EXTM3U +#EXT-X-VERSION:3 +#EXT-X-TARGETDURATION:11 +#EXT-X-MEDIA-SEQUENCE:0 +#EXTINF:9.609611, +XXXXXXXXT114-V015600_3_0.ts +#EXT-X-ENDLIST