Exemple #1
0
    def test_form(self):
        fd = get_request_fd(
            'GET',
            '/foo',
            headers={'Content-Type': 'application/x-www-form-urlencoded'},
            body='foo=bar&abc=def&x=%2f%%')
        req = Request.create('app', fd, 'addr')
        form = req.form
        self.assertEqual(form,
                         MultiDict({
                             'foo': 'bar',
                             'abc': 'def',
                             'x': '/%%'
                         }))
        self.assertTrue(req.form is form)

        fd = get_request_fd(
            'GET',
            '/foo',
            headers={'Content-Type': 'application/x-www-form-urlencoded'},
            body='')
        req = Request.create('app', fd, 'addr')
        form = req.form
        self.assertEqual(form, MultiDict({}))
        self.assertTrue(req.form is form)

        fd = get_request_fd('GET',
                            '/foo',
                            headers={'Content-Type': 'application/json'},
                            body='foo=bar&abc=def&x=%2f%%')
        req = Request.create('app', fd, 'addr')
        self.assertIsNone(req.form)
Exemple #2
0
    def test_json(self):
        fd = get_request_fd('GET', '/foo', headers={
            'Content-Type': 'application/json'}, body='{"foo":"bar"}')
        req = Request.create(fd, 'addr')
        json = req.json
        self.assertEqual(json, {'foo': 'bar'})
        self.assertTrue(req.json is json)

        fd = get_request_fd('GET', '/foo', headers={
            'Content-Type': 'application/json'}, body='[1, "2"]')
        req = Request.create(fd, 'addr')
        self.assertEqual(req.json, [1, '2'])

        fd = get_request_fd('GET', '/foo', headers={
            'Content-Type': 'application/xml'}, body='[1, "2"]')
        req = Request.create(fd, 'addr')
        self.assertIsNone(req.json)
Exemple #3
0
 def test_args(self):
     fd = get_request_fd('GET', '/?foo=bar&abc=def&x=%2f%%')
     req = Request.create('app', fd, 'addr')
     self.assertEqual(req.query_string, 'foo=bar&abc=def&x=%2f%%')
     self.assertEqual(req.args,
                      MultiDict({
                          'foo': 'bar',
                          'abc': 'def',
                          'x': '/%%'
                      }))
Exemple #4
0
    def test_render_template_in_app(self):
        app = Microdot()

        @app.route('/')
        def index(req):
            return render_template('hello.utemplate.txt', name='foo')

        req = Request.create(app, get_request_fd('GET', '/'), 'addr')
        res = app.dispatch_request(req)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(list(res.body_iter()), ['Hello, ', 'foo', '!\n'])
Exemple #5
0
 def test_body(self):
     fd = get_request_fd('GET',
                         '/foo',
                         headers={
                             'Content-Type':
                             'application/x-www-form-urlencoded',
                             'Content-Length': '19'
                         },
                         body='foo=bar&abc=def&x=y')
     req = Request.create('app', fd, 'addr')
     self.assertEqual(req.body, b'foo=bar&abc=def&x=y')
     with self.assertRaises(RuntimeError):
         req.stream
Exemple #6
0
    def test_large_line(self):
        saved_max_readline = Request.max_readline
        Request.max_readline = 16

        fd = get_request_fd(
            'GET',
            '/foo',
            headers={'Content-Type': 'application/x-www-form-urlencoded'},
            body='foo=bar&abc=def&x=y')
        with self.assertRaises(ValueError):
            Request.create('app', fd, 'addr')

        Request.max_readline = saved_max_readline
Exemple #7
0
 def test_headers(self):
     fd = get_request_fd('GET', '/foo', headers={
         'Content-Type': 'application/json',
         'Cookie': 'foo=bar;abc=def',
         'Content-Length': '3'}, body='aaa')
     req = Request.create(fd, 'addr')
     self.assertEqual(req.headers, {
         'Host': 'example.com:1234',
         'Content-Type': 'application/json',
         'Cookie': 'foo=bar;abc=def',
         'Content-Length': '3'})
     self.assertEqual(req.content_type, 'application/json')
     self.assertEqual(req.cookies, {'foo': 'bar', 'abc': 'def'})
     self.assertEqual(req.content_length, 3)
     self.assertEqual(req.body, b'aaa')
Exemple #8
0
 def test_create_request(self):
     fd = get_request_fd('GET', '/foo')
     req = Request.create(fd, 'addr')
     self.assertEqual(req.client_addr, 'addr')
     self.assertEqual(req.method, 'GET')
     self.assertEqual(req.path, '/foo')
     self.assertEqual(req.http_version, '1.0')
     self.assertIsNone(req.query_string)
     self.assertEqual(req.args, {})
     self.assertEqual(req.headers, {'Host': 'example.com:1234'})
     self.assertEqual(req.cookies, {})
     self.assertEqual(req.content_length, 0)
     self.assertEqual(req.content_type, None)
     self.assertEqual(req.body, b'')
     self.assertEqual(req.json, None)
     self.assertEqual(req.form, None)
Exemple #9
0
    def test_large_payload(self):
        saved_max_content_length = Request.max_content_length
        saved_max_body_length = Request.max_body_length
        Request.max_content_length = 32
        Request.max_body_length = 16

        fd = get_request_fd(
            'GET',
            '/foo',
            headers={'Content-Type': 'application/x-www-form-urlencoded'},
            body='foo=bar&abc=def&x=y')
        req = Request.create('app', fd, 'addr')
        self.assertEqual(req.body, b'')
        self.assertEqual(req.stream.read(), b'foo=bar&abc=def&x=y')

        Request.max_content_length = saved_max_content_length
        Request.max_body_length = saved_max_body_length