Пример #1
0
    def test_handler(self):
        req, res = Request(env), Response(start_res)
        assert req.next.pop(0) == '/'
        result = u('/').all()(
            lambda this, req, res: res.push("Test.").ok())._Microwave__handler(
                req, res)

        assert isinstance(result, Result)
        assert result.ok()

        env['PATH_INFO'] = '/posts/1'
        req, res = Request(env), Response(start_res)
        assert req.next.pop(0) == '/'
        result = u('/').append(u('posts/'),
                               u(':id'))(lambda this, req, res: res.push(
                                   req.rest('id')).ok())._Microwave__handler(
                                       req, res)
        assert result.ok() == [b'1']

        env['PATH_INFO'] = '/file/img/test.png'
        req, res = Request(env), Response(start_res)
        assert req.next.pop(0) == '/'
        result = u('/').append(u('file/'), u('img/'),
                               u(':!png'))(lambda this, req, res: res.push(
                                   req.rest('png')).ok())._Microwave__handler(
                                       req, res)
        assert result.ok() == [b'test.png']

        env['PATH_INFO'] = '/error'
        req, res = Request(env), Response(start_res)
        assert req.next.pop(0) == '/'
        result = u('/').err(500)(lambda this, req, res, err: res.push(err).ok(
        )).all()(lambda this, req, res: res.status(500).err("Test")
                 )._Microwave__handler(req, res)
        assert result.ok() == [b'Test']
Пример #2
0
    def test_query(self):
        req = Request(env)
        assert req.query('foo') is None

        req = Request(dict(env, **{
            'QUERY_STRING': "foo=one&foo=two&foo[foo]=three"
        }))
        assert req.query('foo') == 'one'
        assert req.query('foo[foo]') == 'three'
Пример #3
0
    def test_body(self):
        req = Request(env)
        assert req.body.tell() is 0
        assert isinstance(req.body.read(), bytes)

        req = Request(dict(env, **{'wsgi.input': BytesIO(b"bar=baz")}))
        assert req.body.tell() is 0
        assert req.body.read() == b'bar=baz'
        assert req.env['wsgi.input'].tell() == 7
        assert req.body.tell() is 0
Пример #4
0
    def test_from(self):
        req = Request(env)
        assert req.form('foo') is None

        req = Request(dict(env, **{
            'REQUEST_METHOD': "POST",
            'CONTENT_TYPE': "application/x-www-form-urlencoded",
            'CONTENT_LENGTH': "30",
            'wsgi.input': BytesIO(b"foo=one&foo=two&foo[foo]=three")
        }))
        assert req.form('foo') == 'one'
        assert req.form('foo[foo]') == 'three'

        req = Request(dict(env, **{
            'REQUEST_METHOD': "POST",
            'CONTENT_TYPE':
                "multipart/form-data; "
                "boundary=----WebKitFormBoundaryhvj9Daa5OwrBBWG9",
            'CONTENT_LENGTH': "382",
            'wsgi.input': BytesIO(
                b'------WebKitFormBoundaryhvj9Daa5OwrBBWG9\r\n'
                b'Content-Disposition: '
                b'form-data; name="foo"; filename="test.txt"\r\n'
                b'Content-Type: text/plain\r\n\r\nHi\n\r\n'
                b'------WebKitFormBoundaryhvj9Daa5OwrBBWG9\r\n'
                b'Content-Disposition: form-data; name="bar"\r\n\r\nbaz\r\n'
                b'------WebKitFormBoundaryhvj9Daa5OwrBBWG9\r\n'
                b'Content-Disposition: form-data; '
                b'name="submit"\r\n\r\nUpload Image\r\n'
                b'------WebKitFormBoundaryhvj9Daa5OwrBBWG9--\r\n'
            )
        }))
        assert req.form('bar') == 'baz'
        assert req.form('foo') == b'Hi\n'
Пример #5
0
    def test_query(self):
        req = Request(env)
        assert req.query('foo') is None

        req = Request(
            dict(env, **{'QUERY_STRING': "foo=one&foo=two&foo[foo]=three"}))
        assert req.query('foo') == 'one'
        assert req.query('foo[foo]') == 'three'
