-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodec_test.go
124 lines (89 loc) · 3.24 KB
/
codec_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
package nuggan
import (
"testing"
)
// ---
var config1 = Config{
GroupedBaseUrls: [][]HttpUrl{
{
"https://upload.wikimedia.org/wikipedia/commons",
},
{
"https://cdn0.iconfinder.com/data/icons",
"https://cdn1.iconfinder.com/data/icons",
},
},
}
// --- Encode tests
var encode1 = EncodeMediaUrl(config1)
func TestEncodeMediaUrlInGroup1(t *testing.T) {
got := encode1("https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Ansberg_Veitskapelle_1060027-PSD.jpg/1000px-Ansberg_Veitskapelle_1060027-PSD.jpg")
expected := "_0_L3RodW1iL2EvYTAvQW5zYmVyZ19WZWl0c2thcGVsbGVfMTA2MDAyNy1QU0QuanBnLzEwMDBweC1BbnNiZXJnX1ZlaXRza2FwZWxsZV8xMDYwMDI3LVBTRC5qcGc="
if got != expected {
t.Errorf("%s != %s\n", got, expected)
}
}
func TestEncodeMediaUrlInGroup2(t *testing.T) {
expected := "_1_L29jdGljb25zLzEwMjQvbWFyay1naXRodWItNTEyLnBuZw=="
got1 := encode1("https://cdn0.iconfinder.com/data/icons/octicons/1024/mark-github-512.png")
if got1 != expected {
t.Errorf("%s != %s\n", got1, expected)
}
got2 := encode1("https://cdn1.iconfinder.com/data/icons/octicons/1024/mark-github-512.png")
if got2 != expected {
t.Errorf("%s != %s\n", got2, expected)
}
}
func TestEncodeMediaUrlNoGroup(t *testing.T) {
got := encode1("https://blog.golang.org/lib/godoc/images/go-logo-blue.svg")
expected := "aHR0cHM6Ly9ibG9nLmdvbGFuZy5vcmcvbGliL2dvZG9jL2ltYWdlcy9nby1sb2dvLWJsdWUuc3Zn"
if got != expected {
t.Errorf("%s != %s\n", got, expected)
}
}
// --- Decode tests
var decode1 = DecodeMediaUrl(config1)
func TestDecodeMediaUrlInGroup1(t *testing.T) {
expected := "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Ansberg_Veitskapelle_1060027-PSD.jpg/1000px-Ansberg_Veitskapelle_1060027-PSD.jpg"
got, err := decode1("_0_L3RodW1iL2EvYTAvQW5zYmVyZ19WZWl0c2thcGVsbGVfMTA2MDAyNy1QU0QuanBnLzEwMDBweC1BbnNiZXJnX1ZlaXRza2FwZWxsZV8xMDYwMDI3LVBTRC5qcGc=")
if err != nil {
t.Error(err.Error())
}
if got != expected {
t.Errorf("%s != %s\n", got, expected)
}
}
func TestDecodeMediaUrlInGroup2(t *testing.T) {
expected := "https://cdn0.iconfinder.com/data/icons/octicons/1024/mark-github-512.png"
got, err := decode1("_1_L29jdGljb25zLzEwMjQvbWFyay1naXRodWItNTEyLnBuZw==")
if err != nil {
t.Error(err.Error())
}
if got != expected {
t.Errorf("%s != %s\n", got, expected)
}
}
func TestDecodeMediaUrlNoGroup(t *testing.T) {
expected := "https://blog.golang.org/lib/godoc/images/go-logo-blue.svg"
got, err := decode1("aHR0cHM6Ly9ibG9nLmdvbGFuZy5vcmcvbGliL2dvZG9jL2ltYWdlcy9nby1sb2dvLWJsdWUuc3Zn")
if err != nil {
t.Error(err.Error())
}
if got != expected {
t.Errorf("%s != %s\n", got, expected)
}
}
func TestDecodeMediaUrlMissingSeparator(t *testing.T) {
_, err := decode1("_1L29jdGljb25zLzEwMjQvbWFyay1naXRodWItNTEyLnBuZw==")
expected := "Invalid base64Ref '_1L29jdGljb25zLzEwMjQvbWFyay1naXRodWItNTEyLnBuZw==': second '_' separator expected after group index"
if err == nil || err.Error() != expected {
t.Error("Error must be raised for missing '_' separator")
}
}
func TestDecodeMediaUrlInvalidGroupIndex(t *testing.T) {
_, err := decode1("_10_L29jdGljb25zLzEwMjQvbWFyay1naXRodWItNTEyLnBuZw==")
expected := "Invalid group index: 10"
if err == nil || err.Error() != expected {
t.Error("Error must be raised for invalid group index")
}
}