Skip to content

Commit 63a731c

Browse files
ANKUR DWIVEDIANKUR DWIVEDI
authored andcommitted
added test cases
1 parent 5940c2d commit 63a731c

2 files changed

Lines changed: 50 additions & 45 deletions

File tree

imagekit_test.go

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import (
44
neturl "net/url"
55
"os"
66
"reflect"
7-
"regexp"
8-
"sort"
9-
"strings"
107
"testing"
118

129
"github.com/google/go-cmp/cmp"
@@ -63,7 +60,7 @@ func TestUrl(t *testing.T) {
6360
},
6461
},
6562
},
66-
url: "https://imagekit.io/343534/tr:w-100,rt-90/default-image.jpg",
63+
url: "https://imagekit.io/343534/tr:rt-90,w-100/default-image.jpg",
6764
}, {
6865
name: "signed-url",
6966
params: ikurl.UrlParam{
@@ -76,6 +73,43 @@ func TestUrl(t *testing.T) {
7673
},
7774
url: "https://ik.imagekit.io/test/default-image.jpg?ik-t=1653775928&ik-s=48842eca663c6895331331db6c90f262c601f4e8",
7875
}, {
76+
name: "signed-url-with-transformation",
77+
params: ikurl.UrlParam{
78+
Path: "default-image.jpg",
79+
Signed: true,
80+
ExpireSeconds: 100,
81+
UnixTime: func() int64 {
82+
return 1653775828
83+
},
84+
Transformations: []map[string]any{
85+
{
86+
"height": 300,
87+
"width": 300,
88+
},
89+
},
90+
},
91+
url: "https://ik.imagekit.io/test/tr:h-300,w-300/default-image.jpg?ik-t=1653775928&ik-s=1a74eab9fca6fa0bb2298aa07f4e3892a925a508",
92+
},
93+
{
94+
name: "signed-url-with-transformation-in-query",
95+
params: ikurl.UrlParam{
96+
Path: "default-image.jpg",
97+
Signed: true,
98+
ExpireSeconds: 100,
99+
UnixTime: func() int64 {
100+
return 1653775828
101+
},
102+
TransformationPosition: "query",
103+
Transformations: []map[string]any{
104+
{
105+
"height": 300,
106+
"width": 300,
107+
},
108+
},
109+
},
110+
url: "https://ik.imagekit.io/test/default-image.jpg?tr=h-300%2Cw-300&ik-t=1653775928&ik-s=55f319d3a7db76e652545599a57af3dd94e32e24",
111+
},
112+
{
79113
name: "src-with-transformation",
80114
params: ikurl.UrlParam{
81115
Src: "https://imagekit.io/343534/default-image.jpg",
@@ -86,7 +120,7 @@ func TestUrl(t *testing.T) {
86120
},
87121
},
88122
},
89-
url: "https://imagekit.io/343534/default-image.jpg?tr=w-100,rt-90",
123+
url: "https://imagekit.io/343534/default-image.jpg?tr=rt-90%2Cw-100",
90124
}, {
91125
name: "src-without-transformation",
92126
params: ikurl.UrlParam{
@@ -106,7 +140,7 @@ func TestUrl(t *testing.T) {
106140
},
107141
TransformationPosition: ikurl.QUERY,
108142
},
109-
url: "https://imagekit.io/343534/default-image.jpg?tr=w-100%2crt-90",
143+
url: "https://imagekit.io/343534/default-image.jpg?tr=rt-90%2Cw-100",
110144
}, {
111145
name: "transformations",
112146
params: ikurl.UrlParam{
@@ -141,7 +175,7 @@ func TestUrl(t *testing.T) {
141175
},
142176
},
143177
},
144-
url: "https://ik.imagekit.io/dk1m7xkgi/tr:w-200,h-400,cm-extract,fo-center,x-100,y-110,q-85,f-auto,bl-5,dpr-auto,e-grayscale,di-test2_hBIIEweBy.gif,pr-true,lo-true,t-true,b-5_005500,cp-true,md-true,rt-auto,r-40,bg-344222,e-sharpen,e-contrast,x-1/default-image.jpg",
178+
url: "https://ik.imagekit.io/dk1m7xkgi/tr:b-5_005500,bg-344222,bl-5,cm-extract,cp-true,di-test2_hBIIEweBy.gif,dpr-auto,e-contrast,e-grayscale,e-sharpen,f-auto,fo-center,h-400,lo-true,md-true,pr-true,q-85,r-40,rt-auto,t-true,w-200,x-1,x-100,y-110/default-image.jpg",
145179
}, {
146180
name: "aspect-ratio-xc-yc",
147181
params: ikurl.UrlParam{
@@ -163,7 +197,7 @@ func TestUrl(t *testing.T) {
163197
},
164198
},
165199
},
166-
url: "https://ik.imagekit.io/test/tr:w-200,ar-16-9,cm-extract,fo-center,xc-100,yc-110,q-85,f-auto,bl-50,dpr-2,rt-90,e-sharpen-40/default-image.jpg",
200+
url: "https://ik.imagekit.io/test/tr:ar-16-9,bl-50,cm-extract,dpr-2,e-sharpen-40,f-auto,fo-center,q-85,rt-90,w-200,xc-100,yc-110/default-image.jpg",
167201
}, {
168202
name: "unsharp-mask",
169203
params: ikurl.UrlParam{
@@ -189,7 +223,7 @@ func TestUrl(t *testing.T) {
189223
},
190224
},
191225
},
192-
url: "https://ik.imagekit.io/343534/tr:w-100,h-200:rt-90/default-image.jpg",
226+
url: "https://ik.imagekit.io/343534/tr:h-200,w-100:rt-90/default-image.jpg",
193227
}, {
194228

195229
name: "common-overlay-options",
@@ -206,7 +240,7 @@ func TestUrl(t *testing.T) {
206240
},
207241
},
208242
},
209-
url: "https://ik.imagekit.io/test/tr:ox-100,oy-110,oh-100,ow-90,obg-443322,ofo-bottom/default-image.jpg",
243+
url: "https://ik.imagekit.io/test/tr:obg-443322,ofo-bottom,oh-100,ow-90,ox-100,oy-110/default-image.jpg",
210244
}, {
211245
name: "text-overlay-options",
212246
params: ikurl.UrlParam{
@@ -229,7 +263,7 @@ func TestUrl(t *testing.T) {
229263
},
230264
},
231265
},
232-
url: "https://ik.imagekit.io/test/tr:ot-this%20is%20a%20sample%20overlay,ox-100,oy-110,oh-500,ow-900,otbg-ffffff,otp-20_40,otia-right,otc-blue,otf-Arvo,ots-40,ott-ib,or-20/default-image.jpg",
266+
url: "https://ik.imagekit.io/test/tr:oh-500,or-20,ot-this%20is%20a%20sample%20overlay,otbg-ffffff,otc-blue,otf-Arvo,otia-right,otp-20_40,ots-40,ott-ib,ow-900,ox-100,oy-110/default-image.jpg",
233267
}, {
234268
name: "image-overlay-options",
235269
params: ikurl.UrlParam{
@@ -251,7 +285,7 @@ func TestUrl(t *testing.T) {
251285
},
252286
},
253287
},
254-
url: "https://ik.imagekit.io/test/tr:oi-test2_hBIIEweBy.gif,ox-100,oy-110,oh-200,ow-200,oib-4_blue,oidpr-0.2,oiq-80,oic-at_max,oix-100,oiy-20,oit-false/default-image.jpg",
288+
url: "https://ik.imagekit.io/test/tr:oh-200,oi-test2_hBIIEweBy.gif,oib-4_blue,oic-at_max,oidpr-0.2,oiq-80,oit-false,oix-100,oiy-20,ow-200,ox-100,oy-110/default-image.jpg",
255289
},
256290
}
257291

