File: _relative.lua

package info (click to toggle)
lua-uri 0.1%2B20130926%2Bgit14fa255d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 408 kB
  • ctags: 332
  • sloc: makefile: 53
file content (54 lines) | stat: -rw-r--r-- 2,110 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require "uri-test"
local URI = require "uri"

module("test.relative", lunit.testcase, package.seeall)

local function test_rel (input, userinfo, host, port, path, query, frag,
                         expected)
    local uri = assert(URI:new(input))
    assert_true(uri:is_relative())
    is("uri._relative", getmetatable(uri)._NAME)
    is(nil, uri:scheme())
    is(userinfo, uri:userinfo())
    is(host, uri:host())
    is(port, uri:port())
    is(path, uri:path())
    is(query, uri:query())
    is(frag, uri:fragment())
    if not expected then expected = input end
    is(expected, uri:uri())
    is(expected, tostring(uri))
end

function test_relative ()
    test_rel("", nil, nil, nil, "", nil, nil)
    test_rel("foo/bar", nil, nil, nil, "foo/bar", nil, nil)
    test_rel("/foo/bar", nil, nil, nil, "/foo/bar", nil, nil)
    test_rel("?query", nil, nil, nil, "", "query", nil)
    test_rel("?", nil, nil, nil, "", "", nil)
    test_rel("#foo", nil, nil, nil, "", nil, "foo")
    test_rel("#", nil, nil, nil, "", nil, "")
    test_rel("?q#f", nil, nil, nil, "", "q", "f")
    test_rel("?#", nil, nil, nil, "", "", "")
    test_rel("foo?q#f", nil, nil, nil, "foo", "q", "f")
    test_rel("//host.com", nil, "host.com", nil, "", nil, nil)
    test_rel("//host.com/blah?q#f", nil, "host.com", nil, "/blah", "q", "f")
    test_rel("//host.com:123/blah?q#f", nil, "host.com", 123, "/blah", "q", "f")
    test_rel("//u:p@host.com:123/blah?q#f",
             "u:p", "host.com", 123, "/blah", "q", "f")

    -- Paths shouldn't be normalized in a relative reference, only after it
    -- has been used to create an absolute one.
    test_rel("./foo/bar", nil, nil, nil, "./foo/bar", nil, nil)
    test_rel("././foo/./bar", nil, nil, nil, "././foo/./bar", nil, nil)
    test_rel("../foo/bar", nil, nil, nil, "../foo/bar", nil, nil)
    test_rel("../../foo/../bar", nil, nil, nil, "../../foo/../bar", nil, nil)
end

function test_bad_usage ()
    local uri = assert(URI:new("foo"))
    assert_error("set scheme on relative ref",
                 function () uri:scheme("x-foo") end)
end

-- vi:ts=4 sw=4 expandtab