Commit cc34a078 authored by Bas de Nooijer's avatar Bas de Nooijer

- refactored http client adapter

- improved unittests
parent 426de853
......@@ -48,10 +48,45 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
* Handle Solr communication
*
* @throws Solarium_Exception
* @param Solarium_Client_Request
* @param Solarium_Client_Request $request
* @return Solarium_Client_Response
*/
public function execute($request)
{
$context = $this->createContext($request);
$uri = $this->getBaseUri() . $request->getUri();
list($data, $headers) = $this->_getData($uri, $context);
$this->check($data, $headers);
return new Solarium_Client_Response($data, $headers);
}
/**
* Check result of a request
*
* @throws Solarium_Client_HttpException
* @param string $data
* @param array $headers
* @return void
*/
public function check($data, $headers)
{
// if there is no data and there are no headers it's a total failure,
// a connection to the host was impossible.
if (false === $data && count($headers) == 0) {
throw new Solarium_Client_HttpException("HTTP request failed");
}
}
/**
* Create a stream context for a request
*
* @param Solarium_Client_Request $request
* @return resource
*/
public function createContext($request)
{
$method = $request->getMethod();
$context = stream_context_create(
......@@ -70,26 +105,22 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
'content',
$data
);
//TODO header via request->setRawData!!
$request->addHeader('Content-Type: text/xml; charset=UTF-8');
}
}
$headers = $request->getHeaders();
if (count($headers) > 0) {
stream_context_set_option(
$context,
'http',
'header',
'Content-Type: text/xml; charset=UTF-8'
implode("\r\n", $headers)
);
}
}
$uri = $this->getBaseUri() . $request->getUri();
$data = $this->_getData($uri, $context);
// if there is no data and there are no headers it's a total failure,
// a connection to the host was impossible.
if (false === $data && !isset($http_response_header)) {
throw new Solarium_Client_HttpException("HTTP request failed");
}
return new Solarium_Client_Response($data, $http_response_header);
return $context;
}
/**
......@@ -97,11 +128,18 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
*
* @param string $uri
* @param resource $context
* @return string
* @return array
*/
protected function _getData($uri, $context)
{
return @file_get_contents($uri, false, $context);
$data = @file_get_contents($uri, false, $context);
if (isset($http_response_header)) {
$headers = $http_response_header;
} else {
$headers = array();
}
return array($data, $headers);
}
}
\ No newline at end of file
......@@ -63,7 +63,7 @@ class Solarium_Client_Request extends Solarium_Configurable
/**
* Request headers
*/
protected $_headers;
protected $_headers = array();
/**
* Request params
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*/
class Solarium_Client_Adapter_HttpTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Client_Adapter_Http
*/
protected $_adapter;
public function setUp()
{
$this->_adapter = new Solarium_Client_Adapter_Http();
}
public function testExecute()
{
$data = 'test123';
$request = new Solarium_Client_Request();
$request->setMethod(Solarium_Client_Request::METHOD_GET);
$mock = $this->getMock('Solarium_Client_Adapter_Http', array('_getData','check'));
$mock->expects($this->once())
->method('_getData')
->with($this->equalTo('http://127.0.0.1:8983/solr/?'), $this->isType('resource'))
->will($this->returnValue(array($data, array('HTTP 1.1 200 OK'))));
$mock->execute($request);
}
public function testExecuteErrorResponse()
{
$request = new Solarium_Client_Request();
$this->setExpectedException('Solarium_Client_HttpException');
$this->_adapter->execute($request);
}
public function testCheckError()
{
$this->setExpectedException('Solarium_Client_HttpException');
$this->_adapter->check(false, array());
}
public function testCheckOk()
{
$value = $this->_adapter->check('dummydata',array('HTTP 1.1 200 OK'));
$this->assertEquals(
null,
$value
);
}
public function testCreateContextGetRequest()
{
$timeout = 13;
$method = Solarium_Client_Request::METHOD_HEAD;
$request = new Solarium_Client_Request();
$request->setMethod($method);
$this->_adapter->setTimeout($timeout);
$context = $this->_adapter->createContext($request);
$this->assertEquals(
array('http' => array('method' => $method, 'timeout' => $timeout)),
stream_context_get_options($context)
);
}
public function testCreateContextWithHeaders()
{
$timeout = 13;
$method = Solarium_Client_Request::METHOD_HEAD;
$header1 = 'Content-Type: text/xml; charset=UTF-8';
$header2 = 'X-MyHeader: dummyvalue';
$request = new Solarium_Client_Request();
$request->setMethod($method);
$request->addHeader($header1);
$request->addHeader($header2);
$this->_adapter->setTimeout($timeout);
$context = $this->_adapter->createContext($request);
$this->assertEquals(
array('http' => array('method' => $method, 'timeout' => $timeout, 'header' => $header1."\r\n".$header2)),
stream_context_get_options($context)
);
}
public function testCreateContextPostRequest()
{
$timeout = 13;
$method = Solarium_Client_Request::METHOD_POST;
$data = 'test123';
$request = new Solarium_Client_Request();
$request->setMethod($method);
$request->setRawData($data);
$this->_adapter->setTimeout($timeout);
$context = $this->_adapter->createContext($request);
$this->assertEquals(
array('http' => array('method' => $method, 'timeout' => $timeout, 'content' => $data, 'header' => 'Content-Type: text/xml; charset=UTF-8')),
stream_context_get_options($context)
);
}
}
\ No newline at end of file
......@@ -75,9 +75,10 @@ class Solarium_Client_Adapter_ZendHttpTest extends PHPUnit_Framework_TestCase
public function testGetZendHttpAutoload()
{
$this->_adapter->setOptions(array('myoption', 123));
$zendHttp = $this->_adapter->getZendHttp();
$options = array('timeout' => 10, 'optionZ' => 123, 'options' => array('adapter' => 'Zend_Http_Client_Adapter_Curl'));
$this->_adapter->setOptions($options);
$zendHttp = $this->_adapter->getZendHttp();
$this->assertThat($zendHttp, $this->isInstanceOf('Zend_Http_Client'));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment