/tests/test_url.py

https://gitlab.com/mkobar/thumbor · Python · 151 lines · 116 code · 27 blank · 8 comment · 0 complexity · 2c09e1bc379bcbb599b5a6c2cb1f518d MD5 · raw file

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # thumbor imaging service
  4. # https://github.com/thumbor/thumbor/wiki
  5. # Licensed under the MIT license:
  6. # http://www.opensource.org/licenses/mit-license
  7. # Copyright (c) 2011 globo.com timehome@corp.globo.com
  8. from unittest import TestCase
  9. from preggy import expect
  10. from thumbor.url import Url
  11. class UrlTestCase(TestCase):
  12. def setUp(self):
  13. Url.compiled_regex = None
  14. def test_can_get_regex(self):
  15. regex = Url.regex()
  16. expect(regex).to_equal(
  17. '/?(?:(?:(?P<unsafe>unsafe)|(?P<hash>.+?))/)?(?:(?P<debug>debug)/)?(?:(?P<meta>meta)/)?'
  18. '(?:(?P<trim>trim(?::(?:top-left|bottom-right))?(?::\\d+)?)/)?'
  19. '(?:(?P<crop_left>\\d+)x(?P<crop_top>\\d+):(?P<crop_right>\\d+)x(?P<crop_bottom>\\d+)/)?'
  20. '(?:(?P<adaptive>adaptive-)?(?P<full>full-)?(?P<fit_in>fit-in)/)?(?:(?P<horizontal_flip>-)?'
  21. '(?P<width>(?:\\d+|orig))?x(?P<vertical_flip>-)?(?P<height>(?:\\d+|orig))?/)?'
  22. '(?:(?P<halign>left|right|center)/)?(?:(?P<valign>top|bottom|middle)/)?'
  23. '(?:(?P<smart>smart)/)?(?:filters:(?P<filters>.+?\\))/)?(?P<image>.+)'
  24. )
  25. def test_can_get_regex_without_unsafe(self):
  26. regex = Url.regex(False)
  27. expect(regex).to_equal(
  28. '/?(?:(?P<debug>debug)/)?(?:(?P<meta>meta)/)?'
  29. '(?:(?P<trim>trim(?::(?:top-left|bottom-right))?(?::\\d+)?)/)?'
  30. '(?:(?P<crop_left>\\d+)x(?P<crop_top>\\d+):(?P<crop_right>\\d+)x(?P<crop_bottom>\\d+)/)?'
  31. '(?:(?P<adaptive>adaptive-)?(?P<full>full-)?(?P<fit_in>fit-in)/)?(?:(?P<horizontal_flip>-)?'
  32. '(?P<width>(?:\\d+|orig))?x(?P<vertical_flip>-)?(?P<height>(?:\\d+|orig))?/)?'
  33. '(?:(?P<halign>left|right|center)/)?(?:(?P<valign>top|bottom|middle)/)?'
  34. '(?:(?P<smart>smart)/)?(?:filters:(?P<filters>.+?\\))/)?(?P<image>.+)'
  35. )
  36. def test_parsing_invalid_url(self):
  37. expect(Url.compiled_regex).to_be_null()
  38. url = ""
  39. expect(Url.parse_decrypted(url)).to_be_null()
  40. def test_parsing_complete_url(self):
  41. url = '/debug/meta/trim/300x200:400x500/adaptive-full-fit-in/-300x-400/' \
  42. 'left/top/smart/filters:brightness(100)/some/image.jpg'
  43. expected = {
  44. 'trim': 'trim',
  45. 'full': True,
  46. 'halign': 'left',
  47. 'fit_in': True,
  48. 'vertical_flip': True,
  49. 'image': 'some/image.jpg',
  50. 'crop': {'top': 200, 'right': 400, 'bottom': 500, 'left': 300},
  51. 'height': 400,
  52. 'width': 300,
  53. 'meta': True,
  54. 'horizontal_flip': True,
  55. 'filters': 'brightness(100)',
  56. 'valign': 'top',
  57. 'debug': True,
  58. 'adaptive': True,
  59. 'smart': True,
  60. }
  61. result = Url.parse_decrypted(url)
  62. expect(result).not_to_be_null()
  63. expect(result).to_be_like(expected)
  64. # do it again to use compiled regex
  65. result = Url.parse_decrypted(url)
  66. expect(result).not_to_be_null()
  67. expect(result).to_be_like(expected)
  68. def test_can_generate_url(self):
  69. url = Url.generate_options(
  70. debug=True,
  71. width=300,
  72. height=200,
  73. smart=True,
  74. meta=True,
  75. trim=True,
  76. adaptive=True,
  77. full=True,
  78. fit_in=True,
  79. horizontal_flip=True,
  80. vertical_flip=True,
  81. halign='left',
  82. valign='top',
  83. crop_left=100,
  84. crop_top=100,
  85. crop_right=400,
  86. crop_bottom=400,
  87. filters='brightness(100)'
  88. )
  89. expect(url).to_equal(
  90. 'debug/meta/trim/100x100:400x400/adaptive-full-fit-in/-300x-200/left/top/smart/filters:brightness(100)'
  91. )
  92. def test_can_generate_url_with_defaults(self):
  93. url = Url.generate_options()
  94. expect(url).to_be_empty()
  95. def test_can_generate_url_with_fitin(self):
  96. url = Url.generate_options(fit_in=True, adaptive=False, full=False)
  97. expect(url).to_equal('fit-in')
  98. def test_can_generate_url_with_custom_trim(self):
  99. url = Url.generate_options(
  100. debug=True,
  101. width=300,
  102. height=200,
  103. smart=True,
  104. meta=True,
  105. trim='300x200',
  106. adaptive=True,
  107. full=True,
  108. fit_in=True,
  109. horizontal_flip=True,
  110. vertical_flip=True,
  111. halign='left',
  112. valign='top',
  113. crop_left=100,
  114. crop_top=100,
  115. crop_right=400,
  116. crop_bottom=400,
  117. filters='brightness(100)'
  118. )
  119. expect(url).to_equal(
  120. 'debug/meta/trim:300x200/100x100:400x400/adaptive-full-fit-in/-300x-200/left/top/smart/filters:brightness(100)'
  121. )
  122. def test_can_encode_url(self):
  123. url = '/tes+ t:?%=&()~",\'$'
  124. expect(Url.encode_url(url)).to_equal('/tes%2B%20t:?%=&()~",\'$')