Debian Codegraph @

tests/phpunit/includes/http/GuzzleHttpRequestTest.php

raw symbols SCIP
1 <?php
2
3 use GuzzleHttp\Handler\MockHandler;
4 use GuzzleHttp\HandlerStack;
5 use GuzzleHttp\Middleware;
6 use GuzzleHttp\Psr7\Request;
7 use GuzzleHttp\Psr7\Response;
8
9 /**
10 * class for tests of GuzzleHttpRequest
11 *
12 * No actual requests are made herein - all external communications are mocked
13 *
14 * @covers \GuzzleHttpRequest
15 * @covers \MWHttpRequest
16 */
17 class GuzzleHttpRequestTest extends MediaWikiIntegrationTestCase {
18 /** @var int[] */
19 private $timeoutOptions = [
20 'timeout' => 1,
21 'connectTimeout' => 1
22 ];
23
24 /**
25 * Placeholder url to use for various tests. This is never contacted, but we must use
26 * a url of valid format to avoid validation errors.
27 * @var string
28 */
29 protected $exampleUrl = 'http://www.example.test';
30
31 /**
32 * Minimal example body text
33 * @var string
34 */
35 protected $exampleBodyText = 'x';
36
37 /**
38 * For accumulating callback data for testing
39 * @var string
40 */
41 protected $bodyTextReceived = '';
42
43 /**
44 * Callback: process a chunk of the result of a HTTP request
45 *
46 * @param mixed $req
47 * @param string $buffer
48 * @return int Number of bytes handled
49 */
50 public function processHttpDataChunk( $req, $buffer ) {
51 $this->bodyTextReceived .= $buffer;
52 return strlen( $buffer );
53 }
54
55 public function testSuccess() {
56 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
57 'status' => 200,
58 ], $this->exampleBodyText ) ] ) );
59 $r = new GuzzleHttpRequest( $this->exampleUrl,
60 [ 'handler' => $handler ] + $this->timeoutOptions );
61 $r->execute();
62
63 $this->assertEquals( 200, $r->getStatus() );
64 $this->assertEquals( $this->exampleBodyText, $r->getContent() );
65 }
66
67 public function testSuccessConstructorCallback() {
68 $this->bodyTextReceived = '';
69 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
70 'status' => 200,
71 ], $this->exampleBodyText ) ] ) );
72 $r = new GuzzleHttpRequest( $this->exampleUrl, [
73 'callback' => [ $this, 'processHttpDataChunk' ],
74 'handler' => $handler,
75 ] + $this->timeoutOptions );
76 $r->execute();
77
78 $this->assertEquals( 200, $r->getStatus() );
79 $this->assertEquals( $this->exampleBodyText, $this->bodyTextReceived );
80 }
81
82 public function testSuccessSetCallback() {
83 $this->bodyTextReceived = '';
84 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
85 'status' => 200,
86 ], $this->exampleBodyText ) ] ) );
87 $r = new GuzzleHttpRequest( $this->exampleUrl, [
88 'handler' => $handler,
89 ] + $this->timeoutOptions );
90 $r->setCallback( [ $this, 'processHttpDataChunk' ] );
91 $r->execute();
92
93 $this->assertEquals( 200, $r->getStatus() );
94 $this->assertEquals( $this->exampleBodyText, $this->bodyTextReceived );
95 }
96
97 /**
98 * use a callback stream to pipe the mocked response data to our callback function
99 */
100 public function testSuccessSink() {
101 $this->bodyTextReceived = '';
102 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
103 'status' => 200,
104 ], $this->exampleBodyText ) ] ) );
105 $r = new GuzzleHttpRequest( $this->exampleUrl, [
106 'handler' => $handler,
107 'sink' => new MWCallbackStream( [ $this, 'processHttpDataChunk' ] ),
108 ] + $this->timeoutOptions );
109 $r->execute();
110
111 $this->assertEquals( 200, $r->getStatus() );
112 $this->assertEquals( $this->exampleBodyText, $this->bodyTextReceived );
113 }
114
115 public function testBadUrl() {
116 $r = new GuzzleHttpRequest( '', $this->timeoutOptions );
117 $s = $r->execute();
118 $this->assertSame( 0, $r->getStatus() );
119 $this->assertStatusMessage( 'http-invalid-url', $s );
120 }
121
122 public function testConnectException() {
123 $handler = HandlerStack::create( new MockHandler( [ new GuzzleHttp\Exception\ConnectException(
124 'Mock Connection Exception', new Request( 'GET', $this->exampleUrl )
125 ) ] ) );
126 $r = new GuzzleHttpRequest( $this->exampleUrl,
127 [ 'handler' => $handler ] + $this->timeoutOptions );
128 $s = $r->execute();
129 $this->assertSame( 0, $r->getStatus() );
130 $this->assertStatusMessage( 'http-request-error', $s );
131 }
132
133 public function testTimeout() {
134 $handler = HandlerStack::create( new MockHandler( [ new GuzzleHttp\Exception\RequestException(
135 'Connection timed out', new Request( 'GET', $this->exampleUrl )
136 ) ] ) );
137 $r = new GuzzleHttpRequest( $this->exampleUrl,
138 [ 'handler' => $handler ] + $this->timeoutOptions );
139 $s = $r->execute();
140 $this->assertSame( 0, $r->getStatus() );
141 $this->assertStatusMessage( 'http-timed-out', $s );
142 }
143
144 public function testNotFound() {
145 $handler = HandlerStack::create( new MockHandler( [ new Response( 404, [
146 'status' => '404',
147 ] ) ] ) );
148 $r = new GuzzleHttpRequest( $this->exampleUrl,
149 [ 'handler' => $handler ] + $this->timeoutOptions );
150 $s = $r->execute();
151 $this->assertEquals( 404, $r->getStatus() );
152 $this->assertStatusMessage( 'http-bad-status', $s );
153 }
154
155 /*
156 * Test of POST requests header
157 */
158 public function testPostBody() {
159 $container = [];
160 $history = Middleware::history( $container );
161 $stack = HandlerStack::create( new MockHandler( [ new Response() ] ) );
162 $stack->push( $history );
163 $client = new GuzzleHttpRequest( $this->exampleUrl, [
164 'method' => 'POST',
165 'handler' => $stack,
166 'post' => 'key=value',
167 ] + $this->timeoutOptions );
168 $client->execute();
169
170 $request = $container[0]['request'];
171 $this->assertEquals( 'POST', $request->getMethod() );
172 $this->assertEquals( 'application/x-www-form-urlencoded',
173 $request->getHeader( 'Content-Type' )[0] );
174 }
175
176 /**
177 * Test POSTed multipart request body with custom content type
178 */
179 public function testPostBodyContentType() {
180 $container = [];
181 $history = Middleware::history( $container );
182 $stack = HandlerStack::create( new MockHandler( [ new Response() ] ) );
183 $stack->push( $history );
184 $client = new GuzzleHttpRequest( $this->exampleUrl, [
185 'method' => 'POST',
186 'handler' => $stack,
187 'postData' => new \GuzzleHttp\Psr7\MultipartStream( [ [
188 'name' => 'a',
189 'contents' => 'b'
190 ] ] ),
191 ] + $this->timeoutOptions );
192 $client->setHeader( 'Content-Type', 'text/mwtest' );
193 $client->execute();
194
195 $request = $container[0]['request'];
196 $this->assertEquals( 'text/mwtest',
197 $request->getHeader( 'Content-Type' )[0] );
198 }
199
200 /*
201 * Test that cookies from CookieJar were sent in the outgoing request.
202 */
203 public function testCookieSent() {
204 $domain = parse_url( $this->exampleUrl, PHP_URL_HOST );
205 $expectedCookies = [ 'cookie1' => 'value1', 'anothercookie' => 'secondvalue' ];
206 $jar = new CookieJar;
207 foreach ( $expectedCookies as $key => $val ) {
208 $jar->setCookie( $key, $val, [ 'domain' => $domain ] );
209 }
210
211 $container = [];
212 $history = Middleware::history( $container );
213 $stack = HandlerStack::create( new MockHandler( [ new Response() ] ) );
214 $stack->push( $history );
215 $client = new GuzzleHttpRequest( $this->exampleUrl, [
216 'method' => 'POST',
217 'handler' => $stack,
218 'post' => 'key=value',
219 ] + $this->timeoutOptions );
220 $client->setCookieJar( $jar );
221 $client->execute();
222
223 $request = $container[0]['request'];
224 $this->assertEquals( [ 'cookie1=value1; anothercookie=secondvalue' ],
225 $request->getHeader( 'Cookie' ) );
226 }
227
228 /*
229 * Test that cookies returned by HTTP response were added back into the CookieJar.
230 */
231 public function testCookieReceived() {
232 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
233 'status' => 200,
234 'Set-Cookie' => [ 'cookie1=value1', 'anothercookie=secondvalue' ]
235 ] ) ] ) );
236 $r = new GuzzleHttpRequest( $this->exampleUrl,
237 [ 'handler' => $handler ] + $this->timeoutOptions );
238 $r->execute();
239
240 $domain = parse_url( $this->exampleUrl, PHP_URL_HOST );
241 $this->assertEquals( 'cookie1=value1; anothercookie=secondvalue',
242 $r->getCookieJar()->serializeToHttpRequest( '/', $domain ) );
243 }
244 }