From 1166b910b6fe67be413d12ba75b8a555b921e330 Mon Sep 17 00:00:00 2001 From: Dmitry Patsura Date: Tue, 23 Jul 2019 21:13:21 +0300 Subject: [PATCH] Feature: More tests --- tests/functional/CurlTest.php | 57 +++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/tests/functional/CurlTest.php b/tests/functional/CurlTest.php index 44b04a9..54e8b55 100644 --- a/tests/functional/CurlTest.php +++ b/tests/functional/CurlTest.php @@ -2,22 +2,32 @@ namespace Test\Functional; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; use SocialConnect\HttpClient\Curl; use SocialConnect\HttpClient\Request; use SocialConnect\HttpClient\StreamFactory; class CurlTest extends \PHPUnit\Framework\TestCase { - public function testGetMethod() + protected function executeRequest(RequestInterface $request) { $client = new Curl(); - $response = $client->sendRequest( - new Request('GET', 'http://127.0.0.1:5555/test-get') - ); + return $client->sendRequest($request); + } + protected function assertOk(ResponseInterface $response) + { parent::assertSame(200, $response->getStatusCode()); parent::assertSame('OK', $response->getReasonPhrase()); - parent::assertSame($response->getHeaderLine('content-type'), 'application/json'); + parent::assertSame('application/json', $response->getHeaderLine('content-type')); + } + + public function testGetMethod() + { + $response = $this->executeRequest(new Request('GET', 'http://127.0.0.1:5555/test-get')); + + $this->assertOk($response); $content = $response->getBody()->getContents(); $result = json_decode($content, true); @@ -36,11 +46,9 @@ public function testPostMethod() $streamFactory->createStream('payload') ); - $client = new Curl(); - $response = $client->sendRequest($request); + $response = $this->executeRequest($request); - parent::assertSame(200, $response->getStatusCode()); - parent::assertSame('OK', $response->getReasonPhrase()); + $this->assertOk($response); parent::assertSame($response->getHeaderLine('content-type'), 'application/json'); $content = $response->getBody()->getContents(); @@ -51,16 +59,33 @@ public function testPostMethod() parent::assertEquals($result['headers']['x-my-header'], ['5']); } - public function testDeleteMethod() + public function testPutMethod() { - $client = new Curl(); - $response = $client->sendRequest( - new Request('DELETE', 'http://127.0.0.1:5555/test-delete') + $streamFactory = new StreamFactory(); + + $request = new Request('PUT', 'http://127.0.0.1:5555/test-put'); + $request = $request->withHeader('X-MY-Header', '5'); + $request = $request->withBody( + $streamFactory->createStream('payload') ); - parent::assertSame(200, $response->getStatusCode()); - parent::assertSame('OK', $response->getReasonPhrase()); - parent::assertSame($response->getHeaderLine('content-type'), 'application/json'); + $response = $this->executeRequest($request); + + $this->assertOk($response); + + $content = $response->getBody()->getContents(); + $result = json_decode($content, true); + + parent::assertEquals($result['method'], 'PUT'); + parent::assertEquals($result['uri'], 'http://127.0.0.1:5555/test-put'); + parent::assertEquals($result['headers']['x-my-header'], ['5']); + } + + public function testDeleteMethod() + { + $response = $this->executeRequest(new Request('DELETE', 'http://127.0.0.1:5555/test-delete')); + + $this->assertOk($response); $content = $response->getBody()->getContents(); $result = json_decode($content, true);