diff --git a/library/Solarium/Core/Client/Adapter/AdapterHelper.php b/library/Solarium/Core/Client/Adapter/AdapterHelper.php new file mode 100644 index 000000000..5cac1726e --- /dev/null +++ b/library/Solarium/Core/Client/Adapter/AdapterHelper.php @@ -0,0 +1,30 @@ +getFileUpload(); + $body = "--{$request->getHash()}\r\n"; + $body .= 'Content-Disposition: form-data; name="file"; filename="'.$filename.'"'; + $body .= "\r\nContent-Type: application/octet-stream\r\n\r\n"; + $body .= file_get_contents($request->getFileUpload(), 'r'); + $body .= "\r\n--{$request->getHash()}--\r\n"; + + return $body; + } +} diff --git a/library/Solarium/Core/Client/Adapter/Guzzle.php b/library/Solarium/Core/Client/Adapter/Guzzle.php index 46b351d1e..130c92781 100644 --- a/library/Solarium/Core/Client/Adapter/Guzzle.php +++ b/library/Solarium/Core/Client/Adapter/Guzzle.php @@ -88,7 +88,8 @@ public function execute($request, $endpoint) } try { - $guzzleResponse = $this->getGuzzleClient()->request( + $guzzleClient = $this->getGuzzleClient(); + $guzzleResponse = $guzzleClient->request( $request->getMethod(), $endpoint->getBaseUri() . $request->getUri(), $requestOptions @@ -138,7 +139,8 @@ private function getRequestBody(Request $request) } if ($request->getFileUpload()) { - return fopen($request->getFileUpload(), 'r'); + $helper = new AdapterHelper(); + return $helper->buildUploadBodyFromRequest($request); } return $request->getRawData(); diff --git a/library/Solarium/Core/Client/Adapter/Guzzle3.php b/library/Solarium/Core/Client/Adapter/Guzzle3.php index 56b32d538..e31c53345 100644 --- a/library/Solarium/Core/Client/Adapter/Guzzle3.php +++ b/library/Solarium/Core/Client/Adapter/Guzzle3.php @@ -138,7 +138,9 @@ private function getRequestBody(Request $request) } if ($request->getFileUpload()) { - return fopen($request->getFileUpload(), 'r'); + $helper = new AdapterHelper(); + $body = $helper->buildUploadBodyFromRequest($request); + return $body; } return $request->getRawData(); diff --git a/library/Solarium/Core/Client/Request.php b/library/Solarium/Core/Client/Request.php index acc02aa94..0245694c3 100644 --- a/library/Solarium/Core/Client/Request.php +++ b/library/Solarium/Core/Client/Request.php @@ -493,4 +493,12 @@ protected function init() } } } + + /** + * @return string + */ + public function getHash() + { + return spl_object_hash($this); + } } diff --git a/library/Solarium/QueryType/Extract/RequestBuilder.php b/library/Solarium/QueryType/Extract/RequestBuilder.php index b31f5bad1..07228c007 100644 --- a/library/Solarium/QueryType/Extract/RequestBuilder.php +++ b/library/Solarium/QueryType/Extract/RequestBuilder.php @@ -55,10 +55,10 @@ class RequestBuilder extends BaseRequestBuilder /** * Build the request. * - * @throws RuntimeException - * * @param Query|QueryInterface $query * + * @throws RuntimeException + * * @return Request */ public function build(QueryInterface $query) @@ -107,6 +107,7 @@ public function build(QueryInterface $query) } elseif (is_readable($file)) { $request->setFileUpload($file); $request->addParam('resource.name', basename($query->getFile())); + $request->addHeader('Content-Type: multipart/form-data; boundary='.$request->getHash()); } else { throw new RuntimeException('Extract query file path/url invalid or not available'); } diff --git a/tests/Solarium/Tests/Core/Client/Adapter/Guzzle3Test.php b/tests/Solarium/Tests/Core/Client/Adapter/Guzzle3Test.php index d725dc481..87a7299bd 100644 --- a/tests/Solarium/Tests/Core/Client/Adapter/Guzzle3Test.php +++ b/tests/Solarium/Tests/Core/Client/Adapter/Guzzle3Test.php @@ -151,7 +151,7 @@ public function executePostWithFile() $this->assertCount(1, $receivedRequests); $this->assertSame('POST', $receivedRequests[0]->getMethod()); - $this->assertStringEqualsFile(__FILE__, (string)$receivedRequests[0]->getBody()); + $this->assertContains(file_get_contents(__FILE__), (string) $receivedRequests[0]->getBody()); $this->assertSame( 'request value', (string)$receivedRequests[0]->getHeader('X-PHPUnit') diff --git a/tests/Solarium/Tests/Core/Client/Adapter/GuzzleTest.php b/tests/Solarium/Tests/Core/Client/Adapter/GuzzleTest.php index 9c1f8ea52..e8acf2042 100644 --- a/tests/Solarium/Tests/Core/Client/Adapter/GuzzleTest.php +++ b/tests/Solarium/Tests/Core/Client/Adapter/GuzzleTest.php @@ -154,7 +154,7 @@ public function executePostWithFile() $this->assertCount(1, $container); $this->assertSame('POST', $container[0]['request']->getMethod()); $this->assertSame('request value', $container[0]['request']->getHeaderline('X-PHPUnit')); - $this->assertStringEqualsFile(__FILE__, (string)$container[0]['request']->getBody()); + $this->assertContains(file_get_contents(__FILE__), (string) $container[0]['request']->getBody()); } /** diff --git a/tests/Solarium/Tests/QueryType/Extract/RequestBuilderTest.php b/tests/Solarium/Tests/QueryType/Extract/RequestBuilderTest.php index 3d64ba683..d8f7a73c4 100644 --- a/tests/Solarium/Tests/QueryType/Extract/RequestBuilderTest.php +++ b/tests/Solarium/Tests/QueryType/Extract/RequestBuilderTest.php @@ -134,8 +134,10 @@ public function testDocumentWithBoostThrowsException() public function testContentTypeHeader() { - $headers = array(); $request = $this->builder->build($this->query); + $headers = [ + 'Content-Type: multipart/form-data; boundary='.$request->getHash(), + ]; $this->assertEquals($headers, $request->getHeaders()); }