Mimic CURLOPT_POST in GuzzleHttpRequest
authorMoritz Schubotz (physikerwelt) <wiki@physikerwelt.de>
Fri, 22 Nov 2019 15:11:26 +0000 (16:11 +0100)
committerPaladox <thomasmulhall410@yahoo.com>
Wed, 4 Dec 2019 16:28:48 +0000 (16:28 +0000)
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)

includes/http/GuzzleHttpRequest.php
tests/phpunit/includes/http/GuzzleHttpRequestTest.php

index fa6dad7..4741ead 100644 (file)
@@ -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
index ff0a9eb..7312dad 100644 (file)
@@ -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] );
+       }
 }