public function testProcessEntityRequest() { $io = new Google_CurlIO(); $req = new Google_HttpRequest("http://localhost.com"); $req->setRequestMethod("POST"); // Verify that the content-length is calculated. $req->setPostBody("{}"); $io->processEntityRequest($req); $this->assertEquals(2, $req->getRequestHeader("content-length")); // Test an empty post body. $req->setPostBody(""); $io->processEntityRequest($req); $this->assertEquals(0, $req->getRequestHeader("content-length")); // Test a null post body. $req->setPostBody(null); $io->processEntityRequest($req); $this->assertEquals(0, $req->getRequestHeader("content-length")); // Set an array in the postbody, and verify that it is url-encoded. $req->setPostBody(array("a" => "1", "b" => 2)); $io->processEntityRequest($req); $this->assertEquals(7, $req->getRequestHeader("content-length")); $this->assertEquals(Google_CurlIO::FORM_URLENCODED, $req->getRequestHeader("content-type")); $this->assertEquals("a=1&b=2", $req->getPostBody()); // Verify that the content-type isn't reset. $payload = array("a" => "1", "b" => 2); $req->setPostBody($payload); $req->setRequestHeaders(array("content-type" => "multipart/form-data")); $io->processEntityRequest($req); $this->assertEquals("multipart/form-data", $req->getRequestHeader("content-type")); $this->assertEquals($payload, $req->getPostBody()); }
public function execute() { $body = ''; /** @var Google_HttpRequest $req */ foreach ($this->requests as $key => $req) { $body .= "--{$this->boundary}\n"; $body .= $req->toBatchString($key) . "\n"; } $body = rtrim($body); $body .= "\n--{$this->boundary}--"; $url = $GLOBALS['googleApiConfig']['basePath'] . '/batch'; $httpRequest = new Google_HttpRequest($url, 'POST'); $httpRequest->setRequestHeaders(array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary)); $httpRequest->setPostBody($body); $response = Google_Client::$io->makeRequest($httpRequest); $response = $this->parseResponse($response); return $response; }
/** * @visible for testing * Process an http request that contains an enclosed entity. * @param Google_HttpRequest $request * @return Google_HttpRequest Processed request with the enclosed entity. */ public function processEntityRequest(Google_HttpRequest $request) { $postBody = $request->getPostBody(); $contentType = $request->getRequestHeader("content-type"); // Set the default content-type as application/x-www-form-urlencoded. if (false == $contentType) { $contentType = self::FORM_URLENCODED; $request->setRequestHeaders(array('content-type' => $contentType)); } // Force the payload to match the content-type asserted in the header. if ($contentType == self::FORM_URLENCODED && is_array($postBody)) { $postBody = http_build_query($postBody, '', '&'); $request->setPostBody($postBody); } // Make sure the content-length header is set. if (!$postBody || is_string($postBody)) { $postsLength = strlen($postBody); $request->setRequestHeaders(array('content-length' => $postsLength)); } return $request; }