Пример #1
0
def test_find_path():
  """
  Find a handler matching a path.
  """

  start_ln = len(ROUTES['GET'])

  @get('/s/foo/{u_id}')
  def foo_route(request):
    return 200, ''

  fn, params = find_path('GET', '/s/foo/a1')

  assert fn == foo_route
  assert params == {'u_id': 'a1'}

  path = re.compile('^/s/foo/(?P<u_id>[\w\-]+)/?$')
  ROUTES['GET'].remove((path, fn))
  assert len(ROUTES['GET']) == start_ln
Пример #2
0
def test_find_path():
    """
    Find a handler matching a path.
    """

    start_ln = len(routes.routes['GET'])

    @get('/s/foo/{u_id}')
    def foo_route(request):
        return 200, ''

    fn, params = find_path('GET', '/s/foo/a1')

    assert fn == foo_route
    assert params == {'u_id': 'a1'}

    path = re.compile('^/s/foo/(?P<u_id>[\w\-]+)/?$')
    routes.routes['GET'].remove((path, fn))
    assert len(routes.routes['GET']) == start_ln
Пример #3
0
def call_handler(request):
    """
    Given a request dictionary, call the appropriate handler.
    Return a tuple of code (str), data (dict), and cookies (list).
    """

    method = request['method']
    if method not in ('GET', 'POST', 'PUT', 'DELETE'):
        return abort(405)

    path = request['path']
    handler, parameters = find_path(method, path)
    if not handler:
        return abort(404)

    try:
        return handler(request=request, **parameters)

    except Exception:
        if config['debug']:
            return 500, format_exc()
        return abort(500)
Пример #4
0
def call_handler(environ):
    """
    Given a WSGI environment,
    call the appropriate handler.
    Return a tuple of code (str), data (dict), and cookies (list).
    """

    method = environ['REQUEST_METHOD']
    if method not in ('GET', 'POST', 'PUT', 'DELETE'):
        return abort(405)

    path = environ['SCRIPT_NAME'] + environ['PATH_INFO']
    handler, parameters = find_path(method, path)
    if not handler:
        return abort(404)

    try:
        return handler(request=construct_request(environ), **parameters)

    except Exception:
        if config['debug']:
            return 500, format_exc()
        return abort(500)