package server import "testing" func TestLastPathSegment(t *testing.T) { cases := []struct { path string want string ok bool }{ {"/4H7K9QM2XR3T", "4H7K9QM2XR3T", true}, {"/account/login/4H7K9QM2XR3T", "4H7K9QM2XR3T", true}, // a trailing slash is added by some mail clients and previews {"/account/4H7K9QM2XR3T/", "4H7K9QM2XR3T", true}, {"/logo.png", "logo.png", true}, {"/", "", false}, {"", "", false}, // traversal and encoded slashes are refused rather than cleaned {"/a/../b", "", false}, {"/a%2Fb", "", false}, {"/a%2fb", "", false}, } for _, c := range cases { got, ok := LastPathSegment(c.path) if ok != c.ok || got != c.want { t.Errorf("LastPathSegment(%q) = %q,%v want %q,%v", c.path, got, ok, c.want, c.ok) } } } func TestTrimLastPathSegment(t *testing.T) { // a consumed code has to come back out of the path before the request is // forwarded, or url rewrite rules that compare the path exactly stop matching cases := []struct { path string want string }{ {"/signin/4H7K9QM2XR3T", "/signin"}, {"/a/b/4H7K9QM2XR3T", "/a/b"}, {"/4H7K9QM2XR3T", "/"}, {"/signin/4H7K9QM2XR3T/", "/signin"}, {"/", "/"}, } for _, c := range cases { if got := TrimLastPathSegment(c.path); got != c.want { t.Errorf("TrimLastPathSegment(%q) = %q, want %q", c.path, got, c.want) } } }