|
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
|
|
|
namespace Facebook\Tests; |
|
24
|
|
|
|
|
25
|
|
|
use Facebook\Application; |
|
26
|
|
|
use Facebook\Request; |
|
27
|
|
|
use Facebook\FileUpload\File; |
|
28
|
|
|
use Facebook\FileUpload\Video; |
|
29
|
|
|
use PHPUnit\Framework\TestCase; |
|
30
|
|
|
|
|
31
|
|
|
class RequestTest extends TestCase |
|
32
|
|
|
{ |
|
33
|
|
|
public function testAnEmptyRequestEntityCanInstantiate() |
|
34
|
|
|
{ |
|
35
|
|
|
$app = new Application('123', 'foo_secret'); |
|
36
|
|
|
$request = new Request($app); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertInstanceOf(Request::class, $request); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @expectedException \Facebook\Exception\SDKException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testAMissingAccessTokenWillThrow() |
|
45
|
|
|
{ |
|
46
|
|
|
$app = new Application('123', 'foo_secret'); |
|
47
|
|
|
$request = new Request($app); |
|
48
|
|
|
|
|
49
|
|
|
$request->validateAccessToken(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @expectedException \Facebook\Exception\SDKException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testAMissingMethodWillThrow() |
|
56
|
|
|
{ |
|
57
|
|
|
$app = new Application('123', 'foo_secret'); |
|
58
|
|
|
$request = new Request($app); |
|
59
|
|
|
|
|
60
|
|
|
$request->validateMethod(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @expectedException \Facebook\Exception\SDKException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testAnInvalidMethodWillThrow() |
|
67
|
|
|
{ |
|
68
|
|
|
$app = new Application('123', 'foo_secret'); |
|
69
|
|
|
$request = new Request($app, 'foo_token', 'FOO'); |
|
70
|
|
|
|
|
71
|
|
|
$request->validateMethod(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testGetHeadersWillAutoAppendETag() |
|
75
|
|
|
{ |
|
76
|
|
|
$app = new Application('123', 'foo_secret'); |
|
77
|
|
|
$request = new Request($app, null, 'GET', '/foo', [], 'fooETag'); |
|
78
|
|
|
|
|
79
|
|
|
$headers = $request->getHeaders(); |
|
80
|
|
|
|
|
81
|
|
|
$expectedHeaders = Request::getDefaultHeaders(); |
|
82
|
|
|
$expectedHeaders['If-None-Match'] = 'fooETag'; |
|
83
|
|
|
|
|
84
|
|
|
$this->assertEquals($expectedHeaders, $headers); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testGetParamsWillAutoAppendAccessTokenAndAppSecretProof() |
|
88
|
|
|
{ |
|
89
|
|
|
$app = new Application('123', 'foo_secret'); |
|
90
|
|
|
$request = new Request($app, 'foo_token', 'POST', '/foo', ['foo' => 'bar']); |
|
91
|
|
|
|
|
92
|
|
|
$params = $request->getParams(); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertEquals([ |
|
95
|
|
|
'foo' => 'bar', |
|
96
|
|
|
'access_token' => 'foo_token', |
|
97
|
|
|
'appsecret_proof' => 'df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9', |
|
98
|
|
|
], $params); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testAnAccessTokenCanBeSetFromTheParams() |
|
102
|
|
|
{ |
|
103
|
|
|
$app = new Application('123', 'foo_secret'); |
|
104
|
|
|
$request = new Request($app, null, 'POST', '/me', ['access_token' => 'bar_token']); |
|
105
|
|
|
|
|
106
|
|
|
$accessToken = $request->getAccessToken(); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertEquals('bar_token', $accessToken); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @expectedException \Facebook\Exception\SDKException |
|
113
|
|
|
*/ |
|
114
|
|
|
public function testAccessTokenConflictsWillThrow() |
|
115
|
|
|
{ |
|
116
|
|
|
$app = new Application('123', 'foo_secret'); |
|
117
|
|
|
new Request($app, 'foo_token', 'POST', '/me', ['access_token' => 'bar_token']); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testAProperUrlWillBeGenerated() |
|
121
|
|
|
{ |
|
122
|
|
|
$app = new Application('123', 'foo_secret'); |
|
123
|
|
|
$getRequest = new Request($app, 'foo_token', 'GET', '/foo', ['foo' => 'bar']); |
|
124
|
|
|
|
|
125
|
|
|
$getUrl = $getRequest->getUrl(); |
|
126
|
|
|
$expectedParams = 'foo=bar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9'; |
|
127
|
|
|
$expectedUrl = '/foo?' . $expectedParams; |
|
128
|
|
|
|
|
129
|
|
|
$this->assertEquals($expectedUrl, $getUrl); |
|
130
|
|
|
|
|
131
|
|
|
$postRequest = new Request($app, 'foo_token', 'POST', '/bar', ['foo' => 'bar'], null, 'v0.0'); |
|
132
|
|
|
|
|
133
|
|
|
$postUrl = $postRequest->getUrl(); |
|
134
|
|
|
$expectedUrl = '/v0.0/bar'; |
|
135
|
|
|
|
|
136
|
|
|
$this->assertEquals($expectedUrl, $postUrl); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function testAuthenticationParamsAreStrippedAndReapplied() |
|
140
|
|
|
{ |
|
141
|
|
|
$app = new Application('123', 'foo_secret'); |
|
142
|
|
|
|
|
143
|
|
|
$request = new Request( |
|
144
|
|
|
$app, |
|
145
|
|
|
$accessToken = 'foo_token', |
|
146
|
|
|
$method = 'GET', |
|
147
|
|
|
$endpoint = '/foo', |
|
148
|
|
|
$params = [ |
|
149
|
|
|
'access_token' => 'foo_token', |
|
150
|
|
|
'appsecret_proof' => 'bar_app_secret', |
|
151
|
|
|
'bar' => 'baz', |
|
152
|
|
|
] |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
$url = $request->getUrl(); |
|
156
|
|
|
|
|
157
|
|
|
$expectedParams = 'bar=baz&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9'; |
|
158
|
|
|
$expectedUrl = '/foo?' . $expectedParams; |
|
159
|
|
|
$this->assertEquals($expectedUrl, $url); |
|
160
|
|
|
|
|
161
|
|
|
$params = $request->getParams(); |
|
162
|
|
|
|
|
163
|
|
|
$expectedParams = [ |
|
164
|
|
|
'access_token' => 'foo_token', |
|
165
|
|
|
'appsecret_proof' => 'df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9', |
|
166
|
|
|
'bar' => 'baz', |
|
167
|
|
|
]; |
|
168
|
|
|
$this->assertEquals($expectedParams, $params); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function testAFileCanBeAddedToParams() |
|
172
|
|
|
{ |
|
173
|
|
|
$myFile = new File(__DIR__ . '/foo.txt'); |
|
174
|
|
|
$params = [ |
|
175
|
|
|
'name' => 'Foo Bar', |
|
176
|
|
|
'source' => $myFile, |
|
177
|
|
|
]; |
|
178
|
|
|
$app = new Application('123', 'foo_secret'); |
|
179
|
|
|
$request = new Request($app, 'foo_token', 'POST', '/foo/photos', $params); |
|
180
|
|
|
|
|
181
|
|
|
$actualParams = $request->getParams(); |
|
182
|
|
|
|
|
183
|
|
|
$this->assertTrue($request->containsFileUploads()); |
|
184
|
|
|
$this->assertFalse($request->containsVideoUploads()); |
|
185
|
|
|
$this->assertArrayNotHasKey('source', $actualParams); |
|
186
|
|
|
$this->assertEquals('Foo Bar', $actualParams['name']); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function testAVideoCanBeAddedToParams() |
|
190
|
|
|
{ |
|
191
|
|
|
$myFile = new Video(__DIR__ . '/foo.txt'); |
|
192
|
|
|
$params = [ |
|
193
|
|
|
'name' => 'Foo Bar', |
|
194
|
|
|
'source' => $myFile, |
|
195
|
|
|
]; |
|
196
|
|
|
$app = new Application('123', 'foo_secret'); |
|
197
|
|
|
$request = new Request($app, 'foo_token', 'POST', '/foo/videos', $params); |
|
198
|
|
|
|
|
199
|
|
|
$actualParams = $request->getParams(); |
|
200
|
|
|
|
|
201
|
|
|
$this->assertTrue($request->containsFileUploads()); |
|
202
|
|
|
$this->assertTrue($request->containsVideoUploads()); |
|
203
|
|
|
$this->assertArrayNotHasKey('source', $actualParams); |
|
204
|
|
|
$this->assertEquals('Foo Bar', $actualParams['name']); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|