| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package fileserver |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | ) |
| 20 | |
| 21 | func TestBreadcrumbs(t *testing.T) { |
| 22 | testdata := []struct { |
| 23 | path string |
| 24 | expected []crumb |
| 25 | }{ |
| 26 | {"", []crumb{}}, |
| 27 | {"/", []crumb{{Text: "/"}}}, |
| 28 | {"/foo/", []crumb{ |
| 29 | {Link: "../", Text: "/"}, |
| 30 | {Link: "", Text: "foo"}, |
| 31 | }}, |
| 32 | {"/foo/bar/", []crumb{ |
| 33 | {Link: "../../", Text: "/"}, |
| 34 | {Link: "../", Text: "foo"}, |
| 35 | {Link: "", Text: "bar"}, |
| 36 | }}, |
| 37 | {"/foo bar/", []crumb{ |
| 38 | {Link: "../", Text: "/"}, |
| 39 | {Link: "", Text: "foo bar"}, |
| 40 | }}, |
| 41 | {"/foo bar/baz/", []crumb{ |
| 42 | {Link: "../../", Text: "/"}, |
| 43 | {Link: "../", Text: "foo bar"}, |
| 44 | {Link: "", Text: "baz"}, |
| 45 | }}, |
| 46 | {"/100%25 test coverage/is a lie/", []crumb{ |
| 47 | {Link: "../../", Text: "/"}, |
| 48 | {Link: "../", Text: "100% test coverage"}, |
| 49 | {Link: "", Text: "is a lie"}, |
| 50 | }}, |
| 51 | {"/AC%2FDC/", []crumb{ |
| 52 | {Link: "../", Text: "/"}, |
| 53 | {Link: "", Text: "AC/DC"}, |
| 54 | }}, |
| 55 | {"/foo/%2e%2e%2f/bar", []crumb{ |
| 56 | {Link: "../../../", Text: "/"}, |
| 57 | {Link: "../../", Text: "foo"}, |
| 58 | {Link: "../", Text: "../"}, |
| 59 | {Link: "", Text: "bar"}, |
| 60 | }}, |
| 61 | {"/foo/../bar", []crumb{ |
| 62 | {Link: "../../../", Text: "/"}, |
| 63 | {Link: "../../", Text: "foo"}, |
| 64 | {Link: "../", Text: ".."}, |
| 65 | {Link: "", Text: "bar"}, |
| 66 | }}, |
| 67 | {"foo/bar/baz", []crumb{ |
| 68 | {Link: "../../", Text: "foo"}, |
| 69 | {Link: "../", Text: "bar"}, |
| 70 | {Link: "", Text: "baz"}, |
| 71 | }}, |
| 72 | {"/qux/quux/corge/", []crumb{ |
| 73 | {Link: "../../../", Text: "/"}, |
| 74 | {Link: "../../", Text: "qux"}, |
| 75 | {Link: "../", Text: "quux"}, |
| 76 | {Link: "", Text: "corge"}, |
| 77 | }}, |
| 78 | {"/مجلد/", []crumb{ |
| 79 | {Link: "../", Text: "/"}, |
| 80 | {Link: "", Text: "مجلد"}, |
| 81 | }}, |
| 82 | {"/مجلد-1/مجلد-2", []crumb{ |
| 83 | {Link: "../../", Text: "/"}, |
| 84 | {Link: "../", Text: "مجلد-1"}, |
| 85 | {Link: "", Text: "مجلد-2"}, |
| 86 | }}, |
| 87 | {"/مجلد%2F1", []crumb{ |
| 88 | {Link: "../", Text: "/"}, |
| 89 | {Link: "", Text: "مجلد/1"}, |
| 90 | }}, |
| 91 | } |
| 92 | |
| 93 | for testNum, d := range testdata { |
| 94 | l := browseTemplateContext{Path: d.path} |
| 95 | actual := l.Breadcrumbs() |
| 96 | if len(actual) != len(d.expected) { |
| 97 | t.Errorf("Test %d: Got %d components but expected %d; got: %+v", testNum, len(actual), len(d.expected), actual) |
| 98 | continue |
| 99 | } |
| 100 | for i, c := range actual { |
| 101 | if c != d.expected[i] { |
| 102 | t.Errorf("Test %d crumb %d: got %#v but expected %#v at index %d", testNum, i, c, d.expected[i], i) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |