summaryrefslogtreecommitdiff
path: root/pecan/tests/middleware/test_static.py
blob: 976a747081397c254af160f2f59bcbcb059cf7c3 (plain)
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
from unittest import TestCase
from pecan.middleware.static import (StaticFileMiddleware, FileWrapper,
                                     _dump_date)

import os


class TestStaticFileMiddleware(TestCase):

    def setUp(self):
        def app(environ, start_response):
            response_headers = [('Content-type', 'text/plain')]
            start_response('200 OK', response_headers)
            return ['Hello world!\n']

        self.app = StaticFileMiddleware(
            app, os.path.dirname(__file__)
        )

        self._status = None
        self._response_headers = None

    def _request(self, path):
        def start_response(status, response_headers, exc_info=None):
            self._status = status
            self._response_headers = response_headers
        return self.app(
            dict(PATH_INFO=path),
            start_response
        )

    def _get_response_header(self, header):
        for k, v in self._response_headers:
            if k.upper() == header.upper():
                return v
        return None

    def test_file_can_be_found(self):
        result = self._request('/static_fixtures/text.txt')
        assert isinstance(result, FileWrapper)

    def test_no_file_found_causes_passthrough(self):
        result = self._request('/static_fixtures/nosuchfile.txt')
        assert not isinstance(result, FileWrapper)
        assert result == ['Hello world!\n']

    def test_mime_type_works_for_png_files(self):
        self._request('/static_fixtures/self.png')
        assert self._get_response_header('Content-Type') == 'image/png'

    def test_file_can_be_closed(self):
        result = self._request('/static_fixtures/text.txt')
        assert result.close() is None

    def test_file_can_be_iterated_over(self):
        result = self._request('/static_fixtures/text.txt')
        assert len([x for x in result])

    def test_date_dumping_on_unix_timestamps(self):
        result = _dump_date(1331755274.59, ' ')
        assert result == 'Wed, 14 Mar 2012 20:01:14 GMT'

    def test_separator_sanitization_still_finds_file(self):
        os.altsep = ':'
        result = self._request(':static_fixtures:text.txt')
        assert isinstance(result, FileWrapper)