File: test_response.py

package info (click to toggle)
django-ranged-response 0.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 136 kB
  • sloc: python: 150; makefile: 4
file content (69 lines) | stat: -rw-r--r-- 2,170 bytes parent folder | download | duplicates (2)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import io

from django.test.client import RequestFactory
from django.test.testcases import TestCase

from ranged_response import RangedFileResponse


class testResponse(TestCase):

    def setUp(self):
        self.factory = RequestFactory()

    def test_begin(self):
        request = self.factory.get(
            '/path', HTTP_RANGE='bytes=0-3'
        )
        response = RangedFileResponse(
            request, io.BytesIO(b'sui2khiau2tsiang5'), content_type='audio/wav'
        )
        self.assertContent(response, b'sui2')

    def test_middle(self):
        request = self.factory.get(
            '/path', HTTP_RANGE='bytes=4-9'
        )
        response = RangedFileResponse(
            request, io.BytesIO(b'sui2khiau2tsiang5'), content_type='audio/wav'
        )
        self.assertContent(response, b'khiau2')

    def test_end(self):
        request = self.factory.get(
            '/path', HTTP_RANGE='bytes=10-16'
        )
        response = RangedFileResponse(
            request, io.BytesIO(b'sui2khiau2tsiang5'), content_type='audio/wav'
        )
        self.assertContent(response, b'tsiang5')

    def test_failing(self):
        request = self.factory.get(
            '/path', HTTP_RANGE='bytes=17-20'
        )
        response = RangedFileResponse(
            request, io.BytesIO(b'sui2khiau2tsiang5'), content_type='audio/wav'
        )
        self.assertEqual(response.status_code, 416)

    def test_overlapping(self):
        request = self.factory.get(
            '/path', HTTP_RANGE='bytes=10-20'
        )
        response = RangedFileResponse(
            request, io.BytesIO(b'sui2khiau2tsiang5'), content_type='audio/wav'
        )
        self.assertContent(response, b'tsiang5')

    def test_more_one_char(self):
        request = self.factory.get(
            '/path', HTTP_RANGE='bytes=10-17'
        )
        response = RangedFileResponse(
            request, io.BytesIO(b'sui2khiau2tsiang5'), content_type='audio/wav'
        )
        self.assertContent(response, b'tsiang5')

    def assertContent(self, response, except_response):
        self.assertEqual(list(response.streaming_content)[0], except_response)