views:

460

answers:

1

I have Zend_Soap_Server. It should be tested. One issue - unit testing and development modes should work with different databases. It can be done via .htaccess:

SetEnvIfNoCase User-Agent (.*) APPLICATION_ENV=development
SetEnvIfNoCase User-Agent testing APPLICATION_ENV=testing

It would work fine for me, I use Zend_Soap_Client as is for development/production mode, and add extra parameter for testing. There are no special changes in source code for testing support. The issue - I can't set custom user-agent for (Zend_Soap_Client It seems as this parameter isn't supported or isn't documented. I tried to do the same thing via mod_rewrite and adding

new Zend_Soap_Client('...?wsdl&testing');

but it requires to change Zend_Soap_Server to support testing Query_String, otherwise soap-actions aren't reflected with 'testing' outside of wsdl. It's not good in my opinion.

I agree it's a bit more related with acceptance testing, not unit (I have tests for classes such as $server->setClass('classWS')), but anyway, I need it regardless of terminology.

So, what would work fine for me is something like:

new Zend_Soap_Client($wsdl, array('useragent' => 'testing'));

in tests.

+1  A: 

It's possible but it's a bit more complicated than just setting an option. The key is a stream context. The required function is stream_context_create() - please also have a look at "HTTP context options".

$context = stream_context_create(array(
    'http' => array(
        'user_agent' => 'testing'
    )
);
$client = new Zend_Soap_Client($wsdl, array('stream_context' => $context));

// or set option after instatiation
$client->setStreamContext($context);

EDIT:

As the stream context user-agent seems to get overridden another option is to use the user_agent-option of SoapClient itself. But this is a little bit more complicated as this option is not exposed by Zend_Soap_Client.

$client = new Zend_Soap_Client($wsdl);
$options = array_merge($client->getOptions(), array(
    'trace'      => true,
    'user_agent' => 'testing'
));
$soapClient = new Zend_Soap_Client_Common(array($client, '_doRequest'), $wsdl, $options);
$client->setSoapClient($soapClient);

The above code is more or less an extract of what happens in Zend_Soap_Client::_initSoapClientObject() which initializes the default SoapClient when no custom object is registered.

Stefan Gehrig
Thank you for your answer!Unfortunately, I've got $_SERVER['HTTP_USER_AGENT'] => "PHP-SOAP/5.2.6-3ubuntu4.2" even if user_agent is defined via stream_context_create (I tried both your cases).My guess, it's because: header string Additional headers to be sent during request. Values in this option will override other values (such as User-agent:, Host:, and Authentication:).May be Zend adds header option, but I'm not sure
Alexey
Hi Alexey, I added another option you can try. It seems as if the user-agent passed in through a stream context gets overriden by the `SoapClient` itself.
Stefan Gehrig
Thank you again!I realized that ['http']['user_agent'] option doesn't impact native php SoapClient (so, Zend_Soap_Client isn't a reason by itself).I'm not sure is it desired behavior or not, but anyway I think it make sense me to just report to Zend team and ask about enabling 'user_agent' Zend_Soap_Client construct option (because it would work fine - I checked). Anyway, thanks for your solution - it work even if seems not so pretty!
Alexey
My final checking (without Zend):$opts = array('http' => array( 'user_agent' => 'testing' ));$context = stream_context_create($opts);$client = new SoapClient($wsdl, array( 'context_stream' => $context // doesn't change user-agent //'user_agent' => 'testing' // uncomment - work as desired! ));...So, I have no idea why Zend has forbidden this option.
Alexey
This has been fixed in r18569 of the Zend Framework trunk and will be part of the next Framework releas (either 1.9.5 or 1.10.0).
Stefan Gehrig