Skip to content

Commit d05c7f6

Browse files
ANKUR DWIVEDIANKUR DWIVEDI
authored andcommitted
improve test case
1 parent 9f0e968 commit d05c7f6

File tree

2 files changed

+89
-17
lines changed

2 files changed

+89
-17
lines changed

imagekitio/url.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from datetime import datetime as dt
55
from typing import Any, Dict, List
6-
from urllib.parse import ParseResult, urlparse, urlunparse, parse_qsl, urlencode, quote
6+
from urllib.parse import ParseResult, urlparse, urlunparse, parse_qsl, urlencode, quote, unquote
77

88
from .constants.defaults import Default
99
from .constants.supported_transform import SUPPORTED_TRANS
@@ -130,13 +130,7 @@ def get_signature_timestamp(expiry_seconds: int = None) -> int:
130130

131131
@staticmethod
132132
def get_signature(private_key, url, url_endpoint, expiry_timestamp: int) -> str:
133-
last_slash_pos = url.rfind('/')
134-
question_mark_pos = url.find('?', last_slash_pos)
135-
path = url[last_slash_pos + 1:question_mark_pos] if question_mark_pos != -1 else url[last_slash_pos + 1:]
136-
encoded_path = Url.encode_string_if_required(path)
137-
encoded_url = url[:last_slash_pos + 1] + encoded_path + url[question_mark_pos:] if question_mark_pos != -1 else url[:last_slash_pos + 1] + encoded_path
138-
url = encoded_url
139-
print(url)
133+
url = Url.encode_string_if_required(url)
140134
""" "
141135
create signature(hashed hex key) from
142136
private_key, url, url_endpoint and expiry_timestamp
@@ -219,10 +213,19 @@ def transformation_to_str(transformation):
219213

220214
return Default.CHAIN_TRANSFORM_DELIMITER.value.join(parsed_transforms)
221215

216+
@staticmethod
217+
def custom_encodeURIComponent(url_str):
218+
parsed_url = urlparse(url_str)
219+
encoded_url = f"{parsed_url.scheme}://{parsed_url.netloc}"
220+
encoded_url +=quote(parsed_url.path, safe='~@#$&()*!+=:;,?/\'')
221+
if(parsed_url.query):
222+
encoded_url = encoded_url+"?"+quote(unquote(parsed_url.query), safe='~@#$&()*!+=:;?/\'')
223+
return encoded_url
224+
222225
@staticmethod
223226
def has_more_than_ascii(s):
224227
return any(ord(char) > 127 for char in s)
225228

226229
@staticmethod
227230
def encode_string_if_required(s):
228-
return quote(s) if Url.has_more_than_ascii(s) else s
231+
return Url.custom_encodeURIComponent(s) if Url.has_more_than_ascii(s) else s

tests/test_generate_url.py

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from imagekitio.client import ImageKit
44
from imagekitio.constants.defaults import Default
5-
5+
from imagekitio.url import Url
66

77
class TestGenerateURL(unittest.TestCase):
88
def setUp(self) -> None:
@@ -325,21 +325,90 @@ def test_url_signed_with_expire_in_seconds(self):
325325
}
326326
url = self.client.url(options)
327327
self.assertIn("ik-t", url)
328-
329-
def test_url_signed_with_expire_in_seconds_with_diacritic(self):
328+
329+
def test_url_signed_with_diacritic_in_filename(self):
330+
encodedUrl = Url.encode_string_if_required("https://test-domain.com/test-endpoint/test_é_path_alt.jpg")
331+
self.assertEqual(
332+
encodedUrl,
333+
"https://test-domain.com/test-endpoint/test_%C3%A9_path_alt.jpg",
334+
)
335+
signature = Url.get_signature("private_key_test", encodedUrl,"https://test-domain.com/test-endpoint", 9999999999)
330336
options = {
331-
"path": "/four-penguins-with-é.webp",
337+
"path": "/test_é_path_alt.jpg",
338+
"signed": True,
339+
}
340+
url = self.client.url(options)
341+
self.assertEqual(
342+
url,
343+
"https://test-domain.com/test-endpoint/test_é_path_alt.jpg?ik-s=cae8a516223d21e245424dc0b97163aa3a03c789",
344+
)
345+
self.assertIn("ik-s="+signature, url)
346+
347+
def test_url_signed_with_diacritic_in_filename_and_path(self):
348+
encodedUrl = Url.encode_string_if_required("https://test-domain.com/test-endpoint/aéb/test_é_path_alt.jpg")
349+
self.assertEqual(
350+
encodedUrl,
351+
"https://test-domain.com/test-endpoint/a%C3%A9b/test_%C3%A9_path_alt.jpg",
352+
)
353+
signature = Url.get_signature("private_key_test", encodedUrl,"https://test-domain.com/test-endpoint", 9999999999)
354+
options = {
355+
"path": "/aéb/test_é_path_alt.jpg",
356+
"signed": True,
357+
}
358+
url = self.client.url(options)
359+
self.assertEqual(
360+
url,
361+
"https://test-domain.com/test-endpoint/aéb/test_é_path_alt.jpg?ik-s=40a821ef1b7a2e9253599dac36a47be3d49787ab",
362+
)
363+
self.assertIn("ik-s="+signature, url)
364+
365+
def test_url_signed_with_diacritic_in_filename_path_transforamtion_in_path(self):
366+
encodedUrl = Url.encode_string_if_required("https://test-domain.com/test-endpoint/tr:l-text,i-Imagekité,fs-50,l-end/aéb/test_é_path_alt.jpg")
367+
self.assertEqual(
368+
encodedUrl,
369+
"https://test-domain.com/test-endpoint/tr:l-text,i-Imagekit%C3%A9,fs-50,l-end/a%C3%A9b/test_%C3%A9_path_alt.jpg",
370+
)
371+
signature = Url.get_signature("private_key_test", encodedUrl,"https://test-domain.com/test-endpoint", 9999999999)
372+
options = {
373+
"path": "/aéb/test_é_path_alt.jpg",
332374
"transformation": [
333375
{
334-
"width": "400",
376+
"raw": "l-text,i-Imagekité,fs-50,l-end"
335377
},
336378
],
337379
"signed": True,
338-
"expire_seconds": 100,
380+
"transformation_position": "path"
339381
}
340382
url = self.client.url(options)
341-
self.assertIn("ik-s", url)
342-
self.assertIn("ik-t", url)
383+
self.assertEqual(
384+
url,
385+
"https://test-domain.com/test-endpoint/tr:l-text,i-Imagekité,fs-50,l-end/aéb/test_é_path_alt.jpg?ik-s=3a65902916664517bcac38e9ddaa69d4a81a0106",
386+
)
387+
self.assertIn("ik-s="+signature, url)
388+
389+
def test_url_signed_with_diacritic_in_filename_path_transforamtion_in_query(self):
390+
encodedUrl = Url.encode_string_if_required("https://test-domain.com/test-endpoint/aéb/test_é_path_alt.jpg?tr=l-text%2Ci-Imagekité%2Cfs-50%2Cl-end")
391+
self.assertEqual(
392+
encodedUrl,
393+
"https://test-domain.com/test-endpoint/a%C3%A9b/test_%C3%A9_path_alt.jpg?tr=l-text%2Ci-Imagekit%C3%A9%2Cfs-50%2Cl-end",
394+
)
395+
signature = Url.get_signature("private_key_test", encodedUrl,"https://test-domain.com/test-endpoint", 9999999999)
396+
options = {
397+
"path": "/aéb/test_é_path_alt.jpg",
398+
"transformation": [
399+
{
400+
"raw": "l-text,i-Imagekité,fs-50,l-end"
401+
},
402+
],
403+
"signed": True,
404+
"transformation_position": "query"
405+
}
406+
url = self.client.url(options)
407+
self.assertEqual(
408+
url,
409+
"https://test-domain.com/test-endpoint/aéb/test_é_path_alt.jpg?tr=l-text%2Ci-Imagekit%C3%A9%2Cfs-50%2Cl-end&ik-s=ce55719add9ac43908c9de6c0eceece70f4d6c6e",
410+
)
411+
self.assertIn("ik-s="+signature, url)
343412

344413
def test_generate_url_with_path_and_src_uses_path(self):
345414
"""

0 commit comments

Comments
 (0)