From: Moritz Schubotz (physikerwelt) Date: Fri, 22 Nov 2019 15:11:26 +0000 (+0100) Subject: Mimic CURLOPT_POST in GuzzleHttpRequest X-Git-Tag: 1.34.0~11 X-Git-Url: http://git.cyclocoop.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=ff4d49a42e42fc2ac3e6b33b774fdad7a3e40d6f Mimic CURLOPT_POST in GuzzleHttpRequest The MWHttpRequest is implemented by the CurlHttpRequest class and also the GuzzleHttpRequest class. However, curl based rendering set the CURLOPT_POST which implies that the 'Content-Type' header defaults to 'application/x-www-form-urlencoded'. To homgonize the functionality this patch mimics the curl behaviour in Guzzle. Bug: T232866 Change-Id: Id60a8de18e5f1e750a3bde23bd8b0deca4071165 (cherry picked from commit 5e3a0e73955d6324c5dd6e12fbe36d3ba203d9db) --- diff --git a/includes/http/GuzzleHttpRequest.php b/includes/http/GuzzleHttpRequest.php index fa6dad719f..4741eadba6 100644 --- a/includes/http/GuzzleHttpRequest.php +++ b/includes/http/GuzzleHttpRequest.php @@ -140,6 +140,10 @@ class GuzzleHttpRequest extends MWHttpRequest { $this->guzzleOptions['form_params'] = $postData; } else { $this->guzzleOptions['body'] = $postData; + // mimic CURLOPT_POST option + if ( !isset( $this->reqHeaders['Content-Type'] ) ) { + $this->reqHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; + } } // Suppress 'Expect: 100-continue' header, as some servers diff --git a/tests/phpunit/includes/http/GuzzleHttpRequestTest.php b/tests/phpunit/includes/http/GuzzleHttpRequestTest.php index ff0a9eb0bc..7312dad577 100644 --- a/tests/phpunit/includes/http/GuzzleHttpRequestTest.php +++ b/tests/phpunit/includes/http/GuzzleHttpRequestTest.php @@ -2,6 +2,7 @@ use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; +use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Request; @@ -148,4 +149,25 @@ class GuzzleHttpRequestTest extends MediaWikiTestCase { $this->assertEquals( 404, $r->getStatus() ); $this->assertEquals( 'http-bad-status', $errorMsg ); } + + /* + * Test of POST requests header + */ + public function testPostBody() { + $container = []; + $history = Middleware::history( $container ); + $stack = HandlerStack::create(); + $stack->push( $history ); + $client = new GuzzleHttpRequest( $this->exampleUrl, [ + 'method' => 'POST', + 'handler' => $stack, + 'post' => 'key=value', + ] ); + $client->execute(); + + $request = $container[0]['request']; + $this->assertEquals( 'POST', $request->getMethod() ); + $this->assertEquals( 'application/x-www-form-urlencoded', + $request->getHeader( 'Content-Type' )[0] ); + } }