4 from StringIO import StringIO |
4 from StringIO import StringIO |
5 |
5 |
6 from cubicweb.devtools.webtest import CubicWebTestTC |
6 from cubicweb.devtools.webtest import CubicWebTestTC |
7 |
7 |
8 from cubicweb.wsgi.request import CubicWebWsgiRequest |
8 from cubicweb.wsgi.request import CubicWebWsgiRequest |
|
9 from cubicweb.multipart import MultipartError |
9 |
10 |
10 |
11 |
11 class WSGIAppTC(CubicWebTestTC): |
12 class WSGIAppTC(CubicWebTestTC): |
12 def test_content_type(self): |
13 def test_content_type(self): |
13 r = webtest.app.TestRequest.blank('/', {'CONTENT_TYPE': 'text/plain'}) |
14 r = webtest.app.TestRequest.blank('/', {'CONTENT_TYPE': 'text/plain'}) |
64 def test_post(self): |
65 def test_post(self): |
65 self.webapp.post( |
66 self.webapp.post( |
66 '/', |
67 '/', |
67 params={'__login': self.admlogin, '__password': self.admpassword}) |
68 params={'__login': self.admlogin, '__password': self.admpassword}) |
68 |
69 |
|
70 def test_post_bad_form(self): |
|
71 with self.assertRaises(MultipartError): |
|
72 self.webapp.post( |
|
73 '/', |
|
74 params='badcontent', |
|
75 headers={'Content-Type': 'multipart/form-data'}) |
|
76 |
|
77 def test_post_non_form(self): |
|
78 self.webapp.post( |
|
79 '/', |
|
80 params='{}', |
|
81 headers={'Content-Type': 'application/json'}) |
|
82 |
69 def test_get_multiple_variables(self): |
83 def test_get_multiple_variables(self): |
70 r = webtest.app.TestRequest.blank('/?arg=1&arg=2') |
84 r = webtest.app.TestRequest.blank('/?arg=1&arg=2') |
71 req = CubicWebWsgiRequest(r.environ, self.vreg) |
85 req = CubicWebWsgiRequest(r.environ, self.vreg) |
72 |
86 |
73 self.assertEqual([u'1', u'2'], req.form['arg']) |
87 self.assertEqual([u'1', u'2'], req.form['arg']) |
75 def test_post_multiple_variables(self): |
89 def test_post_multiple_variables(self): |
76 r = webtest.app.TestRequest.blank('/', POST='arg=1&arg=2') |
90 r = webtest.app.TestRequest.blank('/', POST='arg=1&arg=2') |
77 req = CubicWebWsgiRequest(r.environ, self.vreg) |
91 req = CubicWebWsgiRequest(r.environ, self.vreg) |
78 |
92 |
79 self.assertEqual([u'1', u'2'], req.form['arg']) |
93 self.assertEqual([u'1', u'2'], req.form['arg']) |
|
94 |
|
95 def test_post_files(self): |
|
96 content_type, params = self.webapp.encode_multipart( |
|
97 (), (('filefield', 'aname', 'acontent'),)) |
|
98 r = webtest.app.TestRequest.blank( |
|
99 '/', POST=params, content_type=content_type) |
|
100 req = CubicWebWsgiRequest(r.environ, self.vreg) |
|
101 self.assertIn('filefield', req.form) |
|
102 fieldvalue = req.form['filefield'] |
|
103 self.assertEqual(u'aname', fieldvalue[0]) |
|
104 self.assertEqual('acontent', fieldvalue[1].read()) |
80 |
105 |
81 def test_post_unicode_urlencoded(self): |
106 def test_post_unicode_urlencoded(self): |
82 params = 'arg=%C3%A9' |
107 params = 'arg=%C3%A9' |
83 r = webtest.app.TestRequest.blank( |
108 r = webtest.app.TestRequest.blank( |
84 '/', POST=params, content_type='application/x-www-form-urlencoded') |
109 '/', POST=params, content_type='application/x-www-form-urlencoded') |