1
1
import test from 'ava'
2
- import { parseURL } from 'ufo'
3
- import { decodeBase64Params , doPatternsMatchUrl , RemotePattern } from '../src/utils'
2
+ import { parseURL } from 'ufo'
3
+ import { decodeBase64Params , doPatternsMatchUrl , RemotePattern } from '../src/utils'
4
4
5
- const encodeUrlAndTransforms = ( url : string = '' , transforms : string = '' , extension : string = '' ) : string => {
5
+ const encodeUrlAndTransforms = ( url = '' , transforms = '' , extension = '' ) : string => {
6
6
const encodedUrl = Buffer . from ( url ) . toString ( 'base64' )
7
7
const encodedTransforms = Buffer . from ( transforms ) . toString ( 'base64' )
8
8
return `${ encodedUrl } /${ encodedTransforms } ${ extension } `
@@ -12,9 +12,9 @@ test('decodeBase64Params: returns expected response if no url is present', (t) =
12
12
const expectedResponse = {
13
13
error : 'Bad Request'
14
14
}
15
-
15
+
16
16
const response = decodeBase64Params ( '' )
17
-
17
+
18
18
t . deepEqual ( response , expectedResponse )
19
19
} )
20
20
@@ -24,173 +24,173 @@ test('decodeBase64Params: returns expected response if no transform exists in pa
24
24
}
25
25
26
26
const encodedUrl = encodeUrlAndTransforms ( '/images/image.jpg' , '' )
27
-
27
+
28
28
const response = decodeBase64Params ( encodedUrl )
29
-
29
+
30
30
t . deepEqual ( response , expectedResponse )
31
31
} )
32
32
33
33
test ( "decodeBase64Params: returns expected response if transform doesn't contain any transforms" , ( t ) => {
34
- const expectedId = '/images/' ;
34
+ const expectedId = '/images/'
35
35
const expectedResponse = {
36
36
error : 'Bad Request'
37
37
}
38
-
38
+
39
39
const encodedUrl = encodeUrlAndTransforms ( expectedId , '' , '.ext' )
40
-
40
+
41
41
const response = decodeBase64Params ( encodedUrl )
42
-
42
+
43
43
t . deepEqual ( response , expectedResponse )
44
44
} )
45
45
46
46
test ( 'decodeBase64Params: returns expected response if transform contains w and h' , ( t ) => {
47
- const expectedId = '/images/image.jpg' ;
47
+ const expectedId = '/images/image.jpg'
48
48
const expectedResponse = {
49
49
id : expectedId ,
50
50
modifiers : 's_100x200'
51
51
}
52
52
53
53
const encodedUrl = encodeUrlAndTransforms ( expectedId , 'w=100&h=200' , '.jpg' )
54
-
54
+
55
55
const response = decodeBase64Params ( encodedUrl )
56
-
56
+
57
57
t . deepEqual ( response , expectedResponse )
58
58
} )
59
59
60
60
test ( 'decodeBase64Params: returns expected response if transform contains w but no h' , ( t ) => {
61
- const expectedId = '/images/image.jpg' ;
61
+ const expectedId = '/images/image.jpg'
62
62
const expectedResponse = {
63
63
id : expectedId ,
64
64
modifiers : 'w_100'
65
65
}
66
66
67
67
const encodedUrl = encodeUrlAndTransforms ( expectedId , 'w=100' , '.jpg' )
68
-
68
+
69
69
const response = decodeBase64Params ( encodedUrl )
70
-
70
+
71
71
t . deepEqual ( response , expectedResponse )
72
72
} )
73
73
74
74
test ( 'decodeBase64Params: returns expected response if transform contains h but no w' , ( t ) => {
75
- const expectedId = '/images/image.jpg' ;
75
+ const expectedId = '/images/image.jpg'
76
76
const expectedResponse = {
77
77
id : expectedId ,
78
78
modifiers : 'h_200'
79
79
}
80
80
81
81
const encodedUrl = encodeUrlAndTransforms ( expectedId , 'h=200' , '.jpg' )
82
-
82
+
83
83
const response = decodeBase64Params ( encodedUrl )
84
-
84
+
85
85
t . deepEqual ( response , expectedResponse )
86
86
} )
87
87
88
88
test ( 'decodeBase64Params: returns expected response if transform contains fm' , ( t ) => {
89
- const expectedId = '/images/image.jpg' ;
89
+ const expectedId = '/images/image.jpg'
90
90
const expectedResponse = {
91
91
id : expectedId ,
92
92
modifiers : 'f_10'
93
93
}
94
94
95
95
const encodedUrl = encodeUrlAndTransforms ( expectedId , 'fm=10' , '.jpg' )
96
-
96
+
97
97
const response = decodeBase64Params ( encodedUrl )
98
-
98
+
99
99
t . deepEqual ( response , expectedResponse )
100
100
} )
101
101
102
102
test ( 'decodeBase64Params: returns expected response if transform contains q' , ( t ) => {
103
- const expectedId = '/images/image.jpg' ;
103
+ const expectedId = '/images/image.jpg'
104
104
const expectedResponse = {
105
105
id : expectedId ,
106
106
modifiers : 'q_101'
107
107
}
108
108
109
109
const encodedUrl = encodeUrlAndTransforms ( expectedId , 'q=101' , '.jpg' )
110
-
110
+
111
111
const response = decodeBase64Params ( encodedUrl )
112
-
112
+
113
113
t . deepEqual ( response , expectedResponse )
114
114
} )
115
115
116
116
test ( 'decodeBase64Params: returns expected response if transform contains pos' , ( t ) => {
117
- const expectedId = '/images/image.jpg' ;
117
+ const expectedId = '/images/image.jpg'
118
118
const expectedResponse = {
119
119
id : expectedId ,
120
120
modifiers : 'crop_10 20'
121
121
}
122
122
123
123
const encodedUrl = encodeUrlAndTransforms ( expectedId , 'pos=10,20' , '.jpg' )
124
-
124
+
125
125
const response = decodeBase64Params ( encodedUrl )
126
-
126
+
127
127
t . deepEqual ( response , expectedResponse )
128
128
} )
129
129
130
130
test ( 'decodeBase64Params: returns expected response if transform contains multiple valid props' , ( t ) => {
131
- const expectedId = '/images/image.jpg' ;
131
+ const expectedId = '/images/image.jpg'
132
132
const expectedResponse = {
133
133
id : expectedId ,
134
134
modifiers : 's_100x200,f_101,crop_10 20,q_1234'
135
135
}
136
136
137
137
const encodedUrl = encodeUrlAndTransforms ( expectedId , 'pos=10,20&w=100&h=200&fm=101&q=1234' , '.jpg' )
138
-
138
+
139
139
const response = decodeBase64Params ( encodedUrl )
140
-
140
+
141
141
t . deepEqual ( response , expectedResponse )
142
142
} )
143
143
144
144
test ( "doPatternsMatchUrl: returns false if protocol exists in remote pattern and it doesn't match" , ( t ) => {
145
- const remotePattern : RemotePattern = {
145
+ const remotePattern : RemotePattern = {
146
146
protocol : 'http' ,
147
147
hostname : 'fail.com'
148
148
}
149
149
const parsedUrl = parseURL ( 'https://fake.url/images?w=100&h=200&fm=1234' , 'https://' )
150
150
151
151
const matches = doPatternsMatchUrl ( remotePattern , parsedUrl )
152
-
152
+
153
153
t . false ( matches )
154
154
} )
155
155
156
156
test ( "doPatternsMatchUrl: returns false if port exists in remote pattern and the full host doesn't match" , ( t ) => {
157
- const remotePattern : RemotePattern = {
157
+ const remotePattern : RemotePattern = {
158
158
hostname : 'fail.com' ,
159
159
port : '3000'
160
160
}
161
161
const parsedUrl = parseURL ( 'https://fake.url/images?w=100&h=200&fm=1234' , 'https://' )
162
162
163
163
const matches = doPatternsMatchUrl ( remotePattern , parsedUrl )
164
-
164
+
165
165
t . false ( matches )
166
166
} )
167
167
168
168
test ( "doPatternsMatchUrl: returns false if port doesn't exists in remote pattern and the host doesn't match" , ( t ) => {
169
- const remotePattern : RemotePattern = {
170
- hostname : 'fail.com' ,
169
+ const remotePattern : RemotePattern = {
170
+ hostname : 'fail.com'
171
171
}
172
172
const parsedUrl = parseURL ( 'https://fake.url/images?w=100&h=200&fm=1234' , 'https://' )
173
173
174
174
const matches = doPatternsMatchUrl ( remotePattern , parsedUrl )
175
-
175
+
176
176
t . false ( matches )
177
177
} )
178
178
179
179
test ( "doPatternsMatchUrl: returns false if the pathname doesn't match" , ( t ) => {
180
- const remotePattern : RemotePattern = {
180
+ const remotePattern : RemotePattern = {
181
181
protocol : 'https' ,
182
182
hostname : 'fake.url' ,
183
183
pathname : '/failpath'
184
184
}
185
185
const parsedUrl = parseURL ( 'https://fake.url/images?w=100&h=200&fm=1234' , 'https://' )
186
186
187
187
const matches = doPatternsMatchUrl ( remotePattern , parsedUrl )
188
-
188
+
189
189
t . false ( matches )
190
190
} )
191
191
192
- test ( " doPatternsMatchUrl: returns true if remote pattern fully matches" , ( t ) => {
193
- const remotePattern : RemotePattern = {
192
+ test ( ' doPatternsMatchUrl: returns true if remote pattern fully matches' , ( t ) => {
193
+ const remotePattern : RemotePattern = {
194
194
protocol : 'https' ,
195
195
hostname : 'fake.url' ,
196
196
port : '3000' ,
@@ -199,19 +199,19 @@ test("doPatternsMatchUrl: returns true if remote pattern fully matches", (t) =>
199
199
const parsedUrl = parseURL ( 'https://fake.url:3000/images?w=100&h=200&fm=1234' , 'https://' )
200
200
201
201
const matches = doPatternsMatchUrl ( remotePattern , parsedUrl )
202
-
202
+
203
203
t . true ( matches )
204
204
} )
205
205
206
- test ( " doPatternsMatchUrl: returns true if remote pattern fully matches without pathname" , ( t ) => {
207
- const remotePattern : RemotePattern = {
206
+ test ( ' doPatternsMatchUrl: returns true if remote pattern fully matches without pathname' , ( t ) => {
207
+ const remotePattern : RemotePattern = {
208
208
protocol : 'https' ,
209
209
hostname : 'fake.url' ,
210
210
port : '3000'
211
211
}
212
212
const parsedUrl = parseURL ( 'https://fake.url:3000/images?w=100&h=200&fm=1234' , 'https://' )
213
213
214
214
const matches = doPatternsMatchUrl ( remotePattern , parsedUrl )
215
-
215
+
216
216
t . true ( matches )
217
217
} )
0 commit comments