3 use GuzzleHttp\Handler\MockHandler
;
4 use GuzzleHttp\HandlerStack
;
5 use GuzzleHttp\Psr7\Response
;
6 use GuzzleHttp\Psr7\Request
;
9 * class for tests of GuzzleHttpRequest
11 * No actual requests are made herein - all external communications are mocked
13 * @covers GuzzleHttpRequest
14 * @covers MWHttpRequest
16 class GuzzleHttpRequestTest
extends MediaWikiTestCase
{
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.
22 protected $exampleUrl = 'http://www.example.test';
25 * Minimal example body text
28 protected $exampleBodyText = 'x';
31 * For accumulating callback data for testing
34 protected $bodyTextReceived = '';
37 * Callback: process a chunk of the result of a HTTP request
40 * @param string $buffer
41 * @return int Number of bytes handled
43 public function processHttpDataChunk( $req, $buffer ) {
44 $this->bodyTextReceived
.= $buffer;
45 return strlen( $buffer );
48 public function testSuccess() {
49 $handler = HandlerStack
::create( new MockHandler( [ new Response( 200, [
51 ], $this->exampleBodyText
) ] ) );
52 $r = new GuzzleHttpRequest( $this->exampleUrl
, [ 'handler' => $handler ] );
55 $this->assertEquals( 200, $r->getStatus() );
56 $this->assertEquals( $this->exampleBodyText
, $r->getContent() );
59 public function testSuccessConstructorCallback() {
60 $this->bodyTextReceived
= '';
61 $handler = HandlerStack
::create( new MockHandler( [ new Response( 200, [
63 ], $this->exampleBodyText
) ] ) );
64 $r = new GuzzleHttpRequest( $this->exampleUrl
, [
65 'callback' => [ $this, 'processHttpDataChunk' ],
66 'handler' => $handler,
70 $this->assertEquals( 200, $r->getStatus() );
71 $this->assertEquals( $this->exampleBodyText
, $this->bodyTextReceived
);
74 public function testSuccessSetCallback() {
75 $this->bodyTextReceived
= '';
76 $handler = HandlerStack
::create( new MockHandler( [ new Response( 200, [
78 ], $this->exampleBodyText
) ] ) );
79 $r = new GuzzleHttpRequest( $this->exampleUrl
, [
80 'handler' => $handler,
82 $r->setCallback( [ $this, 'processHttpDataChunk' ] );
85 $this->assertEquals( 200, $r->getStatus() );
86 $this->assertEquals( $this->exampleBodyText
, $this->bodyTextReceived
);
90 * use a callback stream to pipe the mocked response data to our callback function
92 public function testSuccessSink() {
93 $this->bodyTextReceived
= '';
94 $handler = HandlerStack
::create( new MockHandler( [ new Response( 200, [
96 ], $this->exampleBodyText
) ] ) );
97 $r = new GuzzleHttpRequest( $this->exampleUrl
, [
98 'handler' => $handler,
99 'sink' => new MWCallbackStream( [ $this, 'processHttpDataChunk' ] ),
103 $this->assertEquals( 200, $r->getStatus() );
104 $this->assertEquals( $this->exampleBodyText
, $this->bodyTextReceived
);
107 public function testBadUrl() {
108 $r = new GuzzleHttpRequest( '' );
110 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
112 $this->assertEquals( 0, $r->getStatus() );
113 $this->assertEquals( 'http-invalid-url', $errorMsg );
116 public function testConnectException() {
117 $handler = HandlerStack
::create( new MockHandler( [ new GuzzleHttp\Exception\
ConnectException(
118 'Mock Connection Exception', new Request( 'GET', $this->exampleUrl
)
120 $r = new GuzzleHttpRequest( $this->exampleUrl
, [ 'handler' => $handler ] );
122 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
124 $this->assertEquals( 0, $r->getStatus() );
125 $this->assertEquals( 'http-request-error', $errorMsg );
128 public function testTimeout() {
129 $handler = HandlerStack
::create( new MockHandler( [ new GuzzleHttp\Exception\
RequestException(
130 'Connection timed out', new Request( 'GET', $this->exampleUrl
)
132 $r = new GuzzleHttpRequest( $this->exampleUrl
, [ 'handler' => $handler ] );
134 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
136 $this->assertEquals( 0, $r->getStatus() );
137 $this->assertEquals( 'http-timed-out', $errorMsg );
140 public function testNotFound() {
141 $handler = HandlerStack
::create( new MockHandler( [ new Response( 404, [
144 $r = new GuzzleHttpRequest( $this->exampleUrl
, [ 'handler' => $handler ] );
146 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
148 $this->assertEquals( 404, $r->getStatus() );
149 $this->assertEquals( 'http-bad-status', $errorMsg );