-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_test.go
206 lines (200 loc) · 6.06 KB
/
url_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package imgproxyurl
import (
"reflect"
"testing"
)
const testKey = "e99bd6542067de7dac460558ecada3987dd2d18b066180eaa1c3abc66fb22e463d177ac8f64c93c44d0d78c35adcdda7e0b5f5a116b23ac3d1fa7a305d0727c4"
const testSalt = "a997d51b78d28ba8c05f39b6e634a044b9551352b105f70a4c0fc4c0eca5982719a33527d0253810273bf4d8b747a261cd4898d3e46916cc57d1de8aac132870"
func TestUrl_applyOptions(t *testing.T) {
type fields struct {
key []byte
salt []byte
options map[string]string
sourceUrl string
plainSourceUrl bool
format string
endpoint string
signatureSize int
}
type args struct {
options []Option
}
tests := []struct {
name string
fields fields
args args
want *Url
wantErr bool
}{
{name: "key/salt", fields: fields{}, args: args{options: []Option{Key{testKey}, Salt{testSalt}}}, want: &Url{
key: []byte{233, 155, 214, 84, 32, 103, 222, 125, 172, 70, 5, 88, 236, 173, 163, 152, 125, 210, 209, 139, 6, 97, 128, 234, 161, 195, 171, 198, 111, 178, 46, 70, 61, 23, 122, 200, 246, 76, 147, 196, 77, 13, 120, 195, 90, 220, 221, 167, 224, 181, 245, 161, 22, 178, 58, 195, 209, 250, 122, 48, 93, 7, 39, 196},
salt: []byte{169, 151, 213, 27, 120, 210, 139, 168, 192, 95, 57, 182, 230, 52, 160, 68, 185, 85, 19, 82, 177, 5, 247, 10, 76, 15, 196, 192, 236, 165, 152, 39, 25, 163, 53, 39, 208, 37, 56, 16, 39, 59, 244, 216, 183, 71, 162, 97, 205, 72, 152, 211, 228, 105, 22, 204, 87, 209, 222, 138, 172, 19, 40, 112},
}, wantErr: false},
{name: "malformed key/salt", fields: fields{}, args: args{options: []Option{Key{"key"}, Salt{"salt"}}}, want: &Url{}, wantErr: true},
{name: "options overriding", fields: fields{
options: map[string]string{
"z": "50",
"h": "100",
},
}, args: args{options: []Option{Width{200}, Height{300}}}, want: &Url{
options: map[string]string{
"z": "50",
"h": "300",
"w": "200",
},
}, wantErr: false},
{name: "raw options", fields: fields{
options: map[string]string{
"z": "50",
"h": "100",
},
}, args: args{options: []Option{Raw{OptionKey: "raw", Parameters: []interface{}{1, 2, "test"}}}}, want: &Url{
options: map[string]string{
"z": "50",
"h": "100",
"raw": "1:2:test",
},
}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &Url{
key: tt.fields.key,
salt: tt.fields.salt,
options: tt.fields.options,
sourceUrl: tt.fields.sourceUrl,
plainSourceUrl: tt.fields.plainSourceUrl,
format: tt.fields.format,
endpoint: tt.fields.endpoint,
signatureSize: tt.fields.signatureSize,
}
if err := u.applyOptions(tt.args.options...); (err != nil) != tt.wantErr {
t.Errorf("applyOptions() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(u, tt.want) {
t.Errorf("applyOptions() error: want %v, got %v", tt.want, u)
}
})
}
}
func TestUrl_clone(t *testing.T) {
type fields struct {
key []byte
salt []byte
options map[string]string
sourceUrl string
plainSourceUrl bool
format string
endpoint string
signatureSize int
}
type args struct {
addOptions []Option
}
tests := []struct {
name string
fields fields
args args
want *Url
wantErr bool
}{
{name: "clone w/o additional options", fields: fields{
key: []byte{1, 2, 3},
salt: []byte{2, 3, 4},
options: map[string]string{
"a": "100",
},
sourceUrl: "https://example.com/test.jpg",
plainSourceUrl: true,
format: "png",
endpoint: "https://example2.com/test2.png",
signatureSize: 32,
}, args: args{}, want: &Url{
key: []byte{1, 2, 3},
salt: []byte{2, 3, 4},
options: map[string]string{
"a": "100",
},
sourceUrl: "https://example.com/test.jpg",
plainSourceUrl: true,
format: "png",
endpoint: "https://example2.com/test2.png",
signatureSize: 32,
}, wantErr: false},
{name: "clone w/ additional options", fields: fields{
key: []byte{1, 2, 3},
salt: []byte{2, 3, 4},
options: map[string]string{
"a": "100",
},
sourceUrl: "https://example.com/test.jpg",
plainSourceUrl: true,
format: "png",
endpoint: "https://example2.com/test2.png",
signatureSize: 32,
}, args: args{addOptions: []Option{Width{100}, SignatureSize{31}}}, want: &Url{
key: []byte{1, 2, 3},
salt: []byte{2, 3, 4},
options: map[string]string{
"a": "100",
"w": "100",
},
sourceUrl: "https://example.com/test.jpg",
plainSourceUrl: true,
format: "png",
endpoint: "https://example2.com/test2.png",
signatureSize: 31,
}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &Url{
key: tt.fields.key,
salt: tt.fields.salt,
options: tt.fields.options,
sourceUrl: tt.fields.sourceUrl,
plainSourceUrl: tt.fields.plainSourceUrl,
format: tt.fields.format,
endpoint: tt.fields.endpoint,
signatureSize: tt.fields.signatureSize,
}
got, err := u.clone(tt.args.addOptions)
if (err != nil) != tt.wantErr {
t.Errorf("clone() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("clone() got = %v, want %v", got, tt.want)
}
})
}
}
func TestUrl_String(t *testing.T) {
tests := []struct {
name string
u *Url
want string
}{
{name: "basic options", u: func() *Url {
u, _ := New(
"local:///o/t/otRO1jl3IUVa.jpg",
Width{200},
Height{200},
Format{"png"},
PlainSourceUrl{false},
ResizingType{ResizingTypeFill},
Key{testKey},
Salt{testSalt},
Endpoint{"https://example.com/"},
)
return u
}(), want: "https://example.com/Yysx5pZ_gcWJbVQEHSp37U6r3swrZgFAygnHmbFK2VE/h:200/rt:fill/w:200/bG9jYWw6Ly8vby90L290Uk8xamwzSVVWYS5qcGc.png"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}