Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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]
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
def assert_missing(harness, request_path):
    result = harness.request_processor.dispatch(Path(request_path))
    assert result.status == DispatchStatus.missing
    assert result.match is None
Ejemplo n.º 9
0
def test_path_raw_is_unicode():
    path = Path("/bar.html")
    assert isinstance(path.raw, text_type)
Ejemplo n.º 10
0
def test_path_raw_is_str():
    path = Path("/bar.html")
    assert isinstance(path.raw, str)
Ejemplo n.º 11
0
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")
Ejemplo n.º 12
0
def test_path_has_parts():
    path = Path("/foo/bar.html")
    assert path.parts == ['foo', 'bar.html']
Ejemplo n.º 13
0
def test_extract_path_params_with_none():
    path = Path("/foo/bar")
    actual = _extract_params(path)
    expected = (['foo', 'bar'], [{}, {}])
    assert actual == expected
Ejemplo n.º 14
0
def test_path_unquotes_and_decodes_UTF_8():
    path = Path("/%e2%98%84.html")
    assert path.decoded == "/\u2604.html", path.decoded
Ejemplo n.º 15
0
def test_path_doesnt_unquote_plus():
    path = Path("/+%2B.html")
    assert path.decoded == "/++.html", path.decoded
Ejemplo n.º 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)
Ejemplo n.º 17
0
def test_path_decoded_is_unicode():
    path = Path("/bar.html")
    assert isinstance(path.decoded, text_type)
Ejemplo n.º 18
0
def assert_wildcards(harness, request_path, expected_vals):
    result = harness.request_processor.dispatch(Path(request_path))
    assert result.wildcards == expected_vals
Ejemplo n.º 19
0
def test_path_has_raw_set():
    path = Path("/bar.html")
    assert path.raw == "/bar.html", path.raw
Ejemplo n.º 20
0
def test_path_starts_empty():
    path = Path("/bar.html")
    assert path == {}, path
Ejemplo n.º 21
0
 def __init__(self, path):
     self.path = Path(path)
     self.root = os.path.realpath('fsfix')
Ejemplo n.º 22
0
def test_path_has_decoded_set():
    path = Path("/bar.html")
    assert path.decoded == u"/bar.html", path.decoded
Ejemplo n.º 23
0
def dispatch(harness, request_path):
    return harness.request_processor.dispatch(Path(request_path))
Ejemplo n.º 24
0
def test_path_has_decoded_set():
    path = Path("/b%c3%a4r.html")
    assert path.decoded == "/b\u00e4r.html", path.decoded