@@ -262,45 +296,14 @@ func TestUrl(t *testing.T) {
262296
if err != nil {
263297
t.Errorf(err.Error())
264298
}
265-
266-
if strings.Index(tc.url, "tr:") > -1 {
267-
url, tr := extractTransformation(t, url)
268-
expectedUrl, expectedTr := extractTransformation(t, tc.url)
269-
270-
if !urlsEquals(url, expectedUrl) {
271-
t.Errorf("expected url: %s\ngot: %s", expectedUrl, url)
272-
}
273-
274-
if !cmp.Equal(tr, expectedTr) {
275-
t.Errorf("url: %s, expected tr: %s\ngot tr: %s", tc.url, expectedTr, tr)
276-
}
299+
if !urlsEquals(url, tc.url) {
300+
t.Errorf("expected url: %s\ngot: %s", tc.url, url)
277301
}
278302
})
279303
}
280304

281305
}
282306

283-
func extractTransformation(t *testing.T, url string) (urlResult string, trResult []string) {
284-
re := regexp.MustCompile("tr:(.+)/")
285-
m := re.FindStringSubmatch(url)
286-
287-
if m == nil {
288-
t.Error("transformation not found")
289-
return
290-
}
291-
292-
urlResult = strings.Replace(url, "tr:"+m[1], "", 1)
293-
trs := strings.Split(m[1], ":")
294-
295-
for _, tr := range trs {
296-
parts := strings.Split(tr, ",")
297-
sort.Strings(parts)
298-
trResult = append(trResult, parts...)
299-
}
300-
301-
return urlResult, trResult
302-
}
303-
304307
func urlsEquals(url1 string, url2 string) bool {
305308
u1, _ := neturl.Parse(url1)
306309
u2, _ := neturl.Parse(url2)

url.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/hex"
77
"fmt"
88
neturl "net/url"
9+
"sort"
910
"strconv"
1011
"strings"
1112
"time"
@@ -148,6 +149,7 @@ func transform(tr map[string]any) string {
148149
parts = append(parts, prefix+"-"+value)
149150
}
150151
}
152+
sort.Strings(parts)
151153

152154
return strings.Join(parts, ",")
153155
}

0 commit comments

Comments
 (0)