Support cookies in GuzzleHttpRequest
Currently GuzzleHttpRequest is not sending any cookies. Furthermore, Guzzle expects its own format of CookieJar, which is not compatible with CookieJar used by MediaWiki. Solution is to add Guzzle Middleware that would obtain Cookie header from MediaWiki CookieJar and add it to outgoing Guzzle requests. Note: special handling of received cookies (Set-Cookie header from the server) is unnecessary, this is not Guzzle-specific code and is already done in MWHttpRequest::parseCookies(). Still, adding both a test of sending cookies and receiving cookies. Bug: T245644 Change-Id: If69840e65d5671989cf15450964da3c691fd164a
This commit is contained in:
parent
eb6c5f70d9
commit
116409e279
2 changed files with 74 additions and 4 deletions
|
|
@ -19,7 +19,10 @@
|
|||
*/
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Middleware;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
/**
|
||||
* MWHttpRequest implemented using the Guzzle library
|
||||
|
|
@ -154,9 +157,32 @@ class GuzzleHttpRequest extends MWHttpRequest {
|
|||
|
||||
$this->guzzleOptions['headers'] = $this->reqHeaders;
|
||||
|
||||
if ( $this->handler ) {
|
||||
$this->guzzleOptions['handler'] = $this->handler;
|
||||
}
|
||||
// Create Middleware to use cookies from $this->getCookieJar(),
|
||||
// which is in MediaWiki CookieJar format, not in Guzzle-specific CookieJar format.
|
||||
// Note: received cookies (from HTTP response) don't need to be handled here,
|
||||
// they will be added back into the CookieJar by MWHttpRequest::parseCookies().
|
||||
$stack = HandlerStack::create( $this->handler );
|
||||
|
||||
// @phan-suppress-next-line PhanUndeclaredFunctionInCallable
|
||||
$stack->remove( 'cookies' );
|
||||
|
||||
$mwCookieJar = $this->getCookieJar();
|
||||
$stack->push( Middleware::mapRequest(
|
||||
function ( RequestInterface $request ) use ( $mwCookieJar ) {
|
||||
$uri = $request->getUri();
|
||||
$cookieHeader = $mwCookieJar->serializeToHttpRequest(
|
||||
$uri->getPath() ?: '/',
|
||||
$uri->getHost()
|
||||
);
|
||||
if ( !$cookieHeader ) {
|
||||
return $request;
|
||||
}
|
||||
|
||||
return $request->withHeader( 'Cookie', $cookieHeader );
|
||||
}
|
||||
), 'cookies' );
|
||||
|
||||
$this->guzzleOptions['handler'] = $stack;
|
||||
|
||||
if ( $this->sink ) {
|
||||
$this->guzzleOptions['sink'] = $this->sink;
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ class GuzzleHttpRequestTest extends MediaWikiTestCase {
|
|||
public function testPostBody() {
|
||||
$container = [];
|
||||
$history = Middleware::history( $container );
|
||||
$stack = HandlerStack::create();
|
||||
$stack = HandlerStack::create( new MockHandler( [ new Response() ] ) );
|
||||
$stack->push( $history );
|
||||
$client = new GuzzleHttpRequest( $this->exampleUrl, [
|
||||
'method' => 'POST',
|
||||
|
|
@ -170,4 +170,48 @@ class GuzzleHttpRequestTest extends MediaWikiTestCase {
|
|||
$this->assertEquals( 'application/x-www-form-urlencoded',
|
||||
$request->getHeader( 'Content-Type' )[0] );
|
||||
}
|
||||
|
||||
/*
|
||||
* Test that cookies from CookieJar were sent in the outgoing request.
|
||||
*/
|
||||
public function testCookieSent() {
|
||||
$domain = wfParseUrl( $this->exampleUrl )['host'];
|
||||
$expectedCookies = [ 'cookie1' => 'value1', 'anothercookie' => 'secondvalue' ];
|
||||
$jar = new CookieJar;
|
||||
foreach ( $expectedCookies as $key => $val ) {
|
||||
$jar->setCookie( $key, $val, [ 'domain' => $domain ] );
|
||||
}
|
||||
|
||||
$container = [];
|
||||
$history = Middleware::history( $container );
|
||||
$stack = HandlerStack::create( new MockHandler( [ new Response() ] ) );
|
||||
$stack->push( $history );
|
||||
$client = new GuzzleHttpRequest( $this->exampleUrl, [
|
||||
'method' => 'POST',
|
||||
'handler' => $stack,
|
||||
'post' => 'key=value',
|
||||
] );
|
||||
$client->setCookieJar( $jar );
|
||||
$client->execute();
|
||||
|
||||
$request = $container[0]['request'];
|
||||
$this->assertEquals( [ 'cookie1=value1; anothercookie=secondvalue' ],
|
||||
$request->getHeader( 'Cookie' ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* Test that cookies returned by HTTP response were added back into the CookieJar.
|
||||
*/
|
||||
public function testCookieReceived() {
|
||||
$handler = HandlerStack::create( new MockHandler( [ new Response( 200, [
|
||||
'status' => 200,
|
||||
'Set-Cookie' => [ 'cookie1=value1', 'anothercookie=secondvalue' ]
|
||||
] ) ] ) );
|
||||
$r = new GuzzleHttpRequest( $this->exampleUrl, [ 'handler' => $handler ] );
|
||||
$r->execute();
|
||||
|
||||
$domain = wfParseUrl( $this->exampleUrl )['host'];
|
||||
$this->assertEquals( 'cookie1=value1; anothercookie=secondvalue',
|
||||
$r->getCookieJar()->serializeToHttpRequest( '/', $domain ) );
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue