From 677953067f2bb5502a70f0d004f1ac844b18a128 Mon Sep 17 00:00:00 2001 From: emkael Date: Mon, 16 Jan 2017 22:04:43 +0100 Subject: * Facebook support --- .../tests/HttpClients/AbstractTestHttpClient.php | 60 ++++ .../HttpClients/FacebookCurlHttpClientTest.php | 334 +++++++++++++++++++++ .../HttpClients/FacebookGuzzleHttpClientTest.php | 143 +++++++++ .../HttpClients/FacebookStreamHttpClientTest.php | 134 +++++++++ 4 files changed, 671 insertions(+) create mode 100644 lib/facebook-graph-sdk/tests/HttpClients/AbstractTestHttpClient.php create mode 100644 lib/facebook-graph-sdk/tests/HttpClients/FacebookCurlHttpClientTest.php create mode 100644 lib/facebook-graph-sdk/tests/HttpClients/FacebookGuzzleHttpClientTest.php create mode 100644 lib/facebook-graph-sdk/tests/HttpClients/FacebookStreamHttpClientTest.php (limited to 'lib/facebook-graph-sdk/tests/HttpClients') diff --git a/lib/facebook-graph-sdk/tests/HttpClients/AbstractTestHttpClient.php b/lib/facebook-graph-sdk/tests/HttpClients/AbstractTestHttpClient.php new file mode 100644 index 0000000..269b235 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/HttpClients/AbstractTestHttpClient.php @@ -0,0 +1,60 @@ + '"9d86b21aa74d74e574bbb35ba13524a52deb96e3"', + 'Content-Type' => 'text/javascript; charset=UTF-8', + 'X-FB-Rev' => '9244768', + 'Pragma' => 'no-cache', + 'Expires' => 'Sat, 01 Jan 2000 00:00:00 GMT', + 'Connection' => 'close', + 'Date' => 'Mon, 19 May 2014 18:37:17 GMT', + 'X-FB-Debug' => '02QQiffE7JG2rV6i/Agzd0gI2/OOQ2lk5UW0=', + 'Content-Length' => '29', + 'Cache-Control' => 'private, no-cache, no-store, must-revalidate', + 'Access-Control-Allow-Origin' => '*', + ]; +} diff --git a/lib/facebook-graph-sdk/tests/HttpClients/FacebookCurlHttpClientTest.php b/lib/facebook-graph-sdk/tests/HttpClients/FacebookCurlHttpClientTest.php new file mode 100644 index 0000000..4cf31d3 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/HttpClients/FacebookCurlHttpClientTest.php @@ -0,0 +1,334 @@ +curlMock = m::mock('Facebook\HttpClients\FacebookCurl'); + $this->curlClient = new FacebookCurlHttpClient($this->curlMock); + } + + public function testCanOpenGetCurlConnection() + { + $this->curlMock + ->shouldReceive('init') + ->once() + ->andReturn(null); + $this->curlMock + ->shouldReceive('setoptArray') + ->with(m::on(function ($arg) { + + // array_diff() will sometimes trigger error on child-arrays + if (['X-Foo-Header: X-Bar'] !== $arg[CURLOPT_HTTPHEADER]) { + return false; + } + unset($arg[CURLOPT_HTTPHEADER]); + + $caInfo = array_diff($arg, [ + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_URL => 'http://foo.com', + CURLOPT_CONNECTTIMEOUT => 10, + CURLOPT_TIMEOUT => 123, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HEADER => true, + CURLOPT_SSL_VERIFYHOST => 2, + CURLOPT_SSL_VERIFYPEER => true, + ]); + + if (count($caInfo) !== 1) { + return false; + } + + if (1 !== preg_match('/.+\/certs\/DigiCertHighAssuranceEVRootCA\.pem$/', $caInfo[CURLOPT_CAINFO])) { + return false; + } + + return true; + })) + ->once() + ->andReturn(null); + + $this->curlClient->openConnection('http://foo.com', 'GET', 'foo_body', ['X-Foo-Header' => 'X-Bar'], 123); + } + + public function testCanOpenCurlConnectionWithPostBody() + { + $this->curlMock + ->shouldReceive('init') + ->once() + ->andReturn(null); + $this->curlMock + ->shouldReceive('setoptArray') + ->with(m::on(function ($arg) { + + // array_diff() will sometimes trigger error on child-arrays + if ([] !== $arg[CURLOPT_HTTPHEADER]) { + return false; + } + unset($arg[CURLOPT_HTTPHEADER]); + + $caInfo = array_diff($arg, [ + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_URL => 'http://bar.com', + CURLOPT_CONNECTTIMEOUT => 10, + CURLOPT_TIMEOUT => 60, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HEADER => true, + CURLOPT_SSL_VERIFYHOST => 2, + CURLOPT_SSL_VERIFYPEER => true, + CURLOPT_POSTFIELDS => 'baz=bar', + ]); + + if (count($caInfo) !== 1) { + return false; + } + + if (1 !== preg_match('/.+\/certs\/DigiCertHighAssuranceEVRootCA\.pem$/', $caInfo[CURLOPT_CAINFO])) { + return false; + } + + return true; + })) + ->once() + ->andReturn(null); + + $this->curlClient->openConnection('http://bar.com', 'POST', 'baz=bar', [], 60); + } + + public function testCanCloseConnection() + { + $this->curlMock + ->shouldReceive('close') + ->once() + ->andReturn(null); + + $this->curlClient->closeConnection(); + } + + public function testIsolatesTheHeaderAndBody() + { + $this->curlMock + ->shouldReceive('getinfo') + ->with(CURLINFO_HEADER_SIZE) + ->once() + ->andReturn(strlen($this->fakeRawHeader)); + $this->curlMock + ->shouldReceive('version') + ->once() + ->andReturn(['version_number' => self::CURL_VERSION_STABLE]); + $this->curlMock + ->shouldReceive('exec') + ->once() + ->andReturn($this->fakeRawHeader . $this->fakeRawBody); + + $this->curlClient->sendRequest(); + list($rawHeader, $rawBody) = $this->curlClient->extractResponseHeadersAndBody(); + + $this->assertEquals($rawHeader, trim($this->fakeRawHeader)); + $this->assertEquals($rawBody, $this->fakeRawBody); + } + + public function testProperlyHandlesProxyHeaders() + { + $rawHeader = $this->fakeRawProxyHeader . $this->fakeRawHeader; + $this->curlMock + ->shouldReceive('getinfo') + ->with(CURLINFO_HEADER_SIZE) + ->once() + ->andReturn(mb_strlen($rawHeader)); + $this->curlMock + ->shouldReceive('version') + ->once() + ->andReturn(['version_number' => self::CURL_VERSION_STABLE]); + $this->curlMock + ->shouldReceive('exec') + ->once() + ->andReturn($rawHeader . $this->fakeRawBody); + + $this->curlClient->sendRequest(); + list($rawHeaders, $rawBody) = $this->curlClient->extractResponseHeadersAndBody(); + + $this->assertEquals($rawHeaders, trim($rawHeader)); + $this->assertEquals($rawBody, $this->fakeRawBody); + } + + public function testProperlyHandlesProxyHeadersWithCurlBug() + { + $rawHeader = $this->fakeRawProxyHeader . $this->fakeRawHeader; + $this->curlMock + ->shouldReceive('getinfo') + ->with(CURLINFO_HEADER_SIZE) + ->once() + ->andReturn(mb_strlen($this->fakeRawHeader)); // Mimic bug that doesn't count proxy header + $this->curlMock + ->shouldReceive('version') + ->once() + ->andReturn(['version_number' => self::CURL_VERSION_BUGGY]); + $this->curlMock + ->shouldReceive('exec') + ->once() + ->andReturn($rawHeader . $this->fakeRawBody); + + $this->curlClient->sendRequest(); + list($rawHeaders, $rawBody) = $this->curlClient->extractResponseHeadersAndBody(); + + $this->assertEquals($rawHeaders, trim($rawHeader)); + $this->assertEquals($rawBody, $this->fakeRawBody); + } + + public function testProperlyHandlesProxyHeadersWithCurlBug2() + { + $rawHeader = $this->fakeRawProxyHeader2 . $this->fakeRawHeader; + $this->curlMock + ->shouldReceive('getinfo') + ->with(CURLINFO_HEADER_SIZE) + ->once() + ->andReturn(mb_strlen($this->fakeRawHeader)); // Mimic bug that doesn't count proxy header + $this->curlMock + ->shouldReceive('version') + ->once() + ->andReturn(['version_number' => self::CURL_VERSION_BUGGY]); + $this->curlMock + ->shouldReceive('exec') + ->once() + ->andReturn($rawHeader . $this->fakeRawBody); + + $this->curlClient->sendRequest(); + list($rawHeaders, $rawBody) = $this->curlClient->extractResponseHeadersAndBody(); + + $this->assertEquals($rawHeaders, trim($rawHeader)); + $this->assertEquals($rawBody, $this->fakeRawBody); + } + + public function testProperlyHandlesRedirectHeaders() + { + $rawHeader = $this->fakeRawRedirectHeader . $this->fakeRawHeader; + $this->curlMock + ->shouldReceive('getinfo') + ->with(CURLINFO_HEADER_SIZE) + ->once() + ->andReturn(mb_strlen($rawHeader)); + $this->curlMock + ->shouldReceive('version') + ->once() + ->andReturn(['version_number' => self::CURL_VERSION_STABLE]); + $this->curlMock + ->shouldReceive('exec') + ->once() + ->andReturn($rawHeader . $this->fakeRawBody); + + $this->curlClient->sendRequest(); + list($rawHeaders, $rawBody) = $this->curlClient->extractResponseHeadersAndBody(); + + $this->assertEquals($rawHeaders, trim($rawHeader)); + $this->assertEquals($rawBody, $this->fakeRawBody); + } + + public function testCanSendNormalRequest() + { + $this->curlMock + ->shouldReceive('init') + ->once() + ->andReturn(null); + $this->curlMock + ->shouldReceive('setoptArray') + ->once() + ->andReturn(null); + $this->curlMock + ->shouldReceive('exec') + ->once() + ->andReturn($this->fakeRawHeader . $this->fakeRawBody); + $this->curlMock + ->shouldReceive('errno') + ->once() + ->andReturn(null); + $this->curlMock + ->shouldReceive('getinfo') + ->with(CURLINFO_HEADER_SIZE) + ->once() + ->andReturn(mb_strlen($this->fakeRawHeader)); + $this->curlMock + ->shouldReceive('version') + ->once() + ->andReturn(['version_number' => self::CURL_VERSION_STABLE]); + $this->curlMock + ->shouldReceive('close') + ->once() + ->andReturn(null); + + $response = $this->curlClient->send('http://foo.com/', 'GET', '', [], 60); + + $this->assertInstanceOf('Facebook\Http\GraphRawResponse', $response); + $this->assertEquals($this->fakeRawBody, $response->getBody()); + $this->assertEquals($this->fakeHeadersAsArray, $response->getHeaders()); + $this->assertEquals(200, $response->getHttpResponseCode()); + } + + /** + * @expectedException \Facebook\Exceptions\FacebookSDKException + */ + public function testThrowsExceptionOnClientError() + { + $this->curlMock + ->shouldReceive('init') + ->once() + ->andReturn(null); + $this->curlMock + ->shouldReceive('setoptArray') + ->once() + ->andReturn(null); + $this->curlMock + ->shouldReceive('exec') + ->once() + ->andReturn(false); + $this->curlMock + ->shouldReceive('errno') + ->once() + ->andReturn(123); + $this->curlMock + ->shouldReceive('error') + ->once() + ->andReturn('Foo error'); + + $this->curlClient->send('http://foo.com/', 'GET', '', [], 60); + } +} diff --git a/lib/facebook-graph-sdk/tests/HttpClients/FacebookGuzzleHttpClientTest.php b/lib/facebook-graph-sdk/tests/HttpClients/FacebookGuzzleHttpClientTest.php new file mode 100644 index 0000000..12eb36a --- /dev/null +++ b/lib/facebook-graph-sdk/tests/HttpClients/FacebookGuzzleHttpClientTest.php @@ -0,0 +1,143 @@ +guzzleMock = m::mock('GuzzleHttp\Client'); + $this->guzzleClient = new FacebookGuzzleHttpClient($this->guzzleMock); + } + + public function testCanSendNormalRequest() + { + $request = new Request('GET', 'http://foo.com'); + + $body = Stream::factory($this->fakeRawBody); + $response = new Response(200, $this->fakeHeadersAsArray, $body); + + $this->guzzleMock + ->shouldReceive('createRequest') + ->once() + ->with('GET', 'http://foo.com/', m::on(function ($arg) { + + // array_diff_assoc() will sometimes trigger error on child-arrays + if (['X-foo' => 'bar'] !== $arg['headers']) { + return false; + } + unset($arg['headers']); + + $caInfo = array_diff_assoc($arg, [ + 'body' => 'foo_body', + 'timeout' => 123, + 'connect_timeout' => 10, + ]); + + if (count($caInfo) !== 1) { + return false; + } + + if (1 !== preg_match('/.+\/certs\/DigiCertHighAssuranceEVRootCA\.pem$/', $caInfo['verify'])) { + return false; + } + + return true; + })) + ->andReturn($request); + $this->guzzleMock + ->shouldReceive('send') + ->once() + ->with($request) + ->andReturn($response); + + $response = $this->guzzleClient->send('http://foo.com/', 'GET', 'foo_body', ['X-foo' => 'bar'], 123); + + $this->assertInstanceOf('Facebook\Http\GraphRawResponse', $response); + $this->assertEquals($this->fakeRawBody, $response->getBody()); + $this->assertEquals($this->fakeHeadersAsArray, $response->getHeaders()); + $this->assertEquals(200, $response->getHttpResponseCode()); + } + + /** + * @expectedException \Facebook\Exceptions\FacebookSDKException + */ + public function testThrowsExceptionOnClientError() + { + $request = new Request('GET', 'http://foo.com'); + + $this->guzzleMock + ->shouldReceive('createRequest') + ->once() + ->with('GET', 'http://foo.com/', m::on(function ($arg) { + + // array_diff_assoc() will sometimes trigger error on child-arrays + if ([] !== $arg['headers']) { + return false; + } + unset($arg['headers']); + + $caInfo = array_diff_assoc($arg, [ + 'body' => 'foo_body', + 'timeout' => 60, + 'connect_timeout' => 10, + ]); + + if (count($caInfo) !== 1) { + return false; + } + + if (1 !== preg_match('/.+\/certs\/DigiCertHighAssuranceEVRootCA\.pem$/', $caInfo['verify'])) { + return false; + } + + return true; + })) + ->andReturn($request); + $this->guzzleMock + ->shouldReceive('send') + ->once() + ->with($request) + ->andThrow(new RequestException('Foo', $request)); + + $this->guzzleClient->send('http://foo.com/', 'GET', 'foo_body', [], 60); + } +} diff --git a/lib/facebook-graph-sdk/tests/HttpClients/FacebookStreamHttpClientTest.php b/lib/facebook-graph-sdk/tests/HttpClients/FacebookStreamHttpClientTest.php new file mode 100644 index 0000000..9102b08 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/HttpClients/FacebookStreamHttpClientTest.php @@ -0,0 +1,134 @@ +streamMock = m::mock('Facebook\HttpClients\FacebookStream'); + $this->streamClient = new FacebookStreamHttpClient($this->streamMock); + } + + public function testCanCompileHeader() + { + $headers = [ + 'X-foo' => 'bar', + 'X-bar' => 'faz', + ]; + $header = $this->streamClient->compileHeader($headers); + $this->assertEquals("X-foo: bar\r\nX-bar: faz", $header); + } + + public function testCanSendNormalRequest() + { + $this->streamMock + ->shouldReceive('streamContextCreate') + ->once() + ->with(m::on(function ($arg) { + if (!isset($arg['http']) || !isset($arg['ssl'])) { + return false; + } + + if ($arg['http'] !== [ + 'method' => 'GET', + 'header' => 'X-foo: bar', + 'content' => 'foo_body', + 'timeout' => 123, + 'ignore_errors' => true, + ] + ) { + return false; + } + + $caInfo = array_diff_assoc($arg['ssl'], [ + 'verify_peer' => true, + 'verify_peer_name' => true, + 'allow_self_signed' => true, + ]); + + if (count($caInfo) !== 1) { + return false; + } + + if (1 !== preg_match('/.+\/certs\/DigiCertHighAssuranceEVRootCA\.pem$/', $caInfo['cafile'])) { + return false; + } + + return true; + })) + ->andReturn(null); + $this->streamMock + ->shouldReceive('getResponseHeaders') + ->once() + ->andReturn(explode("\n", trim($this->fakeRawHeader))); + $this->streamMock + ->shouldReceive('fileGetContents') + ->once() + ->with('http://foo.com/') + ->andReturn($this->fakeRawBody); + + $response = $this->streamClient->send('http://foo.com/', 'GET', 'foo_body', ['X-foo' => 'bar'], 123); + + $this->assertInstanceOf('Facebook\Http\GraphRawResponse', $response); + $this->assertEquals($this->fakeRawBody, $response->getBody()); + $this->assertEquals($this->fakeHeadersAsArray, $response->getHeaders()); + $this->assertEquals(200, $response->getHttpResponseCode()); + } + + /** + * @expectedException \Facebook\Exceptions\FacebookSDKException + */ + public function testThrowsExceptionOnClientError() + { + $this->streamMock + ->shouldReceive('streamContextCreate') + ->once() + ->andReturn(null); + $this->streamMock + ->shouldReceive('getResponseHeaders') + ->once() + ->andReturn(null); + $this->streamMock + ->shouldReceive('fileGetContents') + ->once() + ->with('http://foo.com/') + ->andReturn(false); + + $this->streamClient->send('http://foo.com/', 'GET', 'foo_body', [], 60); + } +} -- cgit v1.2.3