mirror of
https://github.com/wahyd4/golink.git
synced 2026-08-09 05:05:56 +10:00
84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
// Copyright 2022 Tailscale Inc & Contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package golink
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestExpandLink(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
long string
|
|
now time.Time
|
|
remainder string
|
|
want string
|
|
}{
|
|
{
|
|
name: "dont-mangle-escapes",
|
|
long: "http://host.com/foo%2f/bar",
|
|
want: "http://host.com/foo%2f/bar",
|
|
},
|
|
{
|
|
name: "dont-mangle-escapes-and-remainder",
|
|
long: "http://host.com/foo%2f/bar",
|
|
remainder: "extra",
|
|
want: "http://host.com/foo%2f/bar/extra",
|
|
},
|
|
{
|
|
name: "remainder-insert-slash",
|
|
long: "http://host.com/foo",
|
|
remainder: "extra",
|
|
want: "http://host.com/foo/extra",
|
|
},
|
|
{
|
|
name: "remainder-long-as-trailing-slash",
|
|
long: "http://host.com/foo/",
|
|
remainder: "extra",
|
|
want: "http://host.com/foo/extra",
|
|
},
|
|
{
|
|
name: "var-expansions-time",
|
|
long: `https://roamresearch.com/#/app/ts-corp/page/{{.Now.Format "01-02-2006"}}`,
|
|
want: "https://roamresearch.com/#/app/ts-corp/page/06-02-2022",
|
|
now: time.Date(2022, 06, 02, 1, 2, 3, 4, time.UTC),
|
|
},
|
|
{
|
|
name: "template-no-path",
|
|
long: "https://calendar.google.com/{{with .Path}}calendar/embed?mode=week&src={{.}}@tailscale.com{{end}}",
|
|
want: "https://calendar.google.com/",
|
|
},
|
|
{
|
|
name: "template-with-path",
|
|
long: "https://calendar.google.com/{{with .Path}}calendar/embed?mode=week&src={{.}}@tailscale.com{{end}}",
|
|
remainder: "amelie",
|
|
want: "https://calendar.google.com/calendar/embed?mode=week&src=amelie@tailscale.com",
|
|
},
|
|
{
|
|
name: "template-with-pathescape-func",
|
|
long: "http://host.com/{{QueryEscape .Path}}",
|
|
remainder: "a/b",
|
|
want: "http://host.com/a%2Fb",
|
|
},
|
|
{
|
|
name: "template-with-queryescape-func",
|
|
long: "http://host.com/{{QueryEscape .Path}}",
|
|
remainder: "a+b",
|
|
want: "http://host.com/a%2Bb",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := expandLink(tt.long, expandEnv{Now: tt.now, Path: tt.remainder})
|
|
if err != nil {
|
|
t.Fatalf("expandLink(%q): %v", tt.long, err)
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("expandLink(%q) = %q; want %q", tt.long, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|