Skip to content

Commit 2899b54

Browse files
committed
fix(source): :result uses post when body is given
1 parent a5564ca commit 2899b54

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

lib/tinify/source.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ def store(options)
4343
end
4444

4545
def result
46-
response = Tinify.client.request(:get, @url, @commands)
46+
response = if @commands&.empty?
47+
Tinify.client.request(:get, @url)
48+
else
49+
Tinify.client.request(:post, @url, @commands)
50+
end
51+
4752
Result.new(response.headers, response.body).freeze
4853
end
4954

test/tinify_source_test.rb

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,35 @@
153153
body: "compressed file")
154154
end
155155

156-
it "should return result" do
157-
assert_kind_of Tinify::Result, Tinify::Source.from_buffer("png file").result
156+
it 'has a `Tinify::Result`' do
157+
assert_kind_of(Tinify::Result,
158+
Tinify::Source.from_buffer("png file").result)
159+
end
160+
161+
it 'has result data' do
162+
assert_equal('compressed file',
163+
Tinify::Source.from_buffer("png file").result.data)
164+
end
165+
166+
it 'is a :get request' do
167+
Tinify::Source.from_buffer("png file").result
168+
169+
assert_requested(:get, "https://api.tinify.com/some/location",
170+
times: 1)
171+
end
172+
173+
it 'is a :post request' do
174+
stub_request(:post, "https://api.tinify.com/some/location")
175+
.with(basic_auth: ['api', 'valid'],
176+
body: '{"preserve":["copyright","location"]}')
177+
.to_return(status: 200, body: "copyrighted file")
178+
179+
Tinify::Source.from_buffer("png file")
180+
.preserve("copyright", "location")
181+
.result
182+
183+
assert_requested(:post, "https://api.tinify.com/some/location",
184+
times: 1)
158185
end
159186
end
160187

@@ -167,7 +194,7 @@
167194
headers: { Location: "https://api.tinify.com/some/location" },
168195
body: '{}')
169196

170-
stub_request(:get, "https://api.tinify.com/some/location")
197+
stub_request(:post, "https://api.tinify.com/some/location")
171198
.with(
172199
basic_auth: ['api', 'valid'],
173200
body: '{"preserve":["copyright","location"]}')
@@ -192,7 +219,7 @@
192219
end
193220

194221
it "should include other options if set" do
195-
stub_request(:get, "https://api.tinify.com/some/location")
222+
stub_request(:post, "https://api.tinify.com/some/location")
196223
.with(
197224
basic_auth: ['api', 'valid'],
198225
body: '{"resize":{"width":400},"preserve":["copyright","location"]}')
@@ -214,7 +241,7 @@
214241
headers: { Location: "https://api.tinify.com/some/location" },
215242
body: '{}')
216243

217-
stub_request(:get, "https://api.tinify.com/some/location")
244+
stub_request(:post, "https://api.tinify.com/some/location")
218245
.with(
219246
basic_auth: ['api', 'valid'],
220247
body: '{"resize":{"width":400}}')
@@ -241,7 +268,7 @@
241268
headers: { Location: "https://api.tinify.com/some/location" },
242269
body: '{}')
243270

244-
stub_request(:get, "https://api.tinify.com/some/location")
271+
stub_request(:post, "https://api.tinify.com/some/location")
245272
.with(
246273
basic_auth: ['api', 'valid'],
247274
body: '{"convert":{"type":["image/webp"]}}')
@@ -268,7 +295,7 @@
268295
headers: { Location: "https://api.tinify.com/some/location" },
269296
body: '{}')
270297

271-
stub_request(:get, "https://api.tinify.com/some/location")
298+
stub_request(:post, "https://api.tinify.com/some/location")
272299
.with(
273300
basic_auth: ['api', 'valid'],
274301
body: '{"transform":{"color":"black"}}')
@@ -286,11 +313,9 @@
286313
end
287314

288315
it "should include other options if set" do
289-
290-
stub_request(:get, "https://api.tinify.com/some/location").
291-
with(:body => '{"convert":{"type":["image/webp"]},"transform":{"color":"black"}}',
292-
).
293-
to_return(:status => 200, :body => "trans-formed-and-coded", :headers => {})
316+
stub_request(:post, "https://api.tinify.com/some/location")
317+
.with(:body => '{"convert":{"type":["image/webp"]},"transform":{"color":"black"}}')
318+
.to_return(:status => 200, :body => "trans-formed-and-coded", :headers => {})
294319

295320
result = Tinify::Source.from_buffer("png file").convert(type: ["image/webp"]).transform(color: "black")
296321
assert_equal "trans-formed-and-coded", result.to_buffer

0 commit comments

Comments
 (0)