MediaWiki REL1_33
GuzzleHttpRequestTest.php
Go to the documentation of this file.
1<?php
2
3use GuzzleHttp\Handler\MockHandler;
4use GuzzleHttp\HandlerStack;
5use GuzzleHttp\Middleware;
6use GuzzleHttp\Psr7\Response;
7use GuzzleHttp\Psr7\Request;
8
23 protected $exampleUrl = 'http://www.example.test';
24
29 protected $exampleBodyText = 'x';
30
35 protected $bodyTextReceived = '';
36
44 public function processHttpDataChunk( $req, $buffer ) {
45 $this->bodyTextReceived .= $buffer;
46 return strlen( $buffer );
47 }
48
49 public function testSuccess() {
50 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
51 'status' => 200,
52 ], $this->exampleBodyText ) ] ) );
53 $r = new GuzzleHttpRequest( $this->exampleUrl, [ 'handler' => $handler ] );
54 $r->execute();
55
56 $this->assertEquals( 200, $r->getStatus() );
57 $this->assertEquals( $this->exampleBodyText, $r->getContent() );
58 }
59
61 $this->bodyTextReceived = '';
62 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
63 'status' => 200,
64 ], $this->exampleBodyText ) ] ) );
65 $r = new GuzzleHttpRequest( $this->exampleUrl, [
66 'callback' => [ $this, 'processHttpDataChunk' ],
67 'handler' => $handler,
68 ] );
69 $r->execute();
70
71 $this->assertEquals( 200, $r->getStatus() );
72 $this->assertEquals( $this->exampleBodyText, $this->bodyTextReceived );
73 }
74
75 public function testSuccessSetCallback() {
76 $this->bodyTextReceived = '';
77 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
78 'status' => 200,
79 ], $this->exampleBodyText ) ] ) );
80 $r = new GuzzleHttpRequest( $this->exampleUrl, [
81 'handler' => $handler,
82 ] );
83 $r->setCallback( [ $this, 'processHttpDataChunk' ] );
84 $r->execute();
85
86 $this->assertEquals( 200, $r->getStatus() );
87 $this->assertEquals( $this->exampleBodyText, $this->bodyTextReceived );
88 }
89
93 public function testSuccessSink() {
94 $this->bodyTextReceived = '';
95 $handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
96 'status' => 200,
97 ], $this->exampleBodyText ) ] ) );
98 $r = new GuzzleHttpRequest( $this->exampleUrl, [
99 'handler' => $handler,
100 'sink' => new MWCallbackStream( [ $this, 'processHttpDataChunk' ] ),
101 ] );
102 $r->execute();
103
104 $this->assertEquals( 200, $r->getStatus() );
105 $this->assertEquals( $this->exampleBodyText, $this->bodyTextReceived );
106 }
107
108 public function testBadUrl() {
109 $r = new GuzzleHttpRequest( '' );
110 $s = $r->execute();
111 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
112
113 $this->assertEquals( 0, $r->getStatus() );
114 $this->assertEquals( 'http-invalid-url', $errorMsg );
115 }
116
117 public function testConnectException() {
118 $handler = HandlerStack::create( new MockHandler( [ new GuzzleHttp\Exception\ConnectException(
119 'Mock Connection Exception', new Request( 'GET', $this->exampleUrl )
120 ) ] ) );
121 $r = new GuzzleHttpRequest( $this->exampleUrl, [ 'handler' => $handler ] );
122 $s = $r->execute();
123 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
124
125 $this->assertEquals( 0, $r->getStatus() );
126 $this->assertEquals( 'http-request-error', $errorMsg );
127 }
128
129 public function testTimeout() {
130 $handler = HandlerStack::create( new MockHandler( [ new GuzzleHttp\Exception\RequestException(
131 'Connection timed out', new Request( 'GET', $this->exampleUrl )
132 ) ] ) );
133 $r = new GuzzleHttpRequest( $this->exampleUrl, [ 'handler' => $handler ] );
134 $s = $r->execute();
135 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
136
137 $this->assertEquals( 0, $r->getStatus() );
138 $this->assertEquals( 'http-timed-out', $errorMsg );
139 }
140
141 public function testNotFound() {
142 $handler = HandlerStack::create( new MockHandler( [ new Response( 404, [
143 'status' => '404',
144 ] ) ] ) );
145 $r = new GuzzleHttpRequest( $this->exampleUrl, [ 'handler' => $handler ] );
146 $s = $r->execute();
147 $errorMsg = $s->getErrorsByType( 'error' )[0]['message'];
148
149 $this->assertEquals( 404, $r->getStatus() );
150 $this->assertEquals( 'http-bad-status', $errorMsg );
151 }
152
153 /*
154 * Test of POST requests header
155 */
156 public function testPostBody() {
157 $container = [];
158 $history = Middleware::history( $container );
159 $stack = HandlerStack::create();
160 $stack->push( $history );
161 $client = new GuzzleHttpRequest( $this->exampleUrl, [
162 'method' => 'POST',
163 'handler' => $stack,
164 'post' => 'key=value',
165 ] );
166 $client->execute();
167
168 $request = $container[0]['request'];
169 $this->assertEquals( 'POST', $request->getMethod() );
170 $this->assertEquals( 'application/x-www-form-urlencoded',
171 $request->getHeader( 'Content-Type' )[0] );
172 }
173}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
class for tests of GuzzleHttpRequest
string $exampleBodyText
Minimal example body text.
string $bodyTextReceived
For accumulating callback data for testing.
string $exampleUrl
Placeholder url to use for various tests.
testSuccessSink()
use a callback stream to pipe the mocked response data to our callback function
processHttpDataChunk( $req, $buffer)
Callback: process a chunk of the result of a HTTP request.
MWHttpRequest implemented using the Guzzle library.
Callback-aware stream.
this hook is for auditing only $req
Definition hooks.txt:979
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2843
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:894
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:37
$buffer