resizing 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +17 -2
- data/Gemfile +6 -2
- data/README.md +59 -32
- data/lib/resizing/active_storage/service/resizing_service.rb +6 -2
- data/lib/resizing/active_storage/service.rb +9 -0
- data/lib/resizing/active_storage.rb +7 -0
- data/lib/resizing/carrier_wave/storage/file.rb +35 -16
- data/lib/resizing/carrier_wave/storage/remote.rb +7 -3
- data/lib/resizing/carrier_wave.rb +10 -11
- data/lib/resizing/client.rb +25 -23
- data/lib/resizing/configurable.rb +1 -1
- data/lib/resizing/configuration.rb +6 -16
- data/lib/resizing/http_clientable.rb +3 -3
- data/lib/resizing/mock_client.rb +6 -5
- data/lib/resizing/public_id.rb +5 -4
- data/lib/resizing/version.rb +1 -1
- data/lib/resizing.rb +15 -12
- data/resizing.gemspec +5 -8
- data/test/resizing/active_storage_service_test.rb +98 -0
- data/test/resizing/carrier_wave/storage/file_test.rb +149 -8
- data/test/resizing/carrier_wave/storage/remote_test.rb +75 -0
- data/test/resizing/carrier_wave_test.rb +99 -37
- data/test/resizing/client_test.rb +68 -12
- data/test/resizing/configurable_test.rb +82 -0
- data/test/resizing/configuration_test.rb +118 -2
- data/test/resizing/constants_test.rb +25 -0
- data/test/resizing/error_test.rb +73 -0
- data/test/resizing/http_clientable_test.rb +84 -0
- data/test/resizing/mock_client_test.rb +75 -0
- data/test/resizing/public_id_test.rb +1 -1
- data/test/resizing_module_test.rb +206 -0
- data/test/test_helper.rb +89 -9
- metadata +28 -36
- /data/{exe → bin}/console +0 -0
- /data/{exe → bin}/generate-changelog +0 -0
- /data/{exe → bin}/setup +0 -0
|
@@ -38,7 +38,7 @@ module Resizing
|
|
|
38
38
|
Resizing.configure = @configuration_template
|
|
39
39
|
|
|
40
40
|
client = Resizing::Client.new
|
|
41
|
-
VCR.use_cassette 'client/post', record: :once
|
|
41
|
+
VCR.use_cassette 'client/post', record: :once do
|
|
42
42
|
r = client.post('test/data/images/sample1.jpg', content_type: 'image/jpeg')
|
|
43
43
|
assert_equal(r['id'], '87263920-2081-498e-a107-9625f4fde01b')
|
|
44
44
|
assert_equal(r['project_id'], Resizing.configure.project_id)
|
|
@@ -56,7 +56,7 @@ module Resizing
|
|
|
56
56
|
Resizing.configure = @configuration_template
|
|
57
57
|
|
|
58
58
|
client = Resizing::Client.new
|
|
59
|
-
VCR.use_cassette 'client/post', record: :once
|
|
59
|
+
VCR.use_cassette 'client/post', record: :once do
|
|
60
60
|
assert_raises ArgumentError do
|
|
61
61
|
client.post('file_is_not_exists', content_type: 'image/jpeg')
|
|
62
62
|
end
|
|
@@ -67,7 +67,7 @@ module Resizing
|
|
|
67
67
|
Resizing.configure = @configuration_template
|
|
68
68
|
|
|
69
69
|
client = Resizing::Client.new
|
|
70
|
-
VCR.use_cassette 'client/post', record: :once
|
|
70
|
+
VCR.use_cassette 'client/post', record: :once do
|
|
71
71
|
f = File.open('test/data/images/sample1.jpg', 'r')
|
|
72
72
|
r = client.post(f, content_type: 'image/jpeg')
|
|
73
73
|
assert_equal(r['id'], '87263920-2081-498e-a107-9625f4fde01b')
|
|
@@ -122,7 +122,7 @@ module Resizing
|
|
|
122
122
|
f = File.open('test/data/images/sample1.jpg', 'r')
|
|
123
123
|
|
|
124
124
|
assert_raises Resizing::APIError do
|
|
125
|
-
|
|
125
|
+
_r = client.put(name, f, content_type: 'image/jpeg')
|
|
126
126
|
end
|
|
127
127
|
end
|
|
128
128
|
|
|
@@ -152,7 +152,7 @@ module Resizing
|
|
|
152
152
|
rescue Resizing::APIError => e
|
|
153
153
|
response = e.decoded_body
|
|
154
154
|
end
|
|
155
|
-
assert_equal response, {
|
|
155
|
+
assert_equal response, { 'error' => 'Magick::ImageMagickError', 'message' => 'invalid image format found' }
|
|
156
156
|
end
|
|
157
157
|
end
|
|
158
158
|
|
|
@@ -165,20 +165,76 @@ module Resizing
|
|
|
165
165
|
VCR.use_cassette 'client/metadata', record: :once do
|
|
166
166
|
name = '87263920-2081-498e-a107-9625f4fde01b'
|
|
167
167
|
r = client.metadata(name)
|
|
168
|
+
# r.body
|
|
169
|
+
# {
|
|
170
|
+
# "id":"87263920-2081-498e-a107-9625f4fde01b",
|
|
171
|
+
# "project_id":"e06e710d-f026-4dcf-b2c0-eab0de8bb83f",
|
|
172
|
+
# "content_type":"image/jpeg",
|
|
173
|
+
# "latest_version_id":"Hg9VFvdI6HRzLFbV495VdwVmHIspLRCo",
|
|
174
|
+
# "latest_etag":"\"5766f95a7f28e6a53dd6fd179bf03a32\"",
|
|
175
|
+
# "size":848590,
|
|
176
|
+
# "created_at":"2020-10-11T05:02:25.912Z",
|
|
177
|
+
# "updated_at":"2020-10-11T05:02:25.912Z",
|
|
178
|
+
# "filename":"sample1.jpg",
|
|
179
|
+
# "width":4032,
|
|
180
|
+
# "height":3016,
|
|
181
|
+
# "format":"jpeg",
|
|
182
|
+
# "version":"Hg9VFvdI6HRzLFbV495VdwVmHIspLRCo",
|
|
183
|
+
# "public_id":"/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/87263920-2081-498e-a107-9625f4fde01b/vHg9VFvdI6HRzLFbV495VdwVmHIspLRCo"
|
|
184
|
+
# }
|
|
185
|
+
|
|
168
186
|
assert_equal(r['id'], name)
|
|
169
187
|
assert_equal(r['project_id'], Resizing.configure.project_id)
|
|
170
188
|
assert_equal(r['content_type'], 'image/jpeg')
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
189
|
+
assert_equal(r['latest_version_id'], 'Hg9VFvdI6HRzLFbV495VdwVmHIspLRCo')
|
|
190
|
+
assert_equal(r['latest_etag'], '"5766f95a7f28e6a53dd6fd179bf03a32"')
|
|
191
|
+
assert_equal(r['created_at'], '2020-10-11T05:02:25.912Z')
|
|
192
|
+
assert_equal(r['updated_at'], '2020-10-11T05:02:25.912Z')
|
|
193
|
+
assert_equal(r['width'], 4032)
|
|
194
|
+
assert_equal(r['height'], 3016)
|
|
195
|
+
assert_equal(r['format'], 'jpeg')
|
|
196
|
+
assert_equal(r['version'], 'Hg9VFvdI6HRzLFbV495VdwVmHIspLRCo')
|
|
177
197
|
assert_equal(
|
|
178
198
|
r['public_id'],
|
|
179
|
-
|
|
199
|
+
'/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/87263920-2081-498e-a107-9625f4fde01b/vHg9VFvdI6HRzLFbV495VdwVmHIspLRCo'
|
|
180
200
|
)
|
|
181
201
|
end
|
|
182
202
|
end
|
|
203
|
+
|
|
204
|
+
def test_get_raises_not_implemented_error
|
|
205
|
+
Resizing.configure = @configuration_template
|
|
206
|
+
client = Resizing::Client.new
|
|
207
|
+
|
|
208
|
+
assert_raises NotImplementedError do
|
|
209
|
+
client.get('some_image_id')
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def test_post_raises_error_without_content_type
|
|
214
|
+
Resizing.configure = @configuration_template
|
|
215
|
+
client = Resizing::Client.new
|
|
216
|
+
|
|
217
|
+
assert_raises ArgumentError do
|
|
218
|
+
client.post('test/data/images/sample1.jpg', {})
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def test_post_raises_error_with_invalid_io
|
|
223
|
+
Resizing.configure = @configuration_template
|
|
224
|
+
client = Resizing::Client.new
|
|
225
|
+
|
|
226
|
+
assert_raises ArgumentError do
|
|
227
|
+
client.post(12_345, content_type: 'image/jpeg')
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def test_put_raises_error_without_content_type
|
|
232
|
+
Resizing.configure = @configuration_template
|
|
233
|
+
client = Resizing::Client.new
|
|
234
|
+
|
|
235
|
+
assert_raises ArgumentError do
|
|
236
|
+
client.put('image_id', 'test/data/images/sample1.jpg', {})
|
|
237
|
+
end
|
|
238
|
+
end
|
|
183
239
|
end
|
|
184
240
|
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
module Resizing
|
|
6
|
+
class ConfigurableTest < Minitest::Test
|
|
7
|
+
class TestConfigurable
|
|
8
|
+
include Configurable
|
|
9
|
+
|
|
10
|
+
def initialize(config = nil)
|
|
11
|
+
initialize_config(config)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def setup
|
|
16
|
+
# NOP
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def teardown
|
|
20
|
+
# NOP
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_initialize_config_with_configuration_object
|
|
24
|
+
config = Configuration.new(
|
|
25
|
+
image_host: 'https://test.example.com',
|
|
26
|
+
project_id: 'test_id',
|
|
27
|
+
secret_token: 'test_token'
|
|
28
|
+
)
|
|
29
|
+
obj = TestConfigurable.new(config)
|
|
30
|
+
|
|
31
|
+
assert_equal config, obj.config
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def test_initialize_config_with_nil_uses_global_configure
|
|
35
|
+
Resizing.configure = Configuration.new(
|
|
36
|
+
image_host: 'https://global.example.com',
|
|
37
|
+
project_id: 'global_id',
|
|
38
|
+
secret_token: 'global_token'
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
obj = TestConfigurable.new(nil)
|
|
42
|
+
|
|
43
|
+
assert_equal Resizing.configure.image_host, obj.config.image_host
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def test_initialize_config_with_hash
|
|
47
|
+
config_hash = {
|
|
48
|
+
image_host: 'https://hash.example.com',
|
|
49
|
+
project_id: 'hash_id',
|
|
50
|
+
secret_token: 'hash_token'
|
|
51
|
+
}
|
|
52
|
+
obj = TestConfigurable.new(config_hash)
|
|
53
|
+
|
|
54
|
+
assert_equal 'https://hash.example.com', obj.config.image_host
|
|
55
|
+
assert_equal 'hash_id', obj.config.project_id
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_config_is_accessible
|
|
59
|
+
config = Configuration.new(
|
|
60
|
+
image_host: 'https://test.example.com',
|
|
61
|
+
project_id: 'test_id',
|
|
62
|
+
secret_token: 'test_token'
|
|
63
|
+
)
|
|
64
|
+
obj = TestConfigurable.new(config)
|
|
65
|
+
|
|
66
|
+
assert_instance_of Configuration, obj.config
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def test_attr_reader_config_is_defined
|
|
70
|
+
config = Configuration.new(
|
|
71
|
+
image_host: 'https://test.example.com',
|
|
72
|
+
project_id: 'test_id',
|
|
73
|
+
secret_token: 'test_token'
|
|
74
|
+
)
|
|
75
|
+
obj = TestConfigurable.new(config)
|
|
76
|
+
|
|
77
|
+
# Verify that config is accessible as a reader
|
|
78
|
+
assert obj.respond_to?(:config)
|
|
79
|
+
assert !obj.respond_to?(:config=)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -23,7 +23,6 @@ module Resizing
|
|
|
23
23
|
template = @template.dup
|
|
24
24
|
template.delete(:image_host)
|
|
25
25
|
config = Resizing::Configuration.new template
|
|
26
|
-
assert_equal(config.host, Resizing::Configuration::DEFAULT_IMAGE_HOST)
|
|
27
26
|
assert_equal(config.image_host, Resizing::Configuration::DEFAULT_IMAGE_HOST)
|
|
28
27
|
end
|
|
29
28
|
|
|
@@ -38,7 +37,7 @@ module Resizing
|
|
|
38
37
|
template = @template.dup
|
|
39
38
|
template[:host] = 'need raise execption if host is presented'
|
|
40
39
|
assert_raises ConfigurationError do
|
|
41
|
-
|
|
40
|
+
_config = Resizing::Configuration.new template
|
|
42
41
|
end
|
|
43
42
|
end
|
|
44
43
|
|
|
@@ -147,5 +146,122 @@ module Resizing
|
|
|
147
146
|
config = Resizing::Configuration.new @template
|
|
148
147
|
assert_match %r{/projects/#{config.project_id}/upload/images/[\da-z-]}, config.generate_identifier
|
|
149
148
|
end
|
|
149
|
+
|
|
150
|
+
def test_generate_image_id_returns_uuid
|
|
151
|
+
config = Resizing::Configuration.new @template
|
|
152
|
+
image_id = config.generate_image_id
|
|
153
|
+
|
|
154
|
+
assert_instance_of String, image_id
|
|
155
|
+
# UUID format: 8-4-4-4-12
|
|
156
|
+
assert_match(/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/, image_id)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def test_generate_image_id_returns_different_uuids
|
|
160
|
+
config = Resizing::Configuration.new @template
|
|
161
|
+
id1 = config.generate_image_id
|
|
162
|
+
id2 = config.generate_image_id
|
|
163
|
+
|
|
164
|
+
refute_equal id1, id2
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def test_equality_returns_true_for_same_configurations
|
|
168
|
+
config1 = Resizing::Configuration.new @template
|
|
169
|
+
config2 = Resizing::Configuration.new @template
|
|
170
|
+
|
|
171
|
+
assert_equal config1, config2
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def test_equality_returns_false_for_different_image_host
|
|
175
|
+
config1 = Resizing::Configuration.new @template
|
|
176
|
+
template2 = @template.dup
|
|
177
|
+
template2[:image_host] = 'http://different.host'
|
|
178
|
+
config2 = Resizing::Configuration.new template2
|
|
179
|
+
|
|
180
|
+
refute_equal config1, config2
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def test_equality_returns_false_for_different_project_id
|
|
184
|
+
config1 = Resizing::Configuration.new @template
|
|
185
|
+
template2 = @template.dup
|
|
186
|
+
template2[:project_id] = 'different-project-id'
|
|
187
|
+
config2 = Resizing::Configuration.new template2
|
|
188
|
+
|
|
189
|
+
refute_equal config1, config2
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def test_equality_returns_false_for_different_class
|
|
193
|
+
config = Resizing::Configuration.new @template
|
|
194
|
+
|
|
195
|
+
refute_equal config, 'not a configuration'
|
|
196
|
+
refute_equal config, @template
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def test_transformation_path_with_multiple_transforms
|
|
200
|
+
config = Resizing::Configuration.new @template
|
|
201
|
+
transforms = [
|
|
202
|
+
{ w: 100, h: 200 },
|
|
203
|
+
{ f: 'webp', q: 80 }
|
|
204
|
+
]
|
|
205
|
+
|
|
206
|
+
path = config.transformation_path(transforms)
|
|
207
|
+
|
|
208
|
+
assert_equal 'w_100,h_200/f_webp,q_80', path
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def test_transformation_path_with_single_hash
|
|
212
|
+
config = Resizing::Configuration.new @template
|
|
213
|
+
transform = { w: 300, h: 300, c: 'fill' }
|
|
214
|
+
|
|
215
|
+
path = config.transformation_path(transform)
|
|
216
|
+
|
|
217
|
+
assert_equal 'w_300,h_300,c_fill', path
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def test_transformation_path_ignores_unknown_options
|
|
221
|
+
config = Resizing::Configuration.new @template
|
|
222
|
+
transform = { w: 100, unknown_option: 'ignored', h: 200 }
|
|
223
|
+
|
|
224
|
+
path = config.transformation_path(transform)
|
|
225
|
+
|
|
226
|
+
assert_equal 'w_100,h_200', path
|
|
227
|
+
refute_includes path, 'unknown_option'
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def test_transformation_path_with_empty_array
|
|
231
|
+
config = Resizing::Configuration.new @template
|
|
232
|
+
path = config.transformation_path([])
|
|
233
|
+
|
|
234
|
+
assert_equal '', path
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def test_generate_identifier_includes_project_id
|
|
238
|
+
config = Resizing::Configuration.new @template
|
|
239
|
+
identifier = config.generate_identifier
|
|
240
|
+
|
|
241
|
+
assert_includes identifier, config.project_id
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def test_generate_identifier_has_correct_format
|
|
245
|
+
config = Resizing::Configuration.new @template
|
|
246
|
+
identifier = config.generate_identifier
|
|
247
|
+
|
|
248
|
+
assert_match %r{\A/projects/[\da-z-]+/upload/images/[\da-z-]+\z}, identifier
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def test_enable_mock_defaults_to_false
|
|
252
|
+
template = @template.dup
|
|
253
|
+
template.delete(:enable_mock)
|
|
254
|
+
config = Resizing::Configuration.new template
|
|
255
|
+
|
|
256
|
+
assert_equal false, config.enable_mock
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def test_enable_mock_can_be_set_to_true
|
|
260
|
+
template = @template.dup
|
|
261
|
+
template[:enable_mock] = true
|
|
262
|
+
config = Resizing::Configuration.new template
|
|
263
|
+
|
|
264
|
+
assert_equal true, config.enable_mock
|
|
265
|
+
end
|
|
150
266
|
end
|
|
151
267
|
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
module Resizing
|
|
6
|
+
class ConstantsTest < Minitest::Test
|
|
7
|
+
def test_http_status_ok_is_defined
|
|
8
|
+
assert_equal 200, Resizing::Constants::HTTP_STATUS_OK
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def test_http_status_created_is_defined
|
|
12
|
+
assert_equal 201, Resizing::Constants::HTTP_STATUS_CREATED
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_http_status_not_found_is_defined
|
|
16
|
+
assert_equal 404, Resizing::Constants::HTTP_STATUS_NOT_FOUND
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_constants_are_integers
|
|
20
|
+
assert_instance_of Integer, Resizing::Constants::HTTP_STATUS_OK
|
|
21
|
+
assert_instance_of Integer, Resizing::Constants::HTTP_STATUS_CREATED
|
|
22
|
+
assert_instance_of Integer, Resizing::Constants::HTTP_STATUS_NOT_FOUND
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
module Resizing
|
|
6
|
+
class ErrorTest < Minitest::Test
|
|
7
|
+
def test_error_is_standard_error_subclass
|
|
8
|
+
assert Resizing::Error < StandardError
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def test_configuration_error_is_error_subclass
|
|
12
|
+
assert Resizing::ConfigurationError < Resizing::Error
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_api_error_is_error_subclass
|
|
16
|
+
assert Resizing::APIError < Resizing::Error
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_api_error_has_decoded_body_accessor
|
|
20
|
+
error = Resizing::APIError.new('test error')
|
|
21
|
+
|
|
22
|
+
assert_respond_to error, :decoded_body
|
|
23
|
+
assert_respond_to error, :decoded_body=
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_api_error_decoded_body_defaults_to_empty_hash
|
|
27
|
+
error = Resizing::APIError.new('test error')
|
|
28
|
+
|
|
29
|
+
assert_equal({}, error.decoded_body)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_api_error_decoded_body_can_be_set_with_hash
|
|
33
|
+
error = Resizing::APIError.new('test error')
|
|
34
|
+
body = { 'error' => 'test', 'code' => 400 }
|
|
35
|
+
|
|
36
|
+
error.decoded_body = body
|
|
37
|
+
|
|
38
|
+
assert_equal body, error.decoded_body
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_api_error_decoded_body_raises_argument_error_for_non_hash
|
|
42
|
+
error = Resizing::APIError.new('test error')
|
|
43
|
+
|
|
44
|
+
assert_raises ArgumentError do
|
|
45
|
+
error.decoded_body = 'not a hash'
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_api_error_decoded_body_raises_argument_error_for_array
|
|
50
|
+
error = Resizing::APIError.new('test error')
|
|
51
|
+
|
|
52
|
+
assert_raises ArgumentError do
|
|
53
|
+
error.decoded_body = %w[not a hash]
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def test_configuration_error_can_be_raised_with_message
|
|
58
|
+
error = assert_raises Resizing::ConfigurationError do
|
|
59
|
+
raise Resizing::ConfigurationError, 'test configuration error'
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
assert_equal 'test configuration error', error.message
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def test_api_error_can_be_raised_with_message
|
|
66
|
+
error = assert_raises Resizing::APIError do
|
|
67
|
+
raise Resizing::APIError, 'test api error'
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
assert_equal 'test api error', error.message
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
module Resizing
|
|
6
|
+
class HttpClientableTest < Minitest::Test
|
|
7
|
+
class TestClient
|
|
8
|
+
include HttpClientable
|
|
9
|
+
|
|
10
|
+
def config
|
|
11
|
+
@config ||= Configuration.new(
|
|
12
|
+
image_host: 'https://image.example.com',
|
|
13
|
+
project_id: 'test_project',
|
|
14
|
+
secret_token: 'test_token'
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def setup
|
|
20
|
+
@client = TestClient.new
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def teardown
|
|
24
|
+
# NOP
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_http_client_initialization
|
|
28
|
+
http_client = @client.http_client
|
|
29
|
+
|
|
30
|
+
assert_instance_of Faraday::Connection, http_client
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_http_client_has_open_timeout
|
|
34
|
+
http_client = @client.http_client
|
|
35
|
+
|
|
36
|
+
assert_equal @client.config.open_timeout, http_client.options[:open_timeout]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_http_client_has_response_timeout
|
|
40
|
+
http_client = @client.http_client
|
|
41
|
+
|
|
42
|
+
assert_equal @client.config.response_timeout, http_client.options[:timeout]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_http_client_is_cached
|
|
46
|
+
http_client1 = @client.http_client
|
|
47
|
+
http_client2 = @client.http_client
|
|
48
|
+
|
|
49
|
+
assert_equal http_client1.object_id, http_client2.object_id
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_handle_faraday_error_yields_block
|
|
53
|
+
result = @client.handle_faraday_error { 'test_result' }
|
|
54
|
+
|
|
55
|
+
assert_equal 'test_result', result
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_handle_faraday_error_catches_timeout_error
|
|
59
|
+
assert_raises Resizing::APIError do
|
|
60
|
+
@client.handle_faraday_error do
|
|
61
|
+
raise Faraday::TimeoutError, 'timeout'
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_handle_timeout_error_raises_api_error
|
|
67
|
+
error = Faraday::TimeoutError.new('test timeout')
|
|
68
|
+
|
|
69
|
+
assert_raises Resizing::APIError do
|
|
70
|
+
@client.handle_timeout_error(error)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def test_handle_timeout_error_message_includes_error_info
|
|
75
|
+
error = Faraday::TimeoutError.new('test timeout')
|
|
76
|
+
|
|
77
|
+
begin
|
|
78
|
+
@client.handle_timeout_error(error)
|
|
79
|
+
rescue Resizing::APIError => e
|
|
80
|
+
assert_includes e.message, 'TimeoutError'
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
module Resizing
|
|
6
|
+
class MockClientTest < Minitest::Test
|
|
7
|
+
def setup
|
|
8
|
+
@client = Resizing::MockClient.new
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def teardown
|
|
12
|
+
# NOP
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_post_returns_parsed_json
|
|
16
|
+
VCR.use_cassette('client/post', record: :none) do
|
|
17
|
+
result = @client.post(nil)
|
|
18
|
+
|
|
19
|
+
assert_instance_of Hash, result
|
|
20
|
+
assert result.key?('id')
|
|
21
|
+
assert result.key?('public_id')
|
|
22
|
+
assert result.key?('latest_version_id')
|
|
23
|
+
assert result.key?('latest_etag')
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_put_returns_parsed_json_with_modified_name
|
|
28
|
+
VCR.use_cassette('client/put', record: :none) do
|
|
29
|
+
name = 'test-image-123'
|
|
30
|
+
result = @client.put(name, nil, {})
|
|
31
|
+
|
|
32
|
+
assert_instance_of Hash, result
|
|
33
|
+
assert_equal name, result['id']
|
|
34
|
+
assert_includes result['public_id'], name
|
|
35
|
+
assert result.key?('latest_version_id')
|
|
36
|
+
assert result.key?('latest_etag')
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def test_delete_returns_parsed_json_with_modified_name
|
|
41
|
+
VCR.use_cassette('client/delete', record: :none) do
|
|
42
|
+
name = 'delete-test-image'
|
|
43
|
+
result = @client.delete(name)
|
|
44
|
+
|
|
45
|
+
assert_instance_of Hash, result
|
|
46
|
+
assert_equal name, result['id']
|
|
47
|
+
assert_includes result['public_id'], name
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_metadata_returns_parsed_json_with_modified_name
|
|
52
|
+
VCR.use_cassette('client/metadata', record: :none) do
|
|
53
|
+
name = 'metadata-test-image'
|
|
54
|
+
result = @client.metadata(name)
|
|
55
|
+
|
|
56
|
+
assert_instance_of Hash, result
|
|
57
|
+
assert_equal name, result['id']
|
|
58
|
+
# The cassette contains a fixed public_id, so we just check it exists
|
|
59
|
+
assert result.key?('public_id')
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_post_response_contains_expected_fields
|
|
64
|
+
VCR.use_cassette('client/post', record: :none) do
|
|
65
|
+
result = @client.post(nil)
|
|
66
|
+
|
|
67
|
+
# Verify response structure
|
|
68
|
+
assert result['id'].is_a?(String)
|
|
69
|
+
assert result['public_id'].is_a?(String)
|
|
70
|
+
assert result['latest_version_id'].is_a?(String)
|
|
71
|
+
assert result['latest_etag'].is_a?(String)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -30,7 +30,7 @@ module Resizing
|
|
|
30
30
|
|
|
31
31
|
def test_expect_equal_identifier
|
|
32
32
|
public_id = Resizing::PublicId.new @public_id_as_string
|
|
33
|
-
assert_equal @public_id_as_string.gsub(
|
|
33
|
+
assert_equal @public_id_as_string.gsub(%r{/v.*$}, ''), public_id.identifier
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
def test_expect_equal_public_id
|