summaryrefslogtreecommitdiff
path: root/lib/facebook-graph-sdk/tests/HttpClients
diff options
context:
space:
mode:
Diffstat (limited to 'lib/facebook-graph-sdk/tests/HttpClients')
-rw-r--r--lib/facebook-graph-sdk/tests/HttpClients/AbstractTestHttpClient.php60
-rw-r--r--lib/facebook-graph-sdk/tests/HttpClients/FacebookCurlHttpClientTest.php334
-rw-r--r--lib/facebook-graph-sdk/tests/HttpClients/FacebookGuzzleHttpClientTest.php143
-rw-r--r--lib/facebook-graph-sdk/tests/HttpClients/FacebookStreamHttpClientTest.php134
4 files changed, 671 insertions, 0 deletions
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 @@
+<?php
+/**
+ * Copyright 2014 Facebook, Inc.
+ *
+ * You are hereby granted a non-exclusive, worldwide, royalty-free license to
+ * use, copy, modify, and distribute this software in source code or binary
+ * form for use in connection with the web services and APIs provided by
+ * Facebook.
+ *
+ * As with any software that integrates with the Facebook platform, your use
+ * of this software is subject to the Facebook Developer Principles and
+ * Policies [http://developers.facebook.com/policy/]. This copyright 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 Facebook\Tests\HttpClients;
+
+abstract class AbstractTestHttpClient extends \PHPUnit_Framework_TestCase
+{
+ protected $fakeRawRedirectHeader = "HTTP/1.1 302 Found
+Content-Type: text/html; charset=utf-8
+Location: https://foobar.com/\r\n\r\n";
+ protected $fakeRawProxyHeader = "HTTP/1.0 200 Connection established\r\n\r\n";
+ protected $fakeRawProxyHeader2 = "HTTP/1.0 200 Connection established
+Proxy-agent: Kerio Control/7.1.1 build 1971\r\n\r\n";
+ protected $fakeRawHeader = "HTTP/1.1 200 OK
+Etag: \"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: *\r\n\r\n";
+ protected $fakeRawBody = "{\"id\":\"123\",\"name\":\"Foo Bar\"}";
+ protected $fakeHeadersAsArray = [
+ 'Etag' => '"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 @@
+<?php
+/**
+ * Copyright 2014 Facebook, Inc.
+ *
+ * You are hereby granted a non-exclusive, worldwide, royalty-free license to
+ * use, copy, modify, and distribute this software in source code or binary
+ * form for use in connection with the web services and APIs provided by
+ * Facebook.
+ *
+ * As with any software that integrates with the Facebook platform, your use
+ * of this software is subject to the Facebook Developer Principles and
+ * Policies [http://developers.facebook.com/policy/]. This copyright 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 Facebook\Tests\HttpClients;
+
+use Mockery as m;
+use Facebook\HttpClients\FacebookCurlHttpClient;
+
+class FacebookCurlHttpClientTest extends AbstractTestHttpClient
+{
+ /**
+ * @var \Facebook\HttpClients\FacebookCurl
+ */
+ protected $curlMock;
+
+ /**
+ * @var FacebookCurlHttpClient
+ */
+ protected $curlClient;
+
+ const CURL_VERSION_STABLE = 0x072400;
+ const CURL_VERSION_BUGGY = 0x071400;
+
+ public function setUp()
+ {
+ $this->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 @@
+<?php
+/**
+ * Copyright 2014 Facebook, Inc.
+ *
+ * You are hereby granted a non-exclusive, worldwide, royalty-free license to
+ * use, copy, modify, and distribute this software in source code or binary
+ * form for use in connection with the web services and APIs provided by
+ * Facebook.
+ *
+ * As with any software that integrates with the Facebook platform, your use
+ * of this software is subject to the Facebook Developer Principles and
+ * Policies [http://developers.facebook.com/policy/]. This copyright 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 Facebook\Tests\HttpClients;
+
+use Mockery as m;
+use Facebook\HttpClients\FacebookGuzzleHttpClient;
+use GuzzleHttp\Message\Request;
+use GuzzleHttp\Message\Response;
+use GuzzleHttp\Stream\Stream;
+use GuzzleHttp\Exception\RequestException;
+
+class FacebookGuzzleHttpClientTest extends AbstractTestHttpClient
+{
+ /**
+ * @var \GuzzleHttp\Client
+ */
+ protected $guzzleMock;
+
+ /**
+ * @var FacebookGuzzleHttpClient
+ */
+ protected $guzzleClient;
+
+ public function setUp()
+ {
+ $this->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 @@
+<?php
+/**
+ * Copyright 2014 Facebook, Inc.
+ *
+ * You are hereby granted a non-exclusive, worldwide, royalty-free license to
+ * use, copy, modify, and distribute this software in source code or binary
+ * form for use in connection with the web services and APIs provided by
+ * Facebook.
+ *
+ * As with any software that integrates with the Facebook platform, your use
+ * of this software is subject to the Facebook Developer Principles and
+ * Policies [http://developers.facebook.com/policy/]. This copyright 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 Facebook\Tests\HttpClients;
+
+use Mockery as m;
+use Facebook\HttpClients\FacebookStreamHttpClient;
+
+class FacebookStreamHttpClientTest extends AbstractTestHttpClient
+{
+ /**
+ * @var \Facebook\HttpClients\FacebookStream
+ */
+ protected $streamMock;
+
+ /**
+ * @var FacebookStreamHttpClient
+ */
+ protected $streamClient;
+
+ public function setUp()
+ {
+ $this->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);
+ }
+}