Example #1
0
def test_get():
    app = XWeb()

    @app.get('/get')
    def handler():
        return 'OK'

    client = webtest.TestApp(app)
    resp = client.get('/get')
    assert resp.text == 'OK'
    assert resp.status_int == 200

    resp = client.post('/get', expect_errors=True)
    assert resp.status_int == 405
Example #2
0
def test_slash():
    app = XWeb()

    @app.get('/get')
    def handler():
        return 'OK'

    client = webtest.TestApp(app)

    resp = client.get('/get')
    assert resp.text == 'OK'

    resp = client.get('/get/')
    assert resp.text == 'OK'
Example #3
0
def test_exception():
    app = XWeb()

    @app.get('/exception')
    def post():
        abort(500)
        return "OK"

    @app.exception(500)
    def exception():
        response.body = "FAIL"

    client = webtest.TestApp(app)
    resp = client.get('/exception', expect_errors=True)
    assert resp.text == "FAIL"
Example #4
0
def test_middleware_override_response():
    app = XWeb()

    @app.middleware('response')
    def process_response():
        return 'OK'

    @app.route('/hello')
    def hello_route():
        return 'FAIL'

    client = webtest.TestApp(app)

    resp = client.get('/hello')
    assert resp.text == 'OK'
Example #5
0
def test_dynamic_route():
    app = XWeb()

    results = []

    @app.route('/folder/:name/:age')
    def handler(name, age):
        results.append(name)
        results.append(age)
        return 'OK'

    client = webtest.TestApp(app)
    resp = client.get('/folder/test123/23')
    assert resp.text == 'OK'
    assert results[0] == 'test123'
    assert results[1] == '23'
Example #6
0
def test_json():
    app = XWeb()

    @app.route('/')
    def handler():
        return {"test": True}

    client = webtest.TestApp(app)
    resp = client.get('/')
    try:
        results = json.loads(resp.text)
    except:
        raise ValueError(
            "Expected JSON response but got '{}'".format(response))

    assert results.get('test') == True
Example #7
0
def test_post_form_multipart_form_data():
    app = XWeb()

    @app.route('/', methods=['POST'])
    def handler():
        return request.forms

    payload = '------xweb\r\n' \
              'Content-Disposition: form-data; name="test"\r\n' \
              '\r\n' \
              'OK\r\n' \
              '------xweb--\r\n'
    headers = {'content-type': 'multipart/form-data; boundary=----xweb'}
    client = webtest.TestApp(app)
    resp = client.post('/', params=payload, headers=headers)
    assert resp.json.get('test') == 'OK'
Example #8
0
def test_static_routes():
    app = XWeb()

    @app.route('/test')
    def handler1():
        return 'OK1'

    @app.route('/pizazz')
    def handler2():
        return 'OK2'

    client = webtest.TestApp(app)
    resp = client.get('/test')
    assert resp.text == 'OK1'

    resp = client.get('/pizazz')
    assert resp.text == 'OK2'
Example #9
0
def test_404_exception():
    app = XWeb()

    @app.get('/')
    def request_abort():
        return "OK"

    @app.exception(404)
    def exception():
        response.body = "FAIL"

    client = webtest.TestApp(app)
    resp = client.get('/')
    assert resp.text == "OK"

    resp = client.get('/no', expect_errors=True)
    assert resp.text == "FAIL"
Example #10
0
def test_middleware_request():
    app = XWeb()
    results = []

    @app.middleware('request')
    def handler():
        results.append(request)

    @app.route('/hello')
    def hello_route():
        return 'OK'

    client = webtest.TestApp(app)

    resp = client.get('/hello')
    assert resp.text == 'OK'
    assert results[0] is request
Example #11
0
def test_middleware_exception():
    app = XWeb()

    @app.middleware('request')
    def request_abort():
        abort(405)
        return "OK"

    @app.get('/')
    def request_abort():
        return "OK"

    @app.exception(405)
    def exception():
        response.body = "FAIL"

    client = webtest.TestApp(app)
    resp = client.get('/', expect_errors=True)
    assert resp.text == "FAIL"
Example #12
0
def test_route_duplicate():
    app = XWeb()

    with pytest.raises(RouteError):
        @app.route('/test')
        def handler1():
            pass

        @app.route('/test')
        def handler2():
            pass

    with pytest.raises(RouteError):
        @app.route('/test/:dynamic/')
        def handler1():
            pass

        @app.route('/test/:dynamic/')
        def handler2():
            pass
Example #13
0
def test_blueprint():
    from xweb.application import XWeb
    from xweb.blueprint import Blueprint

    user = Blueprint('/users/')

    @user.route('/')
    def user_list():
        return 'user'

    app = XWeb()

    @app.route('/')
    def hello():
        return 'app'

    app.register_blueprint(user)

    client = webtest.TestApp(app)
    assert client.get('/').text == 'app'
    assert client.get('/users/').text == 'user'
Example #14
0
def test_middleware_order():
    app = XWeb()

    order = []

    @app.middleware('request')
    def request1():
        order.append(1)

    @app.middleware('request')
    def request2():
        order.append(2)

    @app.middleware('request')
    def request3():
        order.append(3)

    @app.middleware('response')
    def response1():
        order.append(4)

    @app.middleware('response')
    def response2():
        order.append(5)

    @app.middleware('response')
    def response3():
        order.append(6)

    @app.route('/hello')
    def handler():
        return 'OK'

    client = webtest.TestApp(app)
    resp = client.get('/hello')

    assert resp.status_int == 200
    assert order == [1, 2, 3, 4, 5, 6]
Example #15
0
from user import user
from xweb.application import XWeb

app = XWeb()


@app.route('/')
def hello():
    return 'hello world!'


app.register_blueprint(user)
app.listen(3000)