Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 273 lines (249 sloc) 10.806 kb
d57f294 @maluke This commit aggregates most of the work done at the 2011 PyCon Pyramid s...
maluke authored
1 import unittest
2 from webob import Request
3 from webob import Response
4 from webob.dec import _format_args
5 from webob.dec import _func_name
6 from webob.dec import wsgify
7
8 class DecoratorTests(unittest.TestCase):
9 def _testit(self, app, req):
10 if isinstance(req, basestring):
11 req = Request.blank(req)
12 resp = req.get_response(app)
13 return resp
14
15 def test_wsgify(self):
16 resp_str = 'hey, this is a test: %s'
17 @wsgify
18 def test_app(req):
19 return resp_str % req.url
20 resp = self._testit(test_app, '/a url')
21 self.assertEqual(resp.body, resp_str % 'http://localhost/a%20url')
22 self.assertEqual(resp.content_length, 45)
23 self.assertEqual(resp.content_type, 'text/html')
24 self.assertEqual(resp.charset, 'UTF-8')
25 self.assertEqual('%r' % (test_app,), 'wsgify(tests.test_dec.test_app)')
26
27 def test_wsgify_empty_repr(self):
28 self.assertEqual('%r' % (wsgify(),), 'wsgify()')
29
30 def test_wsgify_args(self):
31 resp_str = 'hey hey my my'
32 @wsgify(args=(resp_str,))
33 def test_app(req, strarg):
34 return strarg
35 resp = self._testit(test_app, '/a url')
36 self.assertEqual(resp.body, resp_str)
37 self.assertEqual(resp.content_length, 13)
38 self.assertEqual(resp.content_type, 'text/html')
39 self.assertEqual(resp.charset, 'UTF-8')
40 self.assertEqual('%r' % (test_app,),
41 "wsgify(tests.test_dec.test_app, args=('%s',))" % resp_str)
42
43 def test_wsgify_kwargs(self):
44 resp_str = 'hey hey my my'
45 @wsgify(kwargs=dict(strarg=resp_str))
46 def test_app(req, strarg=''):
47 return strarg
48 resp = self._testit(test_app, '/a url')
49 self.assertEqual(resp.body, resp_str)
50 self.assertEqual(resp.content_length, 13)
51 self.assertEqual(resp.content_type, 'text/html')
52 self.assertEqual(resp.charset, 'UTF-8')
53 self.assertEqual('%r' % test_app,
54 "wsgify(tests.test_dec.test_app, "
55 "kwargs={'strarg': '%s'})" % resp_str)
56
57 def test_wsgify_raise_httpexception(self):
58 from webob.exc import HTTPBadRequest
59 @wsgify
60 def test_app(req):
61 raise HTTPBadRequest
62 resp = self._testit(test_app, '/a url')
63 self.assert_(resp.body.startswith('400 Bad Request'))
64 self.assertEqual(resp.content_type, 'text/plain')
65 self.assertEqual(resp.charset, 'UTF-8')
66 self.assertEqual('%r' % test_app,
67 "wsgify(tests.test_dec.test_app)")
68
69 def test_wsgify_no___get__(self):
70 # use a class instance instead of a fn so we wrap something w/
71 # no __get__
72 class TestApp(object):
73 def __call__(self, req):
74 return 'nothing to see here'
75 test_app = wsgify(TestApp())
76 resp = self._testit(test_app, '/a url')
77 self.assertEqual(resp.body, 'nothing to see here')
78 self.assert_(test_app.__get__(test_app) is test_app)
79
80 def test_wsgify_args_no_func(self):
81 test_app = wsgify(None, args=(1,))
82 self.assertRaises(TypeError, self._testit, test_app, '/a url')
83
84 def test_wsgify_wrong_sig(self):
85 @wsgify
86 def test_app(req):
87 return 'What have you done for me lately?'
88 req = dict()
89 self.assertRaises(TypeError, test_app, req, 1, 2)
90 self.assertRaises(TypeError, test_app, req, 1, key='word')
91
92 def test_wsgify_none_response(self):
93 @wsgify
94 def test_app(req):
95 return
96 resp = self._testit(test_app, '/a url')
97 self.assertEqual(resp.body, '')
98 self.assertEqual(resp.content_type, 'text/html')
99 self.assertEqual(resp.content_length, 0)
100
101 def test_wsgify_get(self):
102 resp_str = "What'choo talkin' about, Willis?"
103 @wsgify
104 def test_app(req):
105 return Response(resp_str)
106 resp = test_app.get('/url/path')
107 self.assertEqual(resp.body, resp_str)
108
109 def test_wsgify_post(self):
110 post_dict = dict(speaker='Robin',
111 words='Holy test coverage, Batman!')
112 @wsgify
113 def test_app(req):
114 return Response('%s: %s' % (req.POST['speaker'],
115 req.POST['words']))
116 resp = test_app.post('/url/path', post_dict)
117 self.assertEqual(resp.body, '%s: %s' % (post_dict['speaker'],
118 post_dict['words']))
119
120 def test_wsgify_request_method(self):
121 resp_str = 'Nice body!'
122 @wsgify
123 def test_app(req):
124 self.assertEqual(req.method, 'PUT')
125 return Response(req.body)
126 resp = test_app.request('/url/path', method='PUT',
127 body=resp_str)
128 self.assertEqual(resp.body, resp_str)
129 self.assertEqual(resp.content_length, 10)
130 self.assertEqual(resp.content_type, 'text/html')
131
132 def test_wsgify_undecorated(self):
133 def test_app(req):
134 return Response('whoa')
135 wrapped_test_app = wsgify(test_app)
136 self.assert_(wrapped_test_app.undecorated is test_app)
137
138 def test_wsgify_custom_request(self):
139 resp_str = 'hey, this is a test: %s'
140 class MyRequest(Request):
141 pass
142 @wsgify(RequestClass=MyRequest)
143 def test_app(req):
144 return resp_str % req.url
145 resp = self._testit(test_app, '/a url')
146 self.assertEqual(resp.body, resp_str % 'http://localhost/a%20url')
147 self.assertEqual(resp.content_length, 45)
148 self.assertEqual(resp.content_type, 'text/html')
149 self.assertEqual(resp.charset, 'UTF-8')
150 self.assertEqual('%r' % (test_app,), "wsgify(tests.test_dec.test_app, "
151 "RequestClass=<class 'tests.test_dec.MyRequest'>)")
152
153 def test_middleware(self):
154 resp_str = "These are the vars: %s"
155 @wsgify.middleware
156 def set_urlvar(req, app, **vars):
157 req.urlvars.update(vars)
158 return app(req)
159 from webob.dec import _MiddlewareFactory
160 self.assert_(set_urlvar.__class__ is _MiddlewareFactory)
161 repr = '%r' % (set_urlvar,)
162 self.assert_(repr.startswith('wsgify.middleware(<function set_urlvar at '))
163 @wsgify
164 def show_vars(req):
165 return resp_str % (sorted(req.urlvars.items()))
166 show_vars2 = set_urlvar(show_vars, a=1, b=2)
167 self.assertEqual('%r' % (show_vars2,),
168 'wsgify.middleware(tests.test_dec.set_urlvar)'
169 '(wsgify(tests.test_dec.show_vars), a=1, b=2)')
170 resp = self._testit(show_vars2, '/path')
171 self.assertEqual(resp.body, resp_str % "[('a', 1), ('b', 2)]")
172 self.assertEqual(resp.content_type, 'text/html')
173 self.assertEqual(resp.charset, 'UTF-8')
174 self.assertEqual(resp.content_length, 40)
175
176 def test_unbound_middleware(self):
177 @wsgify
178 def test_app(req):
179 return Response('Say wha!?')
180 unbound = wsgify.middleware(None, test_app, some='thing')
181 from webob.dec import _UnboundMiddleware
182 self.assert_(unbound.__class__ is _UnboundMiddleware)
183 self.assertEqual(unbound.kw, dict(some='thing'))
184 self.assertEqual('%r' % (unbound,),
185 "wsgify.middleware(wsgify(tests.test_dec.test_app), "
186 "some='thing')")
187 @unbound
188 def middle(req, app, **kw):
189 return app(req)
190 self.assert_(middle.__class__ is wsgify)
191 self.assertEqual('%r' % (middle,),
192 "wsgify.middleware(tests.test_dec.middle)"
193 "(wsgify(tests.test_dec.test_app), some='thing')")
194
195 def test_unbound_middleware_no_app(self):
196 unbound = wsgify.middleware(None, None)
197 from webob.dec import _UnboundMiddleware
198 self.assert_(unbound.__class__ is _UnboundMiddleware)
199 self.assertEqual(unbound.kw, dict())
200 self.assertEqual('%r' % (unbound,),
201 "wsgify.middleware()")
202
203 def test_classapp(self):
204 class HostMap(dict):
205 @wsgify
206 def __call__(self, req):
207 return self[req.host.split(':')[0]]
208 app = HostMap()
209 app['example.com'] = Response('1')
210 app['other.com'] = Response('2')
211 resp = Request.blank('http://example.com/').get_response(wsgify(app))
212 self.assertEqual(resp.content_type, 'text/html')
213 self.assertEqual(resp.charset, 'UTF-8')
214 self.assertEqual(resp.content_length, 1)
215 self.assertEqual(resp.body, '1')
216
217 def test__func_name(self):
218 def func():
219 pass
220 name = _func_name(func)
221 self.assertEqual(name, 'tests.test_dec.func')
222 name = _func_name('a')
223 self.assertEqual(name, "'a'")
224 class Klass(object):
225 @classmethod
226 def classmeth(cls):
227 pass
228 def meth(self):
229 pass
230 name = _func_name(Klass)
231 self.assertEqual(name, 'tests.test_dec.Klass')
232 k = Klass()
233 kname = _func_name(k)
234 self.assert_(kname.startswith('<tests.test_dec.Klass object at 0x'))
235 name = _func_name(k.meth)
236 self.assert_(name.startswith('tests.test_dec.%s' % kname))
237 self.assert_(name.endswith('>.meth'))
238 name = _func_name(Klass.meth)
239 self.assertEqual(name, 'tests.test_dec.Klass.meth')
240 name = _func_name(Klass.classmeth)
241 self.assertEqual(name, "tests.test_dec.<class "
242 "'tests.test_dec.Klass'>.classmeth")
243
244 def test__format_args(self):
245 args_rep = _format_args()
246 self.assertEqual(args_rep, '')
247 kw = dict(a=4, b=5, c=6)
248 args_rep = _format_args(args=(1, 2, 3), kw=kw)
249 self.assertEqual(args_rep, '1, 2, 3, a=4, b=5, c=6')
250 args_rep = _format_args(args=(1, 2, 3), kw=kw, leading_comma=True)
251 self.assertEqual(args_rep, ', 1, 2, 3, a=4, b=5, c=6')
252 class Klass(object):
253 a = 1
254 b = 2
255 c = 3
256 names = ['a', 'b', 'c']
257 obj = Klass()
258 self.assertRaises(AssertionError, _format_args, names=names)
259 args_rep = _format_args(obj=obj, names='a')
260 self.assertEqual(args_rep, 'a=1')
261 args_rep = _format_args(obj=obj, names=names)
262 self.assertEqual(args_rep, 'a=1, b=2, c=3')
263 args_rep = _format_args(kw=kw, defaults=dict(a=4, b=5))
264 self.assertEqual(args_rep, 'c=6')
e9eadf5 @maluke bring back to 100% test coverage
maluke authored
265
266 def test_middleware_direct_call(self):
267 @wsgify.middleware
268 def mw(req, app):
269 return 'foo'
270
271 app = mw(Response())
272 self.assertEqual(app(Request.blank('/')), 'foo')
Something went wrong with that request. Please try again.