示例#1
0
def test_handler(app, client):

    @app.register(muffin.sre('/res(/{res})?/?'))
    @app.register('/res/{res}')
    class Resource(muffin.Handler):

        def get(self, request):
            return request.match_info

        def post(self, request):
            data = yield from self.parse(request)
            return dict(data)

    assert set(Resource.methods) == set(['GET', 'POST'])
    assert asyncio.iscoroutinefunction(Resource.get)

    assert 'resource-*' in app.router._routes

    response = client.delete('/res', status=405)

    response = client.get('/res')
    assert response.json == {'res': ''}

    response = client.get('/res/1')
    assert response.json == {'res': '1'}

    response = client.get('/res/2/')
    assert response.json == {'res': '2'}

    response = client.post('/res', {'data': 'form'})
    assert response.json == {'data': 'form'}

    response = client.post_json('/res', {'data': 'json'})
    assert response.json == {'data': 'json'}

    @app.register(muffin.sre('/res2(/{res2})?/?'))
    class Resource2(muffin.Handler):

        methods = 'get',

        def get(self, request):
            return 'OK'

        def put(self, request):
            raise Exception('Shouldnt be called')

    response = client.get('/res2')
    assert response.text == 'OK'

    client.put('/res2', status=405)

    @Resource2.register('/connect')
    def connect_(handler, request):
        return handler.app.name

    response = client.get('/connect')
    assert response.text == 'muffin'
示例#2
0
def test_sre():
    re = muffin.sre('/test/{id:\d+}/?')
    assert re.match('/test/1')
    assert re.match('/test/2/')

    re = muffin.sre('/test/{id:\d+}/{name}/?')
    assert re.match('/test/1/Mike')

    re = muffin.sre('/test(/{id}/?)?')
    assert re.match('/test/1')
    assert re.match('/test/1/').group('id') == '1'
示例#3
0
 def connect(cls, app, *paths, name=None):
     """ Connect to the application. """
     if not paths:
         paths = [muffin.sre('/%s(/{%s})?/?' % (cls.name, cls.name))]
     return super(RESTHandler, cls).connect(app, *paths, name=name)
示例#4
0
def test_handler(app, client):
    """Test class-based handlers.

    Actually everything in Muffin is class-based handlers.
    Because view-functions will convert to Handler classes.
    """
    from muffin.handler import register

    @app.register(muffin.sre('/res(/{res})?/?'))
    @app.register('/res/{res}')
    class Resource(muffin.Handler):

        def get(self, request):
            return request.match_info

        def post(self, request):
            data = yield from self.parse(request)
            return dict(data)

        @register('/res/lama/rama', methods=['GET', 'POST', 'PATCH'])
        def lama(self, request):
            return 'LAMA'

        @register('/res/rama/lama')
        def rama(self, request):
            return 'RAMA'

    assert set(Resource.methods) == set(['GET', 'POST'])
    assert asyncio.iscoroutinefunction(Resource.get)

    assert 'resource.any' in app.router._routes

    assert 'lama.post' in app.router
    assert 'lama.patch' in app.router

    response = client.get('/res/lama/rama')
    assert response.text == 'LAMA'

    response = client.patch('/res/lama/rama')
    assert response.text == 'LAMA'

    response = client.get('/res/rama/lama')
    assert response.text == 'RAMA'

    response = client.delete('/res', status=405)

    response = client.get('/res')
    assert response.json == {'res': ''}

    response = client.get('/res/1')
    assert response.json == {'res': '1'}

    response = client.get('/res/2/')
    assert response.json == {'res': '2'}

    response = client.post('/res', {'data': 'form'})
    assert response.json == {'data': 'form'}

    response = client.post_json('/res', {'data': 'json'})
    assert response.json == {'data': 'json'}

    @app.register(muffin.sre('/res2(/{res2})?/?'))
    class Resource2(muffin.Handler):

        methods = 'get',

        def get(self, request):
            return 'OK'

        def put(self, request):
            raise Exception('Shouldnt be called')

    response = client.get('/res2')
    assert response.text == 'OK'

    client.put('/res2', status=405)

    @Resource2.register('/connect')
    def connect_(handler, request):
        return handler.app.name

    response = client.get('/connect')
    assert response.text == 'muffin'