|
1 # -*- coding: utf-8 -*- |
|
2 from io import BytesIO |
|
3 |
|
4 import webtest |
|
5 |
|
6 import pyramid.request |
|
7 |
|
8 from cubicweb.pyramid.core import CubicWebPyramidRequest |
|
9 from cubicweb.pyramid.test import PyramidCWTest |
|
10 |
|
11 |
|
12 class WSGIAppTest(PyramidCWTest): |
|
13 def make_request(self, path, environ=None, **kw): |
|
14 r = webtest.app.TestRequest.blank(path, environ, **kw) |
|
15 |
|
16 request = pyramid.request.Request(r.environ) |
|
17 request.registry = self.pyr_registry |
|
18 |
|
19 return request |
|
20 |
|
21 def test_content_type(self): |
|
22 req = CubicWebPyramidRequest( |
|
23 self.make_request('/', {'CONTENT_TYPE': 'text/plain'})) |
|
24 |
|
25 self.assertEqual('text/plain', req.get_header('Content-Type')) |
|
26 |
|
27 def test_content_body(self): |
|
28 req = CubicWebPyramidRequest( |
|
29 self.make_request('/', { |
|
30 'CONTENT_LENGTH': 12, |
|
31 'CONTENT_TYPE': 'text/plain', |
|
32 'wsgi.input': BytesIO(b'some content')})) |
|
33 |
|
34 self.assertEqual(b'some content', req.content.read()) |
|
35 |
|
36 def test_http_scheme(self): |
|
37 req = CubicWebPyramidRequest( |
|
38 self.make_request('/', { |
|
39 'wsgi.url_scheme': 'http'})) |
|
40 |
|
41 self.assertFalse(req.https) |
|
42 |
|
43 def test_https_scheme(self): |
|
44 req = CubicWebPyramidRequest( |
|
45 self.make_request('/', { |
|
46 'wsgi.url_scheme': 'https'})) |
|
47 |
|
48 self.assertTrue(req.https) |
|
49 |
|
50 def test_https_prefix(self): |
|
51 r = self.webapp.get('/https/') |
|
52 self.assertIn('https://', r.text) |
|
53 |
|
54 def test_big_content(self): |
|
55 content = b'x'*100001 |
|
56 |
|
57 req = CubicWebPyramidRequest( |
|
58 self.make_request('/', { |
|
59 'CONTENT_LENGTH': len(content), |
|
60 'CONTENT_TYPE': 'text/plain', |
|
61 'wsgi.input': BytesIO(content)})) |
|
62 |
|
63 self.assertEqual(content, req.content.read()) |
|
64 |
|
65 def test_post(self): |
|
66 self.webapp.post( |
|
67 '/', |
|
68 params={'__login': self.admlogin, '__password': self.admpassword}) |
|
69 |
|
70 def test_get_multiple_variables(self): |
|
71 req = CubicWebPyramidRequest( |
|
72 self.make_request('/?arg=1&arg=2')) |
|
73 |
|
74 self.assertEqual([u'1', u'2'], req.form['arg']) |
|
75 |
|
76 def test_post_multiple_variables(self): |
|
77 req = CubicWebPyramidRequest( |
|
78 self.make_request('/', POST='arg=1&arg=2')) |
|
79 |
|
80 self.assertEqual([u'1', u'2'], req.form['arg']) |
|
81 |
|
82 def test_post_files(self): |
|
83 content_type, params = self.webapp.encode_multipart( |
|
84 (), (('filefield', 'aname', b'acontent'),)) |
|
85 req = CubicWebPyramidRequest( |
|
86 self.make_request('/', POST=params, content_type=content_type)) |
|
87 self.assertIn('filefield', req.form) |
|
88 fieldvalue = req.form['filefield'] |
|
89 self.assertEqual(u'aname', fieldvalue[0]) |
|
90 self.assertEqual(b'acontent', fieldvalue[1].read()) |
|
91 |
|
92 def test_post_unicode_urlencoded(self): |
|
93 params = 'arg=%C3%A9' |
|
94 req = CubicWebPyramidRequest( |
|
95 self.make_request( |
|
96 '/', POST=params, |
|
97 content_type='application/x-www-form-urlencoded')) |
|
98 self.assertEqual(u"é", req.form['arg']) |
|
99 |
|
100 |
|
101 if __name__ == '__main__': |
|
102 from unittest import main |
|
103 main() |