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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package utils_test
import (
"net/url"
jujutesting "github.com/juju/testing"
gc "gopkg.in/check.v1"
"github.com/juju/utils"
)
type relativeURLSuite struct {
jujutesting.LoggingSuite
}
var _ = gc.Suite(&relativeURLSuite{})
var relativeURLTests = []struct {
base string
target string
expect string
expectError string
}{{
expectError: "non-absolute base URL",
}, {
base: "/foo",
expectError: "non-absolute target URL",
}, {
base: "foo",
expectError: "non-absolute base URL",
}, {
base: "/foo",
target: "foo",
expectError: "non-absolute target URL",
}, {
base: "/foo",
target: "/bar",
expect: "bar",
}, {
base: "/foo/",
target: "/bar",
expect: "../bar",
}, {
base: "/bar",
target: "/foo/",
expect: "foo/",
}, {
base: "/foo/",
target: "/bar/",
expect: "../bar/",
}, {
base: "/foo/bar",
target: "/bar/",
expect: "../bar/",
}, {
base: "/foo/bar/",
target: "/bar/",
expect: "../../bar/",
}, {
base: "/foo/bar/baz",
target: "/foo/targ",
expect: "../targ",
}, {
base: "/foo/bar/baz/frob",
target: "/foo/bar/one/two/",
expect: "../one/two/",
}, {
base: "/foo/bar/baz/",
target: "/foo/targ",
expect: "../../targ",
}, {
base: "/foo/bar/baz/frob/",
target: "/foo/bar/one/two/",
expect: "../../one/two/",
}, {
base: "/foo/bar",
target: "/foot/bar",
expect: "../foot/bar",
}, {
base: "/foo/bar/baz/frob",
target: "/foo/bar",
expect: "../../bar",
}, {
base: "/foo/bar/baz/frob/",
target: "/foo/bar",
expect: "../../../bar",
}, {
base: "/foo/bar/baz/frob/",
target: "/foo/bar/",
expect: "../../",
}, {
base: "/foo/bar/baz",
target: "/foo/bar/other",
expect: "other",
}, {
base: "/foo/bar/",
target: "/foo/bar/",
expect: ".",
}, {
base: "/foo/bar",
target: "/foo/bar",
expect: "bar",
}, {
base: "/foo/bar/",
target: "/foo/bar/",
expect: ".",
}, {
base: "/foo/bar",
target: "/foo/",
expect: ".",
}, {
base: "/foo",
target: "/",
expect: ".",
}, {
base: "/foo/",
target: "/",
expect: "../",
}, {
base: "/foo/bar",
target: "/",
expect: "../",
}, {
base: "/foo/bar/",
target: "/",
expect: "../../",
}}
func (*relativeURLSuite) TestRelativeURL(c *gc.C) {
for i, test := range relativeURLTests {
c.Logf("test %d: %q %q", i, test.base, test.target)
// Sanity check the test itself.
if test.expectError == "" {
baseURL := &url.URL{Path: test.base}
expectURL := &url.URL{Path: test.expect}
targetURL := baseURL.ResolveReference(expectURL)
c.Check(targetURL.Path, gc.Equals, test.target, gc.Commentf("resolve reference failure (%q + %q != %q)", test.base, test.expect, test.target))
}
result, err := utils.RelativeURLPath(test.base, test.target)
if test.expectError != "" {
c.Assert(err, gc.ErrorMatches, test.expectError)
c.Assert(result, gc.Equals, "")
} else {
c.Assert(err, gc.IsNil)
c.Check(result, gc.Equals, test.expect)
}
}
}
|