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