Bowe Strickland
2018-10-27 6e49871feaa1a60549206cf5512c9fb7f3d5fd56
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import io
import mimetypes
import os
import unittest
from pyramid import testing
 
 
class TestResponse(unittest.TestCase):
    def _getTargetClass(self):
        from pyramid.response import Response
 
        return Response
 
    def test_implements_IResponse(self):
        from pyramid.interfaces import IResponse
 
        cls = self._getTargetClass()
        self.assertTrue(IResponse.implementedBy(cls))
 
    def test_provides_IResponse(self):
        from pyramid.interfaces import IResponse
 
        inst = self._getTargetClass()()
        self.assertTrue(IResponse.providedBy(inst))
 
 
class TestFileResponse(unittest.TestCase):
    def _makeOne(self, file, **kw):
        from pyramid.response import FileResponse
 
        return FileResponse(file, **kw)
 
    def _getPath(self, suffix='txt'):
        here = os.path.dirname(__file__)
        return os.path.join(here, 'fixtures', 'minimal.%s' % (suffix,))
 
    def test_with_image_content_type(self):
        path = self._getPath('jpg')
        r = self._makeOne(path, content_type='image/jpeg')
        self.assertEqual(r.content_type, 'image/jpeg')
        self.assertEqual(r.headers['content-type'], 'image/jpeg')
        path = self._getPath()
        r.app_iter.close()
 
    def test_with_xml_content_type(self):
        path = self._getPath('xml')
        r = self._makeOne(path, content_type='application/xml')
        self.assertEqual(r.content_type, 'application/xml')
        self.assertEqual(
            r.headers['content-type'], 'application/xml; charset=UTF-8'
        )
        r.app_iter.close()
 
    def test_with_pdf_content_type(self):
        path = self._getPath('xml')
        r = self._makeOne(path, content_type='application/pdf')
        self.assertEqual(r.content_type, 'application/pdf')
        self.assertEqual(r.headers['content-type'], 'application/pdf')
        r.app_iter.close()
 
    def test_without_content_type(self):
        for suffix in ('txt', 'xml', 'pdf'):
            path = self._getPath(suffix)
            r = self._makeOne(path)
            self.assertEqual(
                r.headers['content-type'].split(';')[0],
                mimetypes.guess_type(path, strict=False)[0],
            )
            r.app_iter.close()
 
    def test_python_277_bug_15207(self):
        # python 2.7.7 on windows has a bug where its mimetypes.guess_type
        # function returns Unicode for the content_type, unlike any previous
        # version of Python.  See https://github.com/Pylons/pyramid/issues/1360
        # for more information.
        from pyramid.compat import text_
        import mimetypes as old_mimetypes
        from pyramid import response
 
        class FakeMimetypesModule(object):
            def guess_type(self, *arg, **kw):
                return text_('foo/bar'), None
 
        fake_mimetypes = FakeMimetypesModule()
        try:
            response.mimetypes = fake_mimetypes
            path = self._getPath('xml')
            r = self._makeOne(path)
            self.assertEqual(r.content_type, 'foo/bar')
            self.assertEqual(type(r.content_type), str)
        finally:
            response.mimetypes = old_mimetypes
 
 
class TestFileIter(unittest.TestCase):
    def _makeOne(self, file, block_size):
        from pyramid.response import FileIter
 
        return FileIter(file, block_size)
 
    def test___iter__(self):
        f = io.BytesIO(b'abc')
        inst = self._makeOne(f, 1)
        self.assertEqual(inst.__iter__(), inst)
 
    def test_iteration(self):
        data = b'abcdef'
        f = io.BytesIO(b'abcdef')
        inst = self._makeOne(f, 1)
        r = b''
        for x in inst:
            self.assertEqual(len(x), 1)
            r += x
        self.assertEqual(r, data)
 
    def test_close(self):
        f = io.BytesIO(b'abc')
        inst = self._makeOne(f, 1)
        inst.close()
        self.assertTrue(f.closed)
 
 
class Test_patch_mimetypes(unittest.TestCase):
    def _callFUT(self, module):
        from pyramid.response import init_mimetypes
 
        return init_mimetypes(module)
 
    def test_has_init(self):
        class DummyMimetypes(object):
            def init(self):
                self.initted = True
 
        module = DummyMimetypes()
        result = self._callFUT(module)
        self.assertEqual(result, True)
        self.assertEqual(module.initted, True)
 
    def test_missing_init(self):
        class DummyMimetypes(object):
            pass
 
        module = DummyMimetypes()
        result = self._callFUT(module)
        self.assertEqual(result, False)
 
 
class TestResponseAdapter(unittest.TestCase):
    def setUp(self):
        registry = Dummy()
        self.config = testing.setUp(registry=registry)
 
    def tearDown(self):
        self.config.end()
 
    def _makeOne(self, *types_or_ifaces, **kw):
        from pyramid.response import response_adapter
 
        return response_adapter(*types_or_ifaces, **kw)
 
    def test_register_single(self):
        from zope.interface import Interface
 
        class IFoo(Interface):
            pass
 
        dec = self._makeOne(IFoo)
 
        def foo():  # pragma: no cover
            pass
 
        config = DummyConfigurator()
        scanner = Dummy()
        scanner.config = config
        dec.register(scanner, None, foo)
        self.assertEqual(config.adapters, [(foo, IFoo)])
 
    def test_register_multi(self):
        from zope.interface import Interface
 
        class IFoo(Interface):
            pass
 
        class IBar(Interface):
            pass
 
        dec = self._makeOne(IFoo, IBar)
 
        def foo():  # pragma: no cover
            pass
 
        config = DummyConfigurator()
        scanner = Dummy()
        scanner.config = config
        dec.register(scanner, None, foo)
        self.assertEqual(config.adapters, [(foo, IFoo), (foo, IBar)])
 
    def test___call__(self):
        from zope.interface import Interface
 
        class IFoo(Interface):
            pass
 
        dec = self._makeOne(IFoo)
        dummy_venusian = DummyVenusian()
        dec.venusian = dummy_venusian
 
        def foo():  # pragma: no cover
            pass
 
        dec(foo)
        self.assertEqual(
            dummy_venusian.attached, [(foo, dec.register, 'pyramid', 1)]
        )
 
    def test___call___with_venusian_args(self):
        from zope.interface import Interface
 
        class IFoo(Interface):
            pass
 
        dec = self._makeOne(IFoo, _category='foo', _depth=1)
        dummy_venusian = DummyVenusian()
        dec.venusian = dummy_venusian
 
        def foo():  # pragma: no cover
            pass
 
        dec(foo)
        self.assertEqual(
            dummy_venusian.attached, [(foo, dec.register, 'foo', 2)]
        )
 
 
class TestGetResponseFactory(unittest.TestCase):
    def test_get_factory(self):
        from pyramid.registry import Registry
        from pyramid.response import Response, _get_response_factory
 
        registry = Registry()
        response = _get_response_factory(registry)(None)
        self.assertTrue(isinstance(response, Response))
 
 
class Dummy(object):
    pass
 
 
class DummyConfigurator(object):
    def __init__(self):
        self.adapters = []
 
    def add_response_adapter(self, wrapped, type_or_iface):
        self.adapters.append((wrapped, type_or_iface))
 
 
class DummyVenusian(object):
    def __init__(self):
        self.attached = []
 
    def attach(self, wrapped, fn, category=None, depth=None):
        self.attached.append((wrapped, fn, category, depth))