1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author David Desberg <[email protected]> |
5
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace OAuth\Unit\Common\Http; |
9
|
|
|
|
10
|
|
|
use Closure; |
11
|
|
|
use OAuth\Common\Http\Client; |
12
|
|
|
use OAuth\Common\Http\Client\ClientInterface; |
13
|
|
|
use OAuth\Common\Http\Uri\Uri; |
14
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class HttpClientsTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ClientInterface[]|object |
21
|
|
|
*/ |
22
|
|
|
protected $clients; |
23
|
|
|
|
24
|
|
|
protected function setUp(): void |
25
|
|
|
{ |
26
|
|
|
$streamClient = new Client\StreamClient(); |
27
|
|
|
$streamClient->setTimeout(3); |
28
|
|
|
|
29
|
|
|
$curlClient = new Client\CurlClient(); |
30
|
|
|
$curlClient->setTimeout(3); |
31
|
|
|
|
32
|
|
|
$this->clients[] = $streamClient; |
33
|
|
|
$this->clients[] = $curlClient; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function tearDown(): void |
37
|
|
|
{ |
38
|
|
|
foreach ($this->clients as $client) { |
39
|
|
|
unset($client); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Test that extra headers are passed properly. |
45
|
|
|
*/ |
46
|
|
|
public function testHeaders(): void |
47
|
|
|
{ |
48
|
|
|
$testUri = new Uri('http://httpbin.org/get'); |
49
|
|
|
|
50
|
|
|
$me = $this; |
51
|
|
|
$headerCb = function ($response) use ($me): void { |
52
|
|
|
$data = json_decode($response, true); |
53
|
|
|
$me->assertEquals('extraheadertest', $data['headers']['Testingheader']); |
54
|
|
|
}; |
55
|
|
|
|
56
|
|
|
$this->__doTestRetrieveResponse($testUri, [], ['Testingheader' => 'extraheadertest'], 'GET', $headerCb); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Tests that we get an exception for a >= 400 status code. |
61
|
|
|
*/ |
62
|
|
|
public function testException(): void |
63
|
|
|
{ |
64
|
|
|
// sending a post here should get us a 405 which should trigger an exception |
65
|
|
|
$testUri = new Uri('http://httpbin.org/delete'); |
66
|
|
|
foreach ($this->clients as $client) { |
67
|
|
|
$this->expectException('OAuth\Common\Http\Exception\TokenResponseException'); |
68
|
|
|
$client->retrieveResponse($testUri, ['blah' => 'blih']); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Tests the DELETE method. |
74
|
|
|
*/ |
75
|
|
|
public function testDelete(): void |
76
|
|
|
{ |
77
|
|
|
$testUri = new Uri('http://httpbin.org/delete'); |
78
|
|
|
|
79
|
|
|
$me = $this; |
80
|
|
|
$deleteTestCb = function ($response) use ($me): void { |
81
|
|
|
$data = json_decode($response, true); |
82
|
|
|
$me->assertEquals('', $data['data']); |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
$this->__doTestRetrieveResponse($testUri, [], [], 'DELETE', $deleteTestCb); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Tests the PUT method. |
90
|
|
|
*/ |
91
|
|
|
public function testPut(): void |
92
|
|
|
{ |
93
|
|
|
$testUri = new Uri('http://httpbin.org/put'); |
94
|
|
|
|
95
|
|
|
$me = $this; |
96
|
|
|
$putTestCb = function ($response) use ($me): void { |
97
|
|
|
// verify the put response |
98
|
|
|
$data = json_decode($response, true); |
99
|
|
|
$me->assertEquals(json_encode(['testKey' => 'testValue']), $data['data']); |
100
|
|
|
}; |
101
|
|
|
|
102
|
|
|
$this->__doTestRetrieveResponse($testUri, json_encode(['testKey' => 'testValue']), ['Content-Type' => 'application/json'], 'PUT', $putTestCb); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Tests the POST method. |
107
|
|
|
*/ |
108
|
|
|
public function testPost(): void |
109
|
|
|
{ |
110
|
|
|
// http test server |
111
|
|
|
$testUri = new Uri('http://httpbin.org/post'); |
112
|
|
|
|
113
|
|
|
$me = $this; |
114
|
|
|
$postTestCb = function ($response) use ($me): void { |
115
|
|
|
// verify the post response |
116
|
|
|
$data = json_decode($response, true); |
117
|
|
|
// note that we check this because the retrieveResponse wrapper function automatically adds a content-type |
118
|
|
|
// if there isn't one and it |
119
|
|
|
$me->assertEquals('testValue', $data['form']['testKey']); |
120
|
|
|
}; |
121
|
|
|
|
122
|
|
|
$this->__doTestRetrieveResponse($testUri, ['testKey' => 'testValue'], [], 'POST', $postTestCb); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Expect exception when we try to send a GET request with a body. |
127
|
|
|
*/ |
128
|
|
|
public function testInvalidGet(): void |
129
|
|
|
{ |
130
|
|
|
$testUri = new Uri('http://site.net'); |
131
|
|
|
|
132
|
|
|
foreach ($this->clients as $client) { |
133
|
|
|
$this->expectException('InvalidArgumentException'); |
134
|
|
|
$client->retrieveResponse($testUri, ['blah' => 'blih'], [], 'GET'); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Tests the GET method. |
140
|
|
|
*/ |
141
|
|
|
public function testGet(): void |
142
|
|
|
{ |
143
|
|
|
// test uri |
144
|
|
|
$testUri = new Uri('http://httpbin.org/get?testKey=testValue'); |
145
|
|
|
|
146
|
|
|
$me = $this; |
147
|
|
|
$getTestCb = function ($response) use ($me): void { |
148
|
|
|
$data = json_decode($response, true); |
149
|
|
|
$me->assertEquals('testValue', $data['args']['testKey']); |
150
|
|
|
}; |
151
|
|
|
|
152
|
|
|
$this->__doTestRetrieveResponse($testUri, [], [], 'GET', $getTestCb); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Test on all HTTP clients. |
157
|
|
|
* |
158
|
|
|
* @param array $param |
159
|
|
|
* @param string $method |
160
|
|
|
* @param Closure $responseCallback |
161
|
|
|
*/ |
162
|
|
|
protected function __doTestRetrieveResponse(UriInterface $uri, $param, array $header, $method, $responseCallback): void |
163
|
|
|
{ |
164
|
|
|
foreach ($this->clients as $client) { |
165
|
|
|
$response = $client->retrieveResponse($uri, $param, $header, $method); |
166
|
|
|
$responseCallback($response, $client); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|