Merge "Add config for serving main Page from the domain root"
[lhc/web/wiklou.git] / tests / phpunit / includes / http / GuzzleHttpRequestTest.php
1 <?php
2
3 use GuzzleHttp\Handler\MockHandler;
4 use GuzzleHttp\HandlerStack;
5 use GuzzleHttp\Psr7\Response;
6 use GuzzleHttp\Psr7\Request;
7
8 /**
9 * class for tests of GuzzleHttpRequest
10 *
11 * No actual requests are made herein - all external communications are mocked
12 *
13 * @covers GuzzleHttpRequest
14 * @covers MWHttpRequest
15 */
16 class GuzzleHttpRequestTest extends MediaWikiTestCase {
17 /**
18 * Placeholder url to use for various tests. This is never contacted, but we must use
19 * a url of valid format to avoid validation errors.
20 * @var string
21 */
22 protected $exampleUrl = 'http://www.example.test';
23
24 /**
25 * Minimal example body text
26 * @var string
27 */
28 protected $exampleBodyText = 'x';
29
30 /**
31 * For accumulating callback data for testing
32 * @var string
33 */
34 protected $bodyTextReceived = '';
35
36 /**
37 * Callback: process a chunk of the result of a HTTP request
38 *
39 * @param mixed $req
40 * @param string $buffer
41 * @return int Number of bytes handled
42 */
43 public function processHttpDataChunk( $req, $buffer ) {
44 $this->bodyTextReceived .= $buffer;
45 return strlen( $buffer );
46 }
47
48 public function testSuccess() {
49 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
50 'status' => 200,
51 ], $this->exampleBodyText ) ] ) );
52 $r = new GuzzleHttpRequest( $this->exampleUrl, [ 'handler' => $handler ] );
53 $r->execute();
54
55 $this->assertEquals( 200, $r->getStatus() );
56 $this->assertEquals( $this->exampleBodyText, $r->getContent() );
57 }
58
59 public function testSuccessConstructorCallback() {
60 $this->bodyTextReceived = '';
61 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
62 'status' => 200,
63 ], $this->exampleBodyText ) ] ) );
64 $r = new GuzzleHttpRequest( $this->exampleUrl, [
65 'callback' => [ $this, 'processHttpDataChunk' ],
66 'handler' => $handler,
67 ] );
68 $r->execute();
69
70 $this->assertEquals( 200, $r->getStatus() );
71 $this->assertEquals( $this->exampleBodyText, $this->bodyTextReceived );
72 }
73
74 public function testSuccessSetCallback() {
75 $this->bodyTextReceived = '';
76 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
77 'status' => 200,
78 ], $this->exampleBodyText ) ] ) );
79 $r = new GuzzleHttpRequest( $this->exampleUrl, [
80 'handler' => $handler,
81 ] );
82 $r->setCallback( [ $this, 'processHttpDataChunk' ] );
83 $r->execute();
84
85 $this->assertEquals( 200, $r->getStatus() );
86 $this->assertEquals( $this->exampleBodyText, $this->bodyTextReceived );
87 }
88
89 /**
90 * use a callback stream to pipe the mocked response data to our callback function
91 */
92 public function testSuccessSink() {
93 $this->bodyTextReceived = '';
94 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
95 'status' => 200,
96 ], $this->exampleBodyText ) ] ) );
97 $r = new GuzzleHttpRequest( $this->exampleUrl, [
98 'handler' => $handler,
99 'sink' => new MWCallbackStream( [ $this, 'processHttpDataChunk' ] ),
100 ] );
101 $r->execute();
102
103 $this->assertEquals( 200, $r->getStatus() );
104 $this->assertEquals( $this->exampleBodyText, $this->bodyTextReceived );
105 }
106
107 public function testBadUrl() {
108 $r = new GuzzleHttpRequest( '' );
109 $s = $r->execute();
110 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
111
112 $this->assertSame( 0, $r->getStatus() );
113 $this->assertEquals( 'http-invalid-url', $errorMsg );
114 }
115
116 public function testConnectException() {
117 $handler = HandlerStack::create( new MockHandler( [ new GuzzleHttp\Exception\ConnectException(
118 'Mock Connection Exception', new Request( 'GET', $this->exampleUrl )
119 ) ] ) );
120 $r = new GuzzleHttpRequest( $this->exampleUrl, [ 'handler' => $handler ] );
121 $s = $r->execute();
122 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
123
124 $this->assertSame( 0, $r->getStatus() );
125 $this->assertEquals( 'http-request-error', $errorMsg );
126 }
127
128 public function testTimeout() {
129 $handler = HandlerStack::create( new MockHandler( [ new GuzzleHttp\Exception\RequestException(
130 'Connection timed out', new Request( 'GET', $this->exampleUrl )
131 ) ] ) );
132 $r = new GuzzleHttpRequest( $this->exampleUrl, [ 'handler' => $handler ] );
133 $s = $r->execute();
134 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
135
136 $this->assertSame( 0, $r->getStatus() );
137 $this->assertEquals( 'http-timed-out', $errorMsg );
138 }
139
140 public function testNotFound() {
141 $handler = HandlerStack::create( new MockHandler( [ new Response( 404, [
142 'status' => '404',
143 ] ) ] ) );
144 $r = new GuzzleHttpRequest( $this->exampleUrl, [ 'handler' => $handler ] );
145 $s = $r->execute();
146 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
147
148 $this->assertEquals( 404, $r->getStatus() );
149 $this->assertEquals( 'http-bad-status', $errorMsg );
150 }
151 }