mirror of
https://github.com/wahyd4/golink.git
synced 2026-08-09 05:05:56 +10:00
Specify a command line argument to resolve a link locally and exit. Also add a new flag, -resolve-from-backup, which loads a snapshot into an in-memory database and resolves the specified link.
147 lines
3.6 KiB
Go
147 lines
3.6 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",
|
|
},
|
|
{
|
|
name: "template-with-trimsuffix-func",
|
|
long: `http://host.com/{{TrimSuffix .Path "/"}}`,
|
|
remainder: "a/",
|
|
want: "http://host.com/a",
|
|
},
|
|
}
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestResolveLink(t *testing.T) {
|
|
var err error
|
|
db, err = NewSQLiteDB(":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db.Save(&Link{Short: "meet", Long: "https://meet.google.com/lookup/"})
|
|
db.Save(&Link{Short: "cs", Long: "http://codesearch/{{with .Path}}search?q={{.}}{{end}}"})
|
|
|
|
tests := []struct {
|
|
link string
|
|
want string
|
|
}{
|
|
{
|
|
link: "meet",
|
|
want: "https://meet.google.com/lookup/",
|
|
},
|
|
{
|
|
link: "meet/foo",
|
|
want: "https://meet.google.com/lookup/foo",
|
|
},
|
|
{
|
|
link: "go/meet/foo",
|
|
want: "https://meet.google.com/lookup/foo",
|
|
},
|
|
{
|
|
link: "http://go/meet/foo",
|
|
want: "https://meet.google.com/lookup/foo",
|
|
},
|
|
{
|
|
// if absolute URL provided, host doesn't actually matter
|
|
link: "http://mygo/meet/foo",
|
|
want: "https://meet.google.com/lookup/foo",
|
|
},
|
|
{
|
|
link: "cs",
|
|
want: "http://codesearch/",
|
|
},
|
|
{
|
|
link: "cs/term",
|
|
want: "http://codesearch/search?q=term",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
name := "golink " + tt.link
|
|
t.Run(name, func(t *testing.T) {
|
|
got, err := resolveLink(tt.link)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("ResolveLink(%q) = %q; want %q", tt.link, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|