|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Onoi\HttpRequest\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Onoi\HttpRequest\SocketRequest; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @covers \Onoi\HttpRequest\SocketRequest |
|
9
|
|
|
* @group onoi-http-request |
|
10
|
|
|
* |
|
11
|
|
|
* @license GNU GPL v2+ |
|
12
|
|
|
* @since 1.1 |
|
13
|
|
|
* |
|
14
|
|
|
* @author mwjames |
|
15
|
|
|
*/ |
|
16
|
|
|
class SocketRequestTest extends \PHPUnit_Framework_TestCase { |
|
17
|
|
|
|
|
18
|
|
|
public function testCanConstruct() { |
|
19
|
|
|
|
|
20
|
|
|
$instance = new SocketRequest(); |
|
21
|
|
|
|
|
22
|
|
|
$this->assertInstanceOf( |
|
23
|
|
|
'\Onoi\HttpRequest\SocketRequest', |
|
24
|
|
|
$instance |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
$this->assertInstanceOf( |
|
28
|
|
|
'\Onoi\HttpRequest\HttpRequest', |
|
29
|
|
|
$instance |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testPing() { |
|
34
|
|
|
|
|
35
|
|
|
$instance = new SocketRequest(); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertFalse( |
|
38
|
|
|
$instance->ping() |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_URL, 'http://example.org' ); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertInternalType( |
|
44
|
|
|
'boolean', |
|
45
|
|
|
$instance->ping() |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testExecute() { |
|
50
|
|
|
|
|
51
|
|
|
$instance = new SocketRequest(); |
|
52
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_CONNECTION_TIMEOUT, 1 ); |
|
53
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_URL, 'http://localhost:8888' ); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertInternalType( |
|
56
|
|
|
'boolean', |
|
57
|
|
|
$instance->execute() |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testGetLastError() { |
|
62
|
|
|
|
|
63
|
|
|
$instance = new SocketRequest(); |
|
64
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_CONNECTION_TIMEOUT, 1 ); |
|
65
|
|
|
|
|
66
|
|
|
$instance->execute(); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertInternalType( |
|
69
|
|
|
'string', |
|
70
|
|
|
$instance->getLastError() |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testGetLastErrorCode() { |
|
75
|
|
|
|
|
76
|
|
|
$instance = new SocketRequest(); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertInternalType( |
|
79
|
|
|
'integer', |
|
80
|
|
|
$instance->getLastErrorCode() |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testGetLastTransferInfo() { |
|
85
|
|
|
|
|
86
|
|
|
$instance = new SocketRequest(); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertInternalType( |
|
89
|
|
|
'string', |
|
90
|
|
|
$instance->getLastTransferInfo() |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testCallbackOnRequestNotCompleted() { |
|
95
|
|
|
|
|
96
|
|
|
$instance = new SocketRequest(); |
|
97
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_CONNECTION_TIMEOUT, 0.1 ); |
|
98
|
|
|
|
|
99
|
|
|
$requestResponse = null; |
|
100
|
|
|
|
|
101
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_ON_FAILED_CALLBACK, function( $requestResponseFailed ) use ( &$requestResponse ) { |
|
102
|
|
|
$requestResponse = $requestResponseFailed; |
|
103
|
|
|
} ); |
|
104
|
|
|
|
|
105
|
|
|
$instance->execute(); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertRequestResponse( $requestResponse ); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testCallbackOnRequestCompleted() { |
|
111
|
|
|
|
|
112
|
|
|
$url = 'http://localhost:8080/'; |
|
113
|
|
|
|
|
114
|
|
|
$instance = new SocketRequest( $url ); |
|
115
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_CONNECTION_TIMEOUT, 2 ); |
|
116
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_METHOD, 'HEAD' ); |
|
117
|
|
|
|
|
118
|
|
|
if ( !$instance->ping() ) { |
|
119
|
|
|
$this->markTestSkipped( "Skip test because {$url} was not reachable" ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$requestResponse = null; |
|
123
|
|
|
|
|
124
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_ON_COMPLETED_CALLBACK, function( $requestResponseCompleted ) use ( &$requestResponse ) { |
|
125
|
|
|
$requestResponse = $requestResponseCompleted; |
|
126
|
|
|
} ); |
|
127
|
|
|
|
|
128
|
|
|
$instance->execute(); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertRequestResponse( $requestResponse ); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testTryInvalidCallbackOnRequestCompleted() { |
|
134
|
|
|
|
|
135
|
|
|
$instance = new SocketRequest(); |
|
136
|
|
|
|
|
137
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_CONNECTION_TIMEOUT, 0.1 ); |
|
138
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_ON_COMPLETED_CALLBACK, 'foo' ); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertFalse( |
|
141
|
|
|
$instance->execute() |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function testCallbackOnRequestFailed() { |
|
146
|
|
|
|
|
147
|
|
|
$instance = new SocketRequest(); |
|
148
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_CONNECTION_TIMEOUT, 0.1 ); |
|
149
|
|
|
|
|
150
|
|
|
$requestResponse = null; |
|
151
|
|
|
|
|
152
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_ON_FAILED_CALLBACK, function( $requestResponseFailed ) use ( &$requestResponse ) { |
|
153
|
|
|
$requestResponse = $requestResponseFailed; |
|
154
|
|
|
} ); |
|
155
|
|
|
|
|
156
|
|
|
$instance->execute(); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertRequestResponse( $requestResponse ); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function testCallbackOnRequestNotAccepted() { |
|
162
|
|
|
|
|
163
|
|
|
$instance = new SocketRequest(); |
|
164
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_CONNECTION_TIMEOUT, 2 ); |
|
165
|
|
|
|
|
166
|
|
|
$instance->ping(); |
|
167
|
|
|
|
|
168
|
|
|
$requestResponse = null; |
|
169
|
|
|
|
|
170
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_ON_FAILED_CALLBACK, function( $requestResponseFailed ) use ( &$requestResponse ) { |
|
171
|
|
|
$requestResponse = $requestResponseFailed; |
|
172
|
|
|
} ); |
|
173
|
|
|
|
|
174
|
|
|
$instance->execute(); |
|
175
|
|
|
$this->assertRequestResponse( $requestResponse ); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
private function assertRequestResponse( $requestResponse ) { |
|
179
|
|
|
|
|
180
|
|
|
$this->assertInstanceOf( |
|
181
|
|
|
'\Onoi\HttpRequest\RequestResponse', |
|
182
|
|
|
$requestResponse |
|
183
|
|
|
); |
|
184
|
|
|
|
|
185
|
|
|
$expectedRequestResponseFields = array( |
|
186
|
|
|
'wasCompleted', |
|
187
|
|
|
'responseMessage', |
|
188
|
|
|
'host', |
|
189
|
|
|
'port', |
|
190
|
|
|
'path', |
|
191
|
|
|
'connectionFailure', |
|
192
|
|
|
'requestProcTime' |
|
193
|
|
|
); |
|
194
|
|
|
|
|
195
|
|
|
foreach ( $expectedRequestResponseFields as $field ) { |
|
196
|
|
|
$this->assertTrue( $requestResponse->has( $field ), 'Failed for ' . $field ); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function testDefinedConstants() { |
|
201
|
|
|
|
|
202
|
|
|
$constants = array( |
|
203
|
|
|
'ONOI_HTTP_REQUEST_ON_FAILED_CALLBACK', |
|
204
|
|
|
'ONOI_HTTP_REQUEST_ON_COMPLETED_CALLBACK', |
|
205
|
|
|
'ONOI_HTTP_REQUEST_SOCKET_CLIENT_FLAGS', |
|
206
|
|
|
'ONOI_HTTP_REQUEST_URL', |
|
207
|
|
|
'ONOI_HTTP_REQUEST_CONNECTION_TIMEOUT', |
|
208
|
|
|
'ONOI_HTTP_REQUEST_CONNECTION_FAILURE_REPEAT', |
|
209
|
|
|
'ONOI_HTTP_REQUEST_CONTENT', |
|
210
|
|
|
'ONOI_HTTP_REQUEST_CONTENT_TYPE', |
|
211
|
|
|
'ONOI_HTTP_REQUEST_METHOD', |
|
212
|
|
|
'ONOI_HTTP_REQUEST_PROTOCOL_VERSION', |
|
213
|
|
|
'ONOI_HTTP_REQUEST_SSL_VERIFYPEER', |
|
214
|
|
|
'ONOI_HTTP_REQUEST_FOLLOWLOCATION' |
|
215
|
|
|
); |
|
216
|
|
|
|
|
217
|
|
|
$instance = new SocketRequest(); |
|
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
foreach ( $constants as $constant ) { |
|
220
|
|
|
$this->assertTrue( defined( $constant ) ); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.