Ejemplo n.º 1
0
    def test_wsgi_header_values(self):
        def cmp(wire, app, **options):
            rs = BaseResponse()
            rs.set_header('x-test', app, **options)
            result = [v for (h, v) in rs.headerlist if h.lower()=='x-test'][0]
            self.assertEquals(wire, result)

        if bottle.py3k:
            cmp(tonat('1', 'latin1'), 1)
            cmp('öäü'.encode('utf8').decode('latin1'), 'öäü')
            # Dropped byte header support in Python 3:
            #cmp(äöü'.encode('utf8').decode('latin1'), tob('äöü'))

            # Options support
            cmp(tonat('1; foo=bar', 'latin1'), 1, foo='bar')
            cmp('öäü; foo="öäü"'.encode('utf8').decode('latin1'),
                'öäü', foo='öäü')
            cmp('just; get=into; robot=shinji'.encode('utf8').decode('latin1'),
                'just', get='into', robot='shinji')
        else:
            cmp('1', 1)
            cmp('öäü', 'öäü')
            cmp('äöü', touni('äöü'))

            # Options support
            cmp('1; foo=bar', 1, foo='bar')
            cmp('öäü; foo="öäü"', 'öäü', foo='öäü')
            cmp('öäü; foo="öäü"', touni('öäü'), foo=touni('öäü'))
            cmp('just; get=into; robot=shinji',
                'just', get='into', robot='shinji')
 def test_get(self):
     """ Environ: GET data """
     qs = tonat(tob('a=a&a=1&b=b&c=c&cn=%e7%93%b6'), 'latin1')
     request = BaseRequest({'QUERY_STRING':qs})
     self.assertTrue('a' in request.query)
     self.assertTrue('b' in request.query)
     self.assertEqual(['a','1'], request.query.getall('a'))
     self.assertEqual(['b'], request.query.getall('b'))
     self.assertEqual('1', request.query['a'])
     self.assertEqual('b', request.query['b'])
     self.assertEqual(tonat(tob('瓶'), 'latin1'), request.query['cn'])
     self.assertEqual(touni('瓶'), request.query.cn)
Ejemplo n.º 3
0
 def test_get(self):
     """ Environ: GET data """
     qs = tonat(tob('a=a&a=1&b=b&c=c&cn=%e7%93%b6'), 'latin1')
     request = BaseRequest({'QUERY_STRING': qs})
     self.assertTrue('a' in request.query)
     self.assertTrue('b' in request.query)
     self.assertEqual(['a', '1'], request.query.getall('a'))
     self.assertEqual(['b'], request.query.getall('b'))
     self.assertEqual('1', request.query['a'])
     self.assertEqual('b', request.query['b'])
     self.assertEqual(tonat(tob('瓶'), 'latin1'), request.query['cn'])
     self.assertEqual(touni('瓶'), request.query.cn)
Ejemplo n.º 4
0
 def test_non_string_header(self):
     response = BaseResponse()
     response['x-test'] = 5
     self.assertEqual('5', response['x-test'])
     response['x-test'] = None
     self.assertEqual('', response['x-test'])
     response['x-test'] = touni('瓶')
     self.assertEqual(tonat(touni('瓶')), response['x-test'])
Ejemplo n.º 5
0
 def test_non_string_header(self):
     response = BaseResponse()
     response['x-test'] = 5
     self.assertEqual('5', response['x-test'])
     response['x-test'] = None
     self.assertEqual('', response['x-test'])
     response['x-test'] = touni('瓶')
     self.assertEqual(tonat(touni('瓶')), response['x-test'])
Ejemplo n.º 6
0
    def error404(ex):
        """ Use json 404 if request accepts json otherwise use html"""
        if 'application/json' not in bottle.request.get_header('Accept', ""):
            bottle.response.set_header('content-type', 'text/html')
            return bottle.tonat(bottle.template(bottle.ERROR_PAGE_TEMPLATE, e=ex))

        bottle.response.set_header('content-type', 'application/json')
        return json.dumps(dict(error=ex.body))
Ejemplo n.º 7
0
        def cmp(app, wire):
            rs = BaseResponse()
            rs.set_header('x-test', app)
            result = [v for (h, v) in rs.headerlist
                      if h.lower() == 'x-test'][0]
            self.assertEqual(wire, result)

            cmp(1, tonat('1', 'latin1'))
            cmp('öäü', 'öäü'.encode('utf8').decode('latin1'))
Ejemplo n.º 8
0
    def test_wsgi_header_values(self):
        def cmp(app, wire):
            rs = BaseResponse()
            rs.set_header('x-test', app)
            result = [v for (h, v) in rs.headerlist if h.lower()=='x-test'][0]
            self.assertEqual(wire, result)

        if bottle.py3k:
            cmp(1, tonat('1', 'latin1'))
            cmp('öäü', 'öäü'.encode('utf8').decode('latin1'))
            # Dropped byte header support in Python 3:
            #cmp(tob('äöü'), 'äöü'.encode('utf8').decode('latin1'))
        else:
            cmp(1, '1')
            cmp('öäü', 'öäü')
            cmp(touni('äöü'), 'äöü')
