forked from tailscale/golink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
golink_test.go
146 lines (141 loc) · 3.59 KB
/
golink_test.go
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
// 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&[email protected]",
},
{
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)
}
})
}
}