def test_json_property(self): str_data = jsonmod.dumps({'a': 1, 'b': 2}) req = Request.from_values(str_data, content_type='application/json') data = req.json assert data['a'] == 1, data req = Request.from_values(str_data, content_type='application/vnd.api+json') data = req.json assert data['a'] == 1, data req = Request.from_values(str_data, content_type='application/text') data = req.json assert data is None, data
def test_from_values_inside_context_with_new_bind(self): """ creating a request can affect rg.request """ first_req = rg.request sec_req = Request.from_values({'foo': 'bar'}, bind_to_context=True) assert rg.request is sec_req assert first_req is not sec_req
def test_from_values_inside_context(self): """ creating a request should not affect rg.request by default """ first_req = rg.request sec_req = Request.from_values({'foo': 'bar'}) assert rg.request is first_req assert first_req is not sec_req
def test_json_property_encoding(self): str_data = jsonmod.dumps({ 'a': u'\u2153' }, ensure_ascii=False).encode('utf8') req = Request.from_values(str_data, content_type='applciation/json') data = req.json assert data['a'] == u'\u2153', data['a'] assert isinstance(data['a'], six.text_type)
def test_auto_form_submit(self): # setup the request, which will bind to the app's rg.request # which should result in the form values getting submitted Request.from_values( { 'name_first': 'bob', 'txtfile': (BytesIO(b'my file contents'), 'test.txt'), 'test-form-submit-flag': 'submitted' }, bind_to_context=True) # test the form f = self.Form() assert f.is_submitted() assert f.is_valid() assert f.name_first.value == 'bob' assert f.txtfile.value.file_name == 'test.txt' assert f.txtfile.value.content_type == 'text/plain'
def test_confirm_muttable(self): req = Request.from_values( { 'foo': 'bar', 'txtfile': (BytesIO(b'my file contents'), 'test.txt'), }, path='/foo?val1=1&val2=2') assert req.path == '/foo' assert len(req.args) == 2 assert req.args['val1'] == u'1' assert req.args['val2'] == u'2' req.args['new'] = 1
def init_rg(self): rg.ident = randchars() rg.environ = self.environ # the request object binds itself to rg.request Request(self.environ) if 'beaker.session' in self.environ: rg.session = self.environ['beaker.session'] log.debug('using beaker sessions') else: rg.session = None # if set, it will be called with an unhandled exception if necessary rg.exception_handler = None
def test_json_property_encoding(self): str_data = jsonmod.dumps({'a': u'\u2153'}, ensure_ascii=False).encode('utf8') req = Request.from_values(str_data, content_type='applciation/json') data = req.json assert data['a'] == u'\u2153', data['a'] assert isinstance(data['a'], six.text_type)
def test_from_values_outside_context(self): req = Request.from_values({'foo': 'bar'}) assert req.form['foo'] == 'bar'