Ejemplo n.º 9
0
    def test_wsgi_header_values(self):
        def cmp(app, wire):
            rs = BaseResponse()
            rs.set_header('x-test', app)
            result = [v for (h, v) in rs.headerlist if h.lower()=='x-test'][0]
            self.assertEqual(wire, result)

        if bottle.py3k:
            cmp(1, tonat('1', 'latin1'))
            cmp('öäü', 'öäü'.encode('utf8').decode('latin1'))
            # Dropped byte header support in Python 3:
            #cmp(tob('äöü'), 'äöü'.encode('utf8').decode('latin1'))
        else:
            cmp(1, '1')
            cmp('öäü', 'öäü')
            cmp(touni('äöü'), 'äöü')
Ejemplo n.º 10
0
def transfer_form():
    post = FormsDict()
    # We default to application/x-www-form-urlencoded for everything that
    # is not multipart and take the fast path (also: 3.1 workaround)
    if not request.content_type.startswith('multipart/'):
        pairs = _parse_qsl(tonat(request._get_body_string(), 'latin1'))
        for key, value in pairs:
            post[key] = value
        return post

    request.environ['bottle.request.post'] = post

    get = request.environ['bottle.get'] = FormsDict()
    pairs = _parse_qsl(request.environ.get('QUERY_STRING', ''))
    for key, value in pairs:
        get[key] = value
    request.environ['bottle.request.query'] = get
 def test_post(self):
     """ Environ: POST data """
     sq = tob('a=a&a=1&b=b&c=&d&cn=%e7%93%b6')
     e = {}
     wsgiref.util.setup_testing_defaults(e)
     e['wsgi.input'].write(sq)
     e['wsgi.input'].seek(0)
     e['CONTENT_LENGTH'] = str(len(sq))
     e['REQUEST_METHOD'] = "POST"
     request = BaseRequest(e)
     self.assertTrue('a' in request.POST)
     self.assertTrue('b' in request.POST)
     self.assertEqual(['a','1'], request.POST.getall('a'))
     self.assertEqual(['b'], request.POST.getall('b'))
     self.assertEqual('1', request.POST['a'])
     self.assertEqual('b', request.POST['b'])
     self.assertEqual('', request.POST['c'])
     self.assertEqual('', request.POST['d'])
     self.assertEqual(tonat(tob('瓶'), 'latin1'), request.POST['cn'])
     self.assertEqual(touni('瓶'), request.POST.cn)
Ejemplo n.º 12
0
 def test_post(self):
     """ Environ: POST data """
     sq = tob('a=a&a=1&b=b&c=&d&cn=%e7%93%b6')
     e = {}
     wsgiref.util.setup_testing_defaults(e)
     e['wsgi.input'].write(sq)
     e['wsgi.input'].seek(0)
     e['CONTENT_LENGTH'] = str(len(sq))
     e['REQUEST_METHOD'] = "POST"
     request = BaseRequest(e)
     self.assertTrue('a' in request.POST)
     self.assertTrue('b' in request.POST)
     self.assertEqual(['a', '1'], request.POST.getall('a'))
     self.assertEqual(['b'], request.POST.getall('b'))
     self.assertEqual('1', request.POST['a'])
     self.assertEqual('b', request.POST['b'])
     self.assertEqual('', request.POST['c'])
     self.assertEqual('', request.POST['d'])
     self.assertEqual(tonat(tob('瓶'), 'latin1'), request.POST['cn'])
     self.assertEqual(touni('瓶'), request.POST.cn)
Ejemplo n.º 13
0
def _hval_custom(value):
    value = bottle.tonat(value)
    if '\n' in value or '\r' in value:  # or '\0' in value:
        raise ValueError(
            'Header value must not contain control characters: %r' % value)
    return value
Ejemplo n.º 14
0
    def test_cookie_dict(self):
        """ Environ: Cookie dict """ 
        t = dict()
        t['a=a']      = {'a': 'a'}
        t['a=a; b=b'] = {'a': 'a', 'b':'b'}
        t['a=a; a=b'] = {'a': 'b'}
        for k, v in t.items():
            request = BaseRequest({'HTTP_COOKIE': k})
            for n in v:
                self.assertEqual(v[n], request.cookies[n])
                self.assertEqual(v[n], request.get_cookie(n))

    def test_get(self):
        """ Environ: GET data """ 
<<<<<<< HEAD
        qs = tonat(tob('a=a&a=1&b=b&c=c&cn=瓶'), 'latin1')
=======
        qs = tonat(tob('a=a&a=1&b=b&c=c&cn=%e7%93%b6'), 'latin1')
>>>>>>> f1a09de0816b02f26fbd0104fd5194c51f638aba
        request = BaseRequest({'QUERY_STRING':qs})
        self.assertTrue('a' in request.query)
        self.assertTrue('b' in request.query)
        self.assertEqual(['a','1'], request.query.getall('a'))
        self.assertEqual(['b'], request.query.getall('b'))
        self.assertEqual('1', request.query['a'])
        self.assertEqual('b', request.query['b'])
        self.assertEqual(tonat(tob('瓶'), 'latin1'), request.query['cn'])
        self.assertEqual(touni('瓶'), request.query.cn)
        
    def test_post(self):
        """ Environ: POST data """