-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidsecret_test.go
86 lines (61 loc) · 1.53 KB
/
idsecret_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
package uri
import (
"context"
"fmt"
"net/url"
"path/filepath"
"regexp"
"testing"
)
func TestIdSecretURI(t *testing.T) {
ctx := context.Background()
candidates := []string{
"idsecret:///tmp/example.jpg?id=1234",
}
for _, str_uri := range candidates {
u, err := NewURI(ctx, str_uri)
if err != nil {
t.Fatalf("Failed to create new IIIF URI for '%s', %v", str_uri, err)
}
net_u, err := url.Parse(u.String())
if err != nil {
t.Fatalf("Failed to parse stringified IdSecreURI '%s', %v", u.String(), err)
}
net_q := net_u.Query()
id := net_q.Get("id")
secret := net_q.Get("secret")
secret_o := net_q.Get("secret_o")
if id != "1234" {
t.Fatalf("Unexpected id value for '1234': '%s'", id)
}
if secret == "" {
t.Fatalf("Missing secret value")
}
if secret_o == "" {
t.Fatalf("Missing secret_o value")
}
format := "jpg"
label := "x"
values := &url.Values{}
values.Set("format", format)
values.Set("label", label)
target, err := u.Target(values)
if err != nil {
t.Fatalf("Unable to determine target for '%s', %v", str_uri, err)
}
tree := Id2Path(id)
root := filepath.Dir(target)
fname := filepath.Base(target)
if root != tree {
t.Fatalf("Unexpected root: '%s'", root)
}
fname_pat := fmt.Sprintf("%s_([^_]+)_%s.%s", id, label, format)
fname_re, err := regexp.Compile(fname_pat)
if err != nil {
t.Fatalf("Failed to compile fname pattern, %v", err)
}
if !fname_re.MatchString(fname) {
t.Fatalf("Filename failed pattern match: '%s'", fname)
}
}
}