diff --git a/src/Client.php b/src/Client.php index e06b30a..5201af1 100644 --- a/src/Client.php +++ b/src/Client.php @@ -157,18 +157,30 @@ public function sendRequest(RequestInterface $request) curl_setopt($this->handle, CURLOPT_URL, (string) $request->getUri()); curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, [$this, 'headerFunction']); - $method = $request->getMethod(); + foreach ($request->getHeaders() as $name => $values) { + $headers[] = $name . ': ' . implode(', ', $values); + } + if (isset($headers)) { + curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers); + } - if ($method == HttpMethod::HEAD) { - curl_setopt($this->handle, CURLOPT_NOBODY, true); - } elseif ($method == HttpMethod::POST) { - curl_setopt($this->handle, CURLOPT_POST, true); - $body = $request->getBody(); - if (!empty($body)) { - curl_setopt($this->handle, CURLOPT_POSTFIELDS, $body); - } - } elseif (in_array($method, [HttpMethod::PUT, HttpMethod::PATCH, HttpMethod::DELETE])) { - curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method); + $method = $request->getMethod(); + switch($method) { + case HttpMethod::HEAD: + curl_setopt($this->handle, CURLOPT_NOBODY, true); + break; + case HttpMethod::POST: + curl_setopt($this->handle, CURLOPT_POST, true); + $body = $request->getBody(); + if (!empty($body)) { + curl_setopt($this->handle, CURLOPT_POSTFIELDS, $body); + } + break; + case HttpMethod::PUT: + case HttpMethod::PATCH: + case HttpMethod::DELETE: + curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method); + break; } $ret = curl_exec($this->handle); diff --git a/src/ClientAwareTrait.php b/src/ClientAwareTrait.php new file mode 100644 index 0000000..98a0b71 --- /dev/null +++ b/src/ClientAwareTrait.php @@ -0,0 +1,49 @@ + + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +namespace Matricali\Http; + +use Matricali\Http\ClientInterface as HttpClientInterface; + +/** + * @author Gabriel Polverini + */ +trait ClientAwareTrait +{ + /** + * The client instance. + * + * @var HttpClientInterface + */ + protected $httpClient; + + /** + * setHttpClient. + * + * @param HttpClientInterface $httpClient + */ + public function setHttpClient(HttpClientInterface $httpClient) + { + $this->httpClient = $httpClient; + } +} diff --git a/src/HttpMethod.php b/src/HttpMethod.php index 8d4e998..cfe3602 100644 --- a/src/HttpMethod.php +++ b/src/HttpMethod.php @@ -24,7 +24,7 @@ namespace Matricali\Http; /** - * @author Gabriel Polverini + * @author Gabriel Polverini */ abstract class HttpMethod extends Enum { diff --git a/src/HttpStatusCode.php b/src/HttpStatusCode.php index ff62bca..f40af14 100644 --- a/src/HttpStatusCode.php +++ b/src/HttpStatusCode.php @@ -24,7 +24,7 @@ namespace Matricali\Http; /** - * @author Gabriel Polverini + * @author Gabriel Polverini */ abstract class HttpStatusCode extends Enum { diff --git a/src/Request.php b/src/Request.php index dca033b..cc7e414 100644 --- a/src/Request.php +++ b/src/Request.php @@ -44,8 +44,12 @@ class Request extends AbstractMessage implements RequestInterface * @param string $body Body to send in the request.- * @throws \InvalidArgumentException for invalid HTTP methods. */ - public function __construct($method = HttpMethod::GET, $uri = '', array $headers = array(), $body = null) - { + public function __construct( + $method = HttpMethod::GET, + $uri = '', + array $headers = array(), + $body = null + ) { $this->validateMethod($method); parent::__construct('1.1', $headers, $body); $this->method = $method; diff --git a/src/Response.php b/src/Response.php index bb374d0..b4f906e 100644 --- a/src/Response.php +++ b/src/Response.php @@ -54,8 +54,12 @@ class Response extends AbstractMessage implements ResponseInterface * @param string $protocolVersion HTTP protocol version * @throws \InvalidArgumentException for invalid HTTP methods. */ - public function __construct($body = '', $statusCode = 200, array $headers = array(), $protocolVersion = '1.1') - { + public function __construct( + $body = '', + $statusCode = HttpStatusCode::HTTP_OK, + array $headers = array(), + $protocolVersion = '1.1' + ) { $this->validateStatusCode($statusCode); parent::__construct($protocolVersion, $headers, $body); $this->statusCode = $statusCode; diff --git a/src/Scheme.php b/src/Scheme.php index 8e79260..3e06c9b 100644 --- a/src/Scheme.php +++ b/src/Scheme.php @@ -24,7 +24,7 @@ namespace Matricali\Http; /** - * @author Gabriel Polverini + * @author Gabriel Polverini */ abstract class Scheme extends Enum { diff --git a/tests/ClientAwareTraitTest.php b/tests/ClientAwareTraitTest.php new file mode 100644 index 0000000..86b444c --- /dev/null +++ b/tests/ClientAwareTraitTest.php @@ -0,0 +1,55 @@ + + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +namespace Matricali\Http; + +use PHPUnit\Framework\TestCase; +use Prophecy\Argument; + +/** + * @author Gabriel Polverini + * + * @group Traits + */ +class ClientAwareTraitTest extends TestCase +{ + protected $trait; + + public function setUp() + { + $this->trait = $this->getMockForTrait('Matricali\Http\ClientAwareTrait'); + } + + /** + * @test + */ + public function testSetHttpClient() + { + $httpClient = new Client(); + $this->assertNull($this->trait->setHttpClient($httpClient)); + $reflection = new \ReflectionProperty(get_class($this->trait), 'httpClient'); + $reflection->setAccessible(true); + + $this->assertInstanceOf('Matricali\Http\ClientInterface', $reflection->getValue($this->trait)); + } +} \ No newline at end of file diff --git a/tests/ClientTest.php b/tests/ClientTest.php index de84ae1..db7b464 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -27,7 +27,7 @@ use Prophecy\Argument; /** - * @author Gabriel Polverini + * @author Gabriel Polverini * * @group Client */ @@ -44,6 +44,7 @@ public function testSendRequest() $request = $this->prophesize('Psr\Http\Message\RequestInterface'); $request->getUri()->willReturn('http://404.php.net/'); $request->getMethod()->willReturn(HttpMethod::GET); + $request->getHeaders()->willReturn([]); $client->sendRequest($request->reveal()); } @@ -156,4 +157,20 @@ public function testParseHeaderNotArray() $this->assertFalse($method->invoke($client, 123)); } + + /** + * @test + */ + public function testBasicPostWithHeaders() + { + $client = new Client(); + $headers = [ + 'Accept' => 'application/json', + 'Content-Type' => 'text/xml;charset=UTF-8', + ]; + $response = $client->post('http://www.google.com/', 'test=test&a=b', $headers); + $this->assertEquals(405, $response->getStatusCode()); + $this->assertEquals('1.1', $response->getProtocolVersion()); + $this->assertNotEmpty($response->getBody()); + } } diff --git a/tests/HttpMethodTest.php b/tests/HttpMethodTest.php index 85234fa..d486da5 100644 --- a/tests/HttpMethodTest.php +++ b/tests/HttpMethodTest.php @@ -26,7 +26,7 @@ use PHPUnit\Framework\TestCase; /** - * @author Gabriel Polverini + * @author Gabriel Polverini * * @group Enums */ diff --git a/tests/HttpStatusCodeTest.php b/tests/HttpStatusCodeTest.php index b024777..e815821 100644 --- a/tests/HttpStatusCodeTest.php +++ b/tests/HttpStatusCodeTest.php @@ -26,7 +26,7 @@ use PHPUnit\Framework\TestCase; /** - * @author Gabriel Polverini + * @author Gabriel Polverini * * @group Enums */ diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 5a38d46..58f62fc 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -27,7 +27,7 @@ use Prophecy\Argument; /** - * @author Gabriel Polverini + * @author Gabriel Polverini * * @group Request */ diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 03a853c..55885ae 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -26,7 +26,7 @@ use PHPUnit\Framework\TestCase; /** - * @author Gabriel Polverini + * @author Gabriel Polverini * * @group Response */ diff --git a/tests/SchemeTest.php b/tests/SchemeTest.php index 71a0913..9ec3f20 100644 --- a/tests/SchemeTest.php +++ b/tests/SchemeTest.php @@ -26,7 +26,7 @@ use PHPUnit\Framework\TestCase; /** - * @author Gabriel Polverini + * @author Gabriel Polverini * * @group Enums */ diff --git a/tests/UriTest.php b/tests/UriTest.php index 0b79b93..966a691 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -27,7 +27,7 @@ use PHPUnit\Framework\TestCase; /** - * @author Gabriel Polverini + * @author Gabriel Polverini * * @group Uri */