MediaWiki  1.33.0
GuzzleHttpRequestTest.php
Go to the documentation of this file.
1 <?php
2 
3 use GuzzleHttp\Handler\MockHandler;
4 use GuzzleHttp\HandlerStack;
5 use GuzzleHttp\Psr7\Response;
6 use GuzzleHttp\Psr7\Request;
7 
22  protected $exampleUrl = 'http://www.example.test';
23 
28  protected $exampleBodyText = 'x';
29 
34  protected $bodyTextReceived = '';
35 
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 
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->assertEquals( 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->assertEquals( 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->assertEquals( 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 }
GuzzleHttpRequestTest\testNotFound
testNotFound()
Definition: GuzzleHttpRequestTest.php:140
$req
this hook is for auditing only $req
Definition: hooks.txt:979
$s
$s
Definition: mergeMessageFileList.php:186
GuzzleHttpRequestTest\testSuccessSink
testSuccessSink()
use a callback stream to pipe the mocked response data to our callback function
Definition: GuzzleHttpRequestTest.php:92
GuzzleHttpRequestTest\testTimeout
testTimeout()
Definition: GuzzleHttpRequestTest.php:128
GuzzleHttpRequestTest\testSuccess
testSuccess()
Definition: GuzzleHttpRequestTest.php:48
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
GuzzleHttpRequestTest\processHttpDataChunk
processHttpDataChunk( $req, $buffer)
Callback: process a chunk of the result of a HTTP request.
Definition: GuzzleHttpRequestTest.php:43
$handler
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:780
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
GuzzleHttpRequest
MWHttpRequest implemented using the Guzzle library.
Definition: GuzzleHttpRequest.php:39
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
GuzzleHttpRequestTest\testBadUrl
testBadUrl()
Definition: GuzzleHttpRequestTest.php:107
GuzzleHttpRequestTest\$bodyTextReceived
string $bodyTextReceived
For accumulating callback data for testing.
Definition: GuzzleHttpRequestTest.php:34
GuzzleHttpRequestTest\testSuccessSetCallback
testSuccessSetCallback()
Definition: GuzzleHttpRequestTest.php:74
GuzzleHttpRequestTest\testSuccessConstructorCallback
testSuccessConstructorCallback()
Definition: GuzzleHttpRequestTest.php:59
GuzzleHttpRequestTest\$exampleBodyText
string $exampleBodyText
Minimal example body text.
Definition: GuzzleHttpRequestTest.php:28
MWCallbackStream
Callback-aware stream.
Definition: MWCallbackStream.php:33
GuzzleHttpRequestTest\testConnectException
testConnectException()
Definition: GuzzleHttpRequestTest.php:116
GuzzleHttpRequestTest\$exampleUrl
string $exampleUrl
Placeholder url to use for various tests.
Definition: GuzzleHttpRequestTest.php:22
$buffer
$buffer
Definition: mwdoc-filter.php:49
GuzzleHttpRequestTest
class for tests of GuzzleHttpRequest
Definition: GuzzleHttpRequestTest.php:16