Пример #6
0
    def test_rest(self):
        req = Request(env)
        assert req.rest('foo') is None

        req._rest['foo'] = 'oof'
        assert req.rest('foo') == 'oof'

        req._rest['中文'] = r'%E6%B5%8B%E8%AF%95'
        assert req.rest('中文') == '测试'
Пример #7
0
    def test_from(self):
        req = Request(env)
        assert req.form('foo') is None

        req = Request(
            dict(
                env, **{
                    'REQUEST_METHOD': "POST",
                    'CONTENT_TYPE': "application/x-www-form-urlencoded",
                    'CONTENT_LENGTH': "30",
                    'wsgi.input': BytesIO(b"foo=one&foo=two&foo[foo]=three")
                }))
        assert req.form('foo') == 'one'
        assert req.form('foo[foo]') == 'three'

        req = Request(
            dict(
                env, **{
                    'REQUEST_METHOD':
                    "POST",
                    'CONTENT_TYPE':
                    "multipart/form-data; "
                    "boundary=----WebKitFormBoundaryhvj9Daa5OwrBBWG9",
                    'CONTENT_LENGTH':
                    "382",
                    'wsgi.input':
                    BytesIO(
                        b'------WebKitFormBoundaryhvj9Daa5OwrBBWG9\r\n'
                        b'Content-Disposition: '
                        b'form-data; name="foo"; filename="test.txt"\r\n'
                        b'Content-Type: text/plain\r\n\r\nHi\n\r\n'
                        b'------WebKitFormBoundaryhvj9Daa5OwrBBWG9\r\n'
                        b'Content-Disposition: form-data; name="bar"\r\n\r\nbaz\r\n'
                        b'------WebKitFormBoundaryhvj9Daa5OwrBBWG9\r\n'
                        b'Content-Disposition: form-data; '
                        b'name="submit"\r\n\r\nUpload Image\r\n'
                        b'------WebKitFormBoundaryhvj9Daa5OwrBBWG9--\r\n')
                }))
        assert req.form('bar') == 'baz'
        assert req.form('foo') == b'Hi\n'
Пример #8
0
    def test_handler(self):
        req, res = Request(env), Response(start_res)
        assert req.next.pop(0) == "/"
        result = u("/").all()(lambda this, req, res: res.push("Test.").ok())._Microwave__handler(req, res)

        assert isinstance(result, Result)
        assert result.ok()

        env["PATH_INFO"] = "/posts/1"
        req, res = Request(env), Response(start_res)
        assert req.next.pop(0) == "/"
        result = (
            u("/")
            .append(u("posts/"), u(":id"))(lambda this, req, res: res.push(req.rest("id")).ok())
            ._Microwave__handler(req, res)
        )
        assert result.ok() == [b"1"]

        env["PATH_INFO"] = "/file/img/test.png"
        req, res = Request(env), Response(start_res)
        assert req.next.pop(0) == "/"
        result = (
            u("/")
            .append(u("file/"), u("img/"), u(":!png"))(lambda this, req, res: res.push(req.rest("png")).ok())
            ._Microwave__handler(req, res)
        )
        assert result.ok() == [b"test.png"]

        env["PATH_INFO"] = "/error"
        req, res = Request(env), Response(start_res)
        assert req.next.pop(0) == "/"
        result = (
            u("/")
            .err(500)(lambda this, req, res, err: res.push(err).ok())
            .all()(lambda this, req, res: res.status(500).err("Test"))
            ._Microwave__handler(req, res)
        )
        assert result.ok() == [b"Test"]
Пример #9
0
    def test_rest(self):
        req = Request(env)
        assert req.rest('foo') is None

        req._rest['foo'] = 'oof'
        assert req.rest('foo') == 'oof'

        req._rest['中文'] = r'%E6%B5%8B%E8%AF%95'
        assert req.rest('中文') == '测试'
Пример #10
0
 def test_header(self):
     req = Request(env)
     assert req.header('User-Agent') == 'Mozilla'
     assert req.header('Remote-Addr') == '127.0.0.1'
Пример #11
0
 def test_header(self):
     req = Request(env)
     assert req.header('User-Agent') == 'Mozilla'
     assert req.header('Remote-Addr') == '127.0.0.1'