def test_issue_5118(self):
     """Unable to read POST variables ONCE self.request.body is read."""
     if not test_base.check_webob_version(1.0):
         return
     import cgi
     req = webapp2.Request.from_string(_test_req)
     fieldStorage = req.POST.get('bar')
     self.assertTrue(isinstance(fieldStorage, cgi.FieldStorage))
     self.assertEqual(fieldStorage.type, 'application/octet-stream')
     # Double read.
     fieldStorage = req.POST.get('bar')
     self.assertTrue(isinstance(fieldStorage, cgi.FieldStorage))
     self.assertEqual(fieldStorage.type, 'application/octet-stream')
     # Now read the body.
     x = req.body
     fieldStorage = req.POST.get('bar')
     self.assertTrue(isinstance(fieldStorage, cgi.FieldStorage))
     self.assertEqual(fieldStorage.type, 'application/octet-stream')
 def test_issue_5118(self):
     """Unable to read POST variables ONCE self.request.body is read."""
     if not test_base.check_webob_version(1.0):
         return
     import cgi
     req = webapp2.Request.from_string(_test_req)
     fieldStorage = req.POST.get('bar')
     self.assertTrue(isinstance(fieldStorage, cgi.FieldStorage))
     self.assertEqual(fieldStorage.type, 'application/octet-stream')
     # Double read.
     fieldStorage = req.POST.get('bar')
     self.assertTrue(isinstance(fieldStorage, cgi.FieldStorage))
     self.assertEqual(fieldStorage.type, 'application/octet-stream')
     # Now read the body.
     x = req.body
     fieldStorage = req.POST.get('bar')
     self.assertTrue(isinstance(fieldStorage, cgi.FieldStorage))
     self.assertEqual(fieldStorage.type, 'application/octet-stream')
    def test_get_with_FieldStorage(self):
        if not test_base.check_webob_version(1.0):
            return
        # A valid request without a Content-Length header should still read
        # the full body.
        # Also test parity between as_string and from_string / from_file.
        import cgi
        req = webapp2.Request.from_string(_test_req)
        self.assertTrue(isinstance(req, webapp2.Request))
        self.assertTrue(not repr(req).endswith('(invalid WSGI environ)>'))
        self.assertTrue('\n' not in req.http_version
                        or '\r' in req.http_version)
        self.assertTrue(',' not in req.host)
        self.assertTrue(req.content_length is not None)
        self.assertEqual(req.content_length, 337)
        self.assertTrue('foo' in req.body)
        bar_contents = "these are the contents of the file 'bar.txt'\r\n"
        self.assertTrue(bar_contents in req.body)
        self.assertEqual(req.params['foo'], 'foo')
        bar = req.params['bar']
        self.assertTrue(isinstance(bar, cgi.FieldStorage))
        self.assertEqual(bar.type, 'application/octet-stream')
        bar.file.seek(0)
        self.assertEqual(bar.file.read(), bar_contents)

        bar = req.get_all('bar')
        self.assertEqual(bar[0], bar_contents)

        # out should equal contents, except for the Content-Length header,
        # so insert that.
        _test_req_copy = _test_req.replace(
            'Content-Type', 'Content-Length: 337\r\nContent-Type')
        self.assertEqual(str(req), _test_req_copy)

        req2 = webapp2.Request.from_string(_test_req2)
        self.assertTrue('host' not in req2.headers)
        self.assertEqual(str(req2), _test_req2.rstrip())
        self.assertRaises(ValueError, webapp2.Request.from_string,
                          _test_req2 + 'xx')
    def test_get_with_FieldStorage(self):
        if not test_base.check_webob_version(1.0):
            return
        # A valid request without a Content-Length header should still read
        # the full body.
        # Also test parity between as_string and from_string / from_file.
        import cgi
        req = webapp2.Request.from_string(_test_req)
        self.assertTrue(isinstance(req, webapp2.Request))
        self.assertTrue(not repr(req).endswith('(invalid WSGI environ)>'))
        self.assertTrue('\n' not in req.http_version or '\r' in req.http_version)
        self.assertTrue(',' not in req.host)
        self.assertTrue(req.content_length is not None)
        self.assertEqual(req.content_length, 337)
        self.assertTrue('foo' in req.body)
        bar_contents = "these are the contents of the file 'bar.txt'\r\n"
        self.assertTrue(bar_contents in req.body)
        self.assertEqual(req.params['foo'], 'foo')
        bar = req.params['bar']
        self.assertTrue(isinstance(bar, cgi.FieldStorage))
        self.assertEqual(bar.type, 'application/octet-stream')
        bar.file.seek(0)
        self.assertEqual(bar.file.read(), bar_contents)

        bar = req.get_all('bar')
        self.assertEqual(bar[0], bar_contents)

        # out should equal contents, except for the Content-Length header,
        # so insert that.
        _test_req_copy = _test_req.replace('Content-Type',
                            'Content-Length: 337\r\nContent-Type')
        self.assertEqual(str(req), _test_req_copy)

        req2 = webapp2.Request.from_string(_test_req2)
        self.assertTrue('host' not in req2.headers)
        self.assertEqual(str(req2), _test_req2.rstrip())
        self.assertRaises(ValueError,
                          webapp2.Request.from_string, _test_req2 + 'xx')