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