def _make_request(authenticated=False, **environ): """ Make a Django request from the items in the WSGI ``environ``. """ class MockDjangoUser(object): def __init__(self, authenticated): self.username = "******" self.authenticated = authenticated def is_authenticated(self): return self.authenticated request = TwodWSGIRequest(environ) request.user = MockDjangoUser(authenticated) return request
def test_setting_attributes(self): environ = { 'REQUEST_METHOD': "GET", } req = TwodWSGIRequest(environ) req.foo = "bar" ok_(hasattr(req, "foo")) eq_(req.foo, "bar") # Making sure they're in the WebOb ad-hoc attributes dict, with nothing # else: ok_("webob.adhoc_attrs" in req.environ) eq_(len(req.environ['webob.adhoc_attrs']), 1, "WebOb.Request has the following ad-hoc attributes: %s" % req.environ['webob.adhoc_attrs']) ok_("foo" in req.environ['webob.adhoc_attrs'])
def test_setting_attributes(self): environ = { 'REQUEST_METHOD': "GET", } req = TwodWSGIRequest(environ) req.foo = "bar" self.assertTrue(hasattr(req, "foo")) self.assertEqual(req.foo, "bar") # Making sure they're in the WebOb ad-hoc attributes dict, with nothing # else: self.assertIn("webob.adhoc_attrs", req.environ) self.assertEqual(len(req.environ['webob.adhoc_attrs']), 1, "WebOb.Request has the following ad-hoc attributes: %s" % req.environ['webob.adhoc_attrs']) self.assertIn("foo", req.environ['webob.adhoc_attrs'])
def test_content_length_in_post(self): """ The content length should be set if it wasn't set originally in a POST request. """ environ = { 'REQUEST_METHOD': "POST", 'CONTENT_TYPE': "application/x-www-form-urlencoded", 'wsgi.input': StringIO(urlencode({ 'foo': "bar", 'bar': "foo" })), } twod_request = TwodWSGIRequest(environ) ok_("CONTENT_LENGTH" not in twod_request.environ, "The Content-Length was set in the constructor") # The CONTENT_LENGTH shouldn't have been set after reading the POST # arguments with Django: twod_request.POST ok_("CONTENT_LENGTH" not in twod_request.environ) # But it should have been set when read with WebOb: twod_request.uPOST ok_("CONTENT_LENGTH" in twod_request.environ)
def test_setting_attributes(self): environ = { 'REQUEST_METHOD': "GET", } req = TwodWSGIRequest(environ) req.foo = "bar" ok_(hasattr(req, "foo")) eq_(req.foo, "bar") # Making sure they're in the WebOb ad-hoc attributes dict, with nothing # else: ok_("webob.adhoc_attrs" in req.environ) eq_( len(req.environ['webob.adhoc_attrs']), 1, "WebOb.Request has the following ad-hoc attributes: %s" % req.environ['webob.adhoc_attrs']) ok_("foo" in req.environ['webob.adhoc_attrs'])
def test_deleting_attributes(self): environ = { 'REQUEST_METHOD': "GET", 'webob.adhoc_attrs': { 'foo': "bar" }, } req = TwodWSGIRequest(environ) del req.foo assert_false(hasattr(req, "foo"))
def test_getting_attributes(self): environ = { 'REQUEST_METHOD': "GET", 'webob.adhoc_attrs': { 'foo': "bar" }, } req = TwodWSGIRequest(environ) ok_(hasattr(req, "foo")) eq_(req.foo, "bar")
def _make_requests(self, environ): base_environ = { 'REQUEST_METHOD': "GET", } base_environ.update(environ) requests = (WSGIRequest(base_environ.copy()), Request(base_environ.copy()), TwodWSGIRequest(base_environ.copy())) if "wsgi.input" in environ: old_input = environ['wsgi.input'] for request in requests: request.environ['wsgi.input'] = StringIO(old_input.read()) old_input.seek(0) return requests