def assert_unindexed(harness, request_path, wildcards=None, extension=None, canonical=None):
    result = harness.request_processor.dispatch(Path(request_path))
    assert result.status == DispatchStatus.unindexed
    assert result.match == harness.fs.www.resolve(request_path) + os.path.sep
    assert result.wildcards == wildcards
    assert result.extension == extension
    assert result.canonical == canonical
def assert_match(
    harness, request_path, expected_match,
    status=DispatchStatus.okay, wildcards=None, extension=None, canonical=None
):
    result = harness.request_processor.dispatch(Path(request_path))
    assert result.status == status
    assert result.match == harness.fs.www.resolve(expected_match)
    assert result.wildcards == wildcards
    assert result.extension == extension
    assert result.canonical == canonical
def _dispatch_path_to_filesystem(website, request=None):
    """This wrapper function neutralizes some of Aspen's dispatch exceptions.

    - RedirectFromSlashless, always
    - NotFound, when it's due to an extra slash at the end of the path (i.e.
      dispatch `/foo/bar/` to `/foo/bar.spt`).
    """
    if request is None:
        return
    path = request.path
    qs = request.qs
    request_processor = website.request_processor
    try:
        return dispatch_path_to_filesystem(request_processor=request_processor,
                                           path=path,
                                           querystring=qs)
    except UnindexedDirectory:
        raise
    except NotFound:
        raw_path = getattr(path, 'raw', '')
        if len(raw_path) < 3 or raw_path[-1] != '/' or raw_path[-2] == '/':
            raise
        path = Path(raw_path[:-1])
        if '.' in path.parts[-1]:
            # Don't dispatch `/foo.html/` to a `/foo.html` file
            raise
        r = dispatch_path_to_filesystem(request_processor=request_processor,
                                        path=path,
                                        querystring=qs)
        r['path'] = request.line.uri.path = path
        request.canonical_path = raw_path
        return r
    except RedirectFromSlashless as exception:
        path = urlquote(exception.message.encode('utf8'), string.punctuation)
        path = request.line.uri.path = Path(path)
        request.canonical_path = path.raw
        r = dispatch_path_to_filesystem(request_processor=request_processor,
                                        path=path,
                                        querystring=qs)
        r['path'] = path
        return r
Beispiel #4
0
def test_extract_path_params_complex():
    path = Path("/foo;a=1;b=2,3;c;a=2;b=4/bar;a=2,ab;b=1")
    actual = _extract_params(path)
    expected = (['foo', 'bar'], [{
        'a': ['1', '2'],
        'b': ['2,3', '4'],
        'c': ['']
    }, {
        'a': ['2,ab'],
        'b': ['1']
    }])
    assert actual == expected
Beispiel #5
0
def test_extract_path_params_simple():
    path = Path("/foo;a=1;b=2;c/bar;a=2;b=1")
    actual = _extract_params(path)
    expected = (['foo', 'bar'], [{
        'a': ['1'],
        'b': ['2'],
        'c': ['']
    }, {
        'a': ['2'],
        'b': ['1']
    }])
    assert actual == expected
Beispiel #6
0
def test_path_params_api():
    path = Path("/foo;a=1;b=2;b=3;c/bar;a=2,ab;b=1")
    parts, params = (['foo', 'bar'], [{
        'a': ['1'],
        'b': ['2', '3'],
        'c': ['']
    }, {
        'a': ['2,ab'],
        'b': ['1']
    }])
    assert path.parts == parts, path.parts
    assert path.parts[0].params == params[0]
    assert path.parts[1].params == params[1]
def get_result(harness, request_uri):
    url_path = Path(request_uri)
    dispatch_result = harness.request_processor.dispatch(url_path)
    if dispatch_result.match:
        if dispatch_result.status == DispatchStatus.okay:
            result = 'ok'
        elif dispatch_result.status == DispatchStatus.unindexed:
            result = 'ui'
        else:
            result = str(dispatch_result.status)
        fspath = dispatch_result.match
        if os.sep != posixpath.sep:
            fspath = fspath.replace(os.sep, posixpath.sep)
        result += " " + (fspath[len(harness.fs.www.root) + 1:] or '/')
        wilds = url_path
        if wilds:
            wildtext = ",".join("%s='%s'" % (k, wilds[k])
                                for k in sorted(wilds))
            result += " (%s)" % wildtext
        if dispatch_result.canonical:
            result += " @" + dispatch_result.canonical
    else:
        result = '-'
    return result
def assert_missing(harness, request_path):
    result = harness.request_processor.dispatch(Path(request_path))
    assert result.status == DispatchStatus.missing
    assert result.match is None
Beispiel #9
0
def test_path_raw_is_unicode():
    path = Path("/bar.html")
    assert isinstance(path.raw, text_type)
def test_path_raw_is_str():
    path = Path("/bar.html")
    assert isinstance(path.raw, str)
def test_uri_normal_case_is_normal():
    uri = URI("/baz.html?buz=bloo")
    assert uri.path == Path("/baz.html")
    assert uri.querystring == Querystring("buz=bloo")
Beispiel #12
0
def test_path_has_parts():
    path = Path("/foo/bar.html")
    assert path.parts == ['foo', 'bar.html']
Beispiel #13
0
def test_extract_path_params_with_none():
    path = Path("/foo/bar")
    actual = _extract_params(path)
    expected = (['foo', 'bar'], [{}, {}])
    assert actual == expected
Beispiel #14
0
def test_path_unquotes_and_decodes_UTF_8():
    path = Path("/%e2%98%84.html")
    assert path.decoded == "/\u2604.html", path.decoded
Beispiel #15
0
def test_path_doesnt_unquote_plus():
    path = Path("/+%2B.html")
    assert path.decoded == "/++.html", path.decoded
Beispiel #16
0
def collapse_path_parts(request):
    collapsed = utils.collapse_path_parts(request.line.uri.path.raw)
    request.line.uri.path = request.context['path'] = Path(collapsed)
Beispiel #17
0
def test_path_decoded_is_unicode():
    path = Path("/bar.html")
    assert isinstance(path.decoded, text_type)
def assert_wildcards(harness, request_path, expected_vals):
    result = harness.request_processor.dispatch(Path(request_path))
    assert result.wildcards == expected_vals
Beispiel #19
0
def test_path_has_raw_set():
    path = Path("/bar.html")
    assert path.raw == "/bar.html", path.raw
Beispiel #20
0
def test_path_starts_empty():
    path = Path("/bar.html")
    assert path == {}, path
Beispiel #21
0
 def __init__(self, path):
     self.path = Path(path)
     self.root = os.path.realpath('fsfix')
def test_path_has_decoded_set():
    path = Path("/bar.html")
    assert path.decoded == u"/bar.html", path.decoded
def dispatch(harness, request_path):
    return harness.request_processor.dispatch(Path(request_path))
Beispiel #24
0
def test_path_has_decoded_set():
    path = Path("/b%c3%a4r.html")
    assert path.decoded == "/b\u00e4r.html", path.decoded