Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
solarium
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
solarium
Commits
cc34a078
Commit
cc34a078
authored
May 18, 2011
by
Bas de Nooijer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- refactored http client adapter
- improved unittests
parent
426de853
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
203 additions
and
21 deletions
+203
-21
library/Solarium/Client/Adapter/Http.php
library/Solarium/Client/Adapter/Http.php
+56
-18
library/Solarium/Client/Request.php
library/Solarium/Client/Request.php
+1
-1
tests/Solarium/Client/Adapter/HttpTest.php
tests/Solarium/Client/Adapter/HttpTest.php
+143
-0
tests/Solarium/Client/Adapter/ZendHttpTest.php
tests/Solarium/Client/Adapter/ZendHttpTest.php
+3
-2
No files found.
library/Solarium/Client/Adapter/Http.php
View file @
cc34a078
...
@@ -48,10 +48,45 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
...
@@ -48,10 +48,45 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
* Handle Solr communication
* Handle Solr communication
*
*
* @throws Solarium_Exception
* @throws Solarium_Exception
* @param Solarium_Client_Request
* @param Solarium_Client_Request
$request
* @return Solarium_Client_Response
* @return Solarium_Client_Response
*/
*/
public
function
execute
(
$request
)
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
();
$method
=
$request
->
getMethod
();
$context
=
stream_context_create
(
$context
=
stream_context_create
(
...
@@ -70,26 +105,22 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
...
@@ -70,26 +105,22 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
'content'
,
'content'
,
$data
$data
);
);
//TODO header via request->setRawData!!
stream_context_set_option
(
$request
->
addHeader
(
'Content-Type: text/xml; charset=UTF-8'
);
$context
,
'http'
,
'header'
,
'Content-Type: text/xml; charset=UTF-8'
);
}
}
}
}
$uri
=
$this
->
getBaseUri
()
.
$request
->
getUri
();
$headers
=
$request
->
getHeaders
();
$data
=
$this
->
_getData
(
$uri
,
$context
);
if
(
count
(
$headers
)
>
0
)
{
stream_context_set_option
(
// if there is no data and there are no headers it's a total failure,
$context
,
// a connection to the host was impossible.
'http'
,
if
(
false
===
$data
&&
!
isset
(
$http_response_header
))
{
'header'
,
throw
new
Solarium_Client_HttpException
(
"HTTP request failed"
);
implode
(
"
\r\n
"
,
$headers
)
);
}
}
return
new
Solarium_Client_Response
(
$data
,
$http_response_header
)
;
return
$context
;
}
}
/**
/**
...
@@ -97,11 +128,18 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
...
@@ -97,11 +128,18 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
*
*
* @param string $uri
* @param string $uri
* @param resource $context
* @param resource $context
* @return
string
* @return
array
*/
*/
protected
function
_getData
(
$uri
,
$context
)
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
library/Solarium/Client/Request.php
View file @
cc34a078
...
@@ -63,7 +63,7 @@ class Solarium_Client_Request extends Solarium_Configurable
...
@@ -63,7 +63,7 @@ class Solarium_Client_Request extends Solarium_Configurable
/**
/**
* Request headers
* Request headers
*/
*/
protected
$_headers
;
protected
$_headers
=
array
()
;
/**
/**
* Request params
* Request params
...
...
tests/Solarium/Client/Adapter/HttpTest.php
0 → 100644
View file @
cc34a078
<?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
tests/Solarium/Client/Adapter/ZendHttpTest.php
View file @
cc34a078
...
@@ -75,9 +75,10 @@ class Solarium_Client_Adapter_ZendHttpTest extends PHPUnit_Framework_TestCase
...
@@ -75,9 +75,10 @@ class Solarium_Client_Adapter_ZendHttpTest extends PHPUnit_Framework_TestCase
public
function
testGetZendHttpAutoload
()
public
function
testGetZendHttpAutoload
()
{
{
$
this
->
_adapter
->
setOptions
(
array
(
'myoption'
,
123
));
$
options
=
array
(
'timeout'
=>
10
,
'optionZ'
=>
123
,
'options'
=>
array
(
'adapter'
=>
'Zend_Http_Client_Adapter_Curl'
));
$
zendHttp
=
$this
->
_adapter
->
getZendHttp
(
);
$
this
->
_adapter
->
setOptions
(
$options
);
$zendHttp
=
$this
->
_adapter
->
getZendHttp
();
$this
->
assertThat
(
$zendHttp
,
$this
->
isInstanceOf
(
'Zend_Http_Client'
));
$this
->
assertThat
(
$zendHttp
,
$this
->
isInstanceOf
(
'Zend_Http_Client'
));
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment