Skip to content

Commit 763414d

Browse files
Use HTTP Post when retrieving result (#41)
* Use http post when retrieving result * 1.6.4 and changelog * When commands is empty, do a get with body null * Change condition to use empty * fix: correct HTTP method logic for Source result requests - Fix inverted boolean logic for has_commands check - Ensure GET is used when no commands are present - Ensure POST is used when commands are present - Add comprehensive tests for both GET and POST scenarios
1 parent 0b09830 commit 763414d

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 1.6.4
2+
* Will use POST instead of GET when retrieving result.
3+
14
## 1.6.3
25
* Add minimum TLS 1.2 version to curl options as protocol negotiation on certain openssl/libcurl versions is flaky.
36

lib/Tinify/Source.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ public function transform($options) {
5353
}
5454

5555
public function result() {
56-
$response = Tinify::getClient()->request("get", $this->url, $this->commands);
56+
$has_commands = !empty($this->commands);
57+
$method = $has_commands ? "post" : "get";
58+
$body = $has_commands ? $this->commands : null;
59+
$response = Tinify::getClient()->request($method, $this->url, $body);
5760
return new Result($response->headers, $response->body);
5861
}
5962

test/TinifySourceTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,31 @@ public function testResultShouldReturnResult() {
132132
));
133133

134134
$this->assertInstanceOf("Tinify\Result", Tinify\Source::fromBuffer("png file")->result());
135+
$this->assertSame("GET", CurlMock::last(CURLOPT_CUSTOMREQUEST));
136+
}
137+
138+
/**
139+
* When request does not contain commands, it should use method GET
140+
* when it contains commands, it should have a body and method POST
141+
*/
142+
public function testResultWithCommandsShouldReturnResultUsingPost() {
143+
Tinify\setKey("valid");
144+
145+
CurlMock::register("https://api.tinify.com/shrink", array(
146+
"status" => 201,
147+
"headers" => array("Location" => "https://api.tinify.com/some/location"),
148+
));
149+
150+
CurlMock::register("https://api.tinify.com/some/location", array(
151+
"status" => 200, "body" => "resized file"
152+
));
153+
154+
$source = Tinify\Source::fromBuffer("png file")->resize(array("width" => 400));
155+
$result = $source->result();
156+
157+
$this->assertInstanceOf("Tinify\Result", $result);
158+
$this->assertSame("POST", CurlMock::last(CURLOPT_CUSTOMREQUEST));
159+
$this->assertSame('{"resize":{"width":400}}', CurlMock::last(CURLOPT_POSTFIELDS));
135160
}
136161

137162
public function testPreserveShouldReturnSource() {
@@ -162,6 +187,7 @@ public function testPreserveShouldReturnSourceWithData() {
162187

163188
$this->assertSame("copyrighted file", Tinify\Source::fromBuffer("png file")->preserve("copyright", "location")->toBuffer());
164189
$this->assertSame("{\"preserve\":[\"copyright\",\"location\"]}", CurlMock::last(CURLOPT_POSTFIELDS));
190+
$this->assertSame("POST", CurlMock::last(CURLOPT_CUSTOMREQUEST));
165191
}
166192

167193
public function testPreserveShouldReturnSourceWithDataForArray() {

0 commit comments

Comments
 (0)