1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2017 Facebook, Inc. |
4
|
|
|
* |
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
7
|
|
|
* form for use in connection with the web services and APIs provided by |
8
|
|
|
* Facebook. |
9
|
|
|
* |
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
namespace Facebook\Tests\Http; |
25
|
|
|
|
26
|
|
|
use Facebook\Http\GraphRawResponse; |
27
|
|
|
|
28
|
|
|
class GraphRawResponseTest extends \PHPUnit_Framework_TestCase |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
protected $fakeRawProxyHeader = "HTTP/1.0 200 Connection established |
32
|
|
|
Proxy-agent: Kerio Control/7.1.1 build 1971\r\n\r\n"; |
33
|
|
|
protected $fakeRawHeader = <<<HEADER |
34
|
|
|
HTTP/1.1 200 OK |
35
|
|
|
Etag: "9d86b21aa74d74e574bbb35ba13524a52deb96e3" |
36
|
|
|
Content-Type: text/javascript; charset=UTF-8 |
37
|
|
|
X-FB-Rev: 9244768 |
38
|
|
|
Date: Mon, 19 May 2014 18:37:17 GMT |
39
|
|
|
X-FB-Debug: 02QQiffE7JG2rV6i/Agzd0gI2/OOQ2lk5UW0= |
40
|
|
|
Access-Control-Allow-Origin: *\r\n\r\n |
41
|
|
|
HEADER; |
42
|
|
|
protected $fakeHeadersAsArray = [ |
43
|
|
|
'Etag' => '"9d86b21aa74d74e574bbb35ba13524a52deb96e3"', |
44
|
|
|
'Content-Type' => 'text/javascript; charset=UTF-8', |
45
|
|
|
'X-FB-Rev' => '9244768', |
46
|
|
|
'Date' => 'Mon, 19 May 2014 18:37:17 GMT', |
47
|
|
|
'X-FB-Debug' => '02QQiffE7JG2rV6i/Agzd0gI2/OOQ2lk5UW0=', |
48
|
|
|
'Access-Control-Allow-Origin' => '*', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
protected $jsonFakeHeader = 'x-fb-ads-insights-throttle: {"app_id_util_pct": 0.00,"acc_id_util_pct": 0.00}'; |
52
|
|
|
protected $jsonFakeHeaderAsArray = ['x-fb-ads-insights-throttle' => '{"app_id_util_pct": 0.00,"acc_id_util_pct": 0.00}']; |
53
|
|
|
|
54
|
|
|
public function testCanSetTheHeadersFromAnArray() |
55
|
|
|
{ |
56
|
|
|
$myHeaders = [ |
57
|
|
|
'foo' => 'bar', |
58
|
|
|
'baz' => 'faz', |
59
|
|
|
]; |
60
|
|
|
$response = new GraphRawResponse($myHeaders, ''); |
61
|
|
|
$headers = $response->getHeaders(); |
62
|
|
|
|
63
|
|
|
$this->assertEquals($myHeaders, $headers); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testCanSetTheHeadersFromAString() |
67
|
|
|
{ |
68
|
|
|
$response = new GraphRawResponse($this->fakeRawHeader, ''); |
69
|
|
|
$headers = $response->getHeaders(); |
70
|
|
|
$httpResponseCode = $response->getHttpResponseCode(); |
71
|
|
|
|
72
|
|
|
$this->assertEquals($this->fakeHeadersAsArray, $headers); |
73
|
|
|
$this->assertEquals(200, $httpResponseCode); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testWillIgnoreProxyHeaders() |
77
|
|
|
{ |
78
|
|
|
$response = new GraphRawResponse($this->fakeRawProxyHeader . $this->fakeRawHeader, ''); |
79
|
|
|
$headers = $response->getHeaders(); |
80
|
|
|
$httpResponseCode = $response->getHttpResponseCode(); |
81
|
|
|
|
82
|
|
|
$this->assertEquals($this->fakeHeadersAsArray, $headers); |
83
|
|
|
$this->assertEquals(200, $httpResponseCode); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testCanTransformJsonHeaderValues() |
87
|
|
|
{ |
88
|
|
|
$response = new GraphRawResponse($this->jsonFakeHeader, ''); |
89
|
|
|
$headers = $response->getHeaders(); |
90
|
|
|
|
91
|
|
|
$this->assertEquals($this->jsonFakeHeaderAsArray['x-fb-ads-insights-throttle'], $headers['x-fb-ads-insights-throttle']); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testHttpResponseCode() |
95
|
|
|
{ |
96
|
|
|
// HTTP/1.0 |
97
|
|
|
$headers = str_replace("HTTP/1.1", "HTTP/1.0", $this->fakeRawHeader); |
98
|
|
|
$response = new GraphRawResponse($headers, ""); |
99
|
|
|
$this->assertEquals(200, $response->getHttpResponseCode()); |
100
|
|
|
|
101
|
|
|
// HTTP/1.1 |
102
|
|
|
$response = new GraphRawResponse($this->fakeRawHeader, ""); |
103
|
|
|
$this->assertEquals(200, $response->getHttpResponseCode()); |
104
|
|
|
|
105
|
|
|
// HTTP/2 |
106
|
|
|
$headers = str_replace("HTTP/1.1", "HTTP/2", $this->fakeRawHeader); |
107
|
|
|
$response = new GraphRawResponse($headers, ""); |
108
|
|
|
$this->assertEquals(200, $response->getHttpResponseCode()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|