Exemplo n.º 1
0
def test_single_mw_basic():
    dumdum = DummyMiddleware()
    app = Application([('/', hello_world)], resources={}, middlewares=[dumdum])
    assert repr(app)
    assert dumdum in app.middlewares
    assert dumdum in app.routes[0].middlewares

    resp = app.get_local_client().get('/')
    assert resp.status_code == 200
Exemplo n.º 2
0
def test_subapplication_basic():
    dum1 = DummyMiddleware()
    dum2 = DummyMiddleware()
    no_name_app = Application([('/', hello_world)],
                              middlewares=[dum1])
    name_app = Application([('/', hello_world),
                            ('/foo', hello_world)],
                           resources={'name': 'Rajkumar'},
                           middlewares=[dum1])
    app = Application([('/', no_name_app),
                       ('/beta/', name_app)],
                      resources={'name': 'Kurt'},
                      middlewares=[dum2])

    assert len(app.routes) == 3

    merged_name_app_patts = [r.pattern for r in app.routes
                             if r.pattern.startswith('/beta')]
    name_app_patts = [r.pattern for r in name_app.routes]

    def check_patts(app_patts, subapp_patts):
        return all(a.endswith(s) for a, s in zip(app_patts, subapp_patts))

    # should be the same order as name_app
    assert len(merged_name_app_patts) == len(name_app_patts)
    assert check_patts(merged_name_app_patts, name_app_patts)

    assert len(set([r.pattern for r in app.routes])) == 3
    assert len(app.routes[0]._middlewares) == 1  # middleware merging

    resp = no_name_app.get_local_client().get('/')
    assert resp.data == b'Hello, world!'
    resp = name_app.get_local_client().get('/')
    assert resp.data == b'Hello, Rajkumar!'

    app_client = app.get_local_client()
    resp = app_client.get('/')
    assert resp.data == b'Hello, Kurt!'
    resp = app_client.get('/beta/')
    assert resp.data == b'Hello, Kurt!'
    resp = app_client.get('/beta/foo')
    assert resp.data == b'Hello, Kurt!'
    resp = app_client.get('/larp4lyfe/')
    assert resp.status_code == 404
Exemplo n.º 3
0
def test_single_mw_basic():
    dumdum = DummyMiddleware()
    app = Application([('/', hello_world)],
                      resources={},
                      middlewares=[dumdum])
    assert dumdum in app.middlewares
    assert dumdum in app.routes[0]._middlewares

    resp = app.get_local_client().get('/')
    assert resp.status_code == 200
Exemplo n.º 4
0
def test_duplicate_noarg_mw():
    for mw_count in range(0, 100, 20):
        mw = [DummyMiddleware() for i in range(mw_count)]
        app = Application([('/', hello_world)], middlewares=mw)
        assert app
        assert len(app.routes[0].middlewares) == mw_count

        resp = app.get_local_client().get('/')
        assert resp.status_code == 200
    return
Exemplo n.º 5
0
def test_subapplication_basic():
    dum1 = DummyMiddleware()
    dum2 = DummyMiddleware()
    no_name_app = Application([('/', hello_world)], middlewares=[dum1])
    name_app = Application([('/', hello_world), ('/foo', hello_world)],
                           resources={'name': 'Rajkumar'},
                           middlewares=[dum1])
    app = Application([('/', no_name_app), ('/beta/', name_app)],
                      resources={'name': 'Kurt'},
                      middlewares=[dum2])

    assert len(app.routes) == 3

    merged_name_app_patts = [
        r.pattern for r in app.routes if r.pattern.startswith('/beta')
    ]
    name_app_patts = [r.pattern for r in name_app.routes]

    def check_patts(app_patts, subapp_patts):
        return all(a.endswith(s) for a, s in zip(app_patts, subapp_patts))

    # should be the same order as name_app
    assert len(merged_name_app_patts) == len(name_app_patts)
    assert check_patts(merged_name_app_patts, name_app_patts)

    assert len(set([r.pattern for r in app.routes])) == 3
    assert len(app.routes[0].middlewares) == 1  # middleware merging

    resp = no_name_app.get_local_client().get('/')
    assert resp.data == b'Hello, world!'
    resp = name_app.get_local_client().get('/')
    assert resp.data == b'Hello, Rajkumar!'

    app_client = app.get_local_client()
    resp = app_client.get('/')
    assert resp.data == b'Hello, Kurt!'
    resp = app_client.get('/beta/')
    assert resp.data == b'Hello, Kurt!'
    resp = app_client.get('/beta/foo')
    assert resp.data == b'Hello, Kurt!'
    resp = app_client.get('/larp4lyfe/')
    assert resp.status_code == 404
Exemplo n.º 6
0
def test_duplicate_noarg_mw():
    for mw_count in range(0, 100, 20):
        mw = [DummyMiddleware() for i in range(mw_count)]
        app = Application([('/', hello_world)],
                          middlewares=mw)
        assert app
        assert len(app.routes[0]._middlewares) == mw_count

        resp = app.get_local_client().get('/')
        assert resp.status_code == 200
    return