Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.6.4
* Will use POST instead of GET when retrieving result.

## 1.6.3
* Add minimum TLS 1.2 version to curl options as protocol negotiation on certain openssl/libcurl versions is flaky.

Expand Down
2 changes: 1 addition & 1 deletion lib/Tinify.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tinify;

const VERSION = "1.6.3";
const VERSION = "1.6.4";

class Tinify {
private static $key = NULL;
Expand Down
5 changes: 4 additions & 1 deletion lib/Tinify/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public function transform($options) {
}

public function result() {
$response = Tinify::getClient()->request("get", $this->url, $this->commands);
$has_commands = !empty($this->commands);
$method = $has_commands ? "post" : "get";
$body = $has_commands ? $this->commands : null;
$response = Tinify::getClient()->request($method, $this->url, $body);
return new Result($response->headers, $response->body);
}

Expand Down
26 changes: 26 additions & 0 deletions test/TinifySourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@ public function testResultShouldReturnResult() {
));

$this->assertInstanceOf("Tinify\Result", Tinify\Source::fromBuffer("png file")->result());
$this->assertSame("GET", CurlMock::last(CURLOPT_CUSTOMREQUEST));
}

/**
* When request does not contain commands, it should use method GET
* when it contains commands, it should have a body and method POST
*/
public function testResultWithCommandsShouldReturnResultUsingPost() {
Tinify\setKey("valid");

CurlMock::register("https://api.tinify.com/shrink", array(
"status" => 201,
"headers" => array("Location" => "https://api.tinify.com/some/location"),
));

CurlMock::register("https://api.tinify.com/some/location", array(
"status" => 200, "body" => "resized file"
));

$source = Tinify\Source::fromBuffer("png file")->resize(array("width" => 400));
$result = $source->result();

$this->assertInstanceOf("Tinify\Result", $result);
$this->assertSame("POST", CurlMock::last(CURLOPT_CUSTOMREQUEST));
$this->assertSame('{"resize":{"width":400}}', CurlMock::last(CURLOPT_POSTFIELDS));
}

public function testPreserveShouldReturnSource() {
Expand Down Expand Up @@ -162,6 +187,7 @@ public function testPreserveShouldReturnSourceWithData() {

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

public function testPreserveShouldReturnSourceWithDataForArray() {
Expand Down
Loading