Skip to content

Commit 59eb60c

Browse files
committed
Allow overriding all Guzzle configuration settings
1 parent 1290340 commit 59eb60c

File tree

2 files changed

+122
-18
lines changed

2 files changed

+122
-18
lines changed

src/Guzzle/ClientFactory.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ClientFactory implements LoggerAwareInterface {
2323
/**
2424
* @since 2.1.0
2525
*
26-
* @param array $config with possible keys:
26+
* @param array $config All configurtion settings supported by Guzzle, and these:
2727
* middleware => array of extra middleware to pass to guzzle
2828
* user-agent => string default user agent to use for requests
2929
*/
@@ -48,29 +48,36 @@ public function getClient() {
4848
* @return Client
4949
*/
5050
private function newClient() {
51-
$middlewareFactory = new MiddlewareFactory();
52-
$middlewareFactory->setLogger( $this->logger );
51+
$this->config += array(
52+
'cookies' => true,
53+
'headers' => array(),
54+
'middleware' => array(),
55+
);
5356

54-
$handlerStack = HandlerStack::create( new CurlHandler() );
55-
$handlerStack->push( $middlewareFactory->retry() );
57+
if( !array_key_exists( 'User-Agent', $this->config['headers'] ) ) {
58+
if( array_key_exists( 'user-agent', $this->config ) ) {
59+
$this->config['headers']['User-Agent'] = $this->config['user-agent'];
60+
} else {
61+
$this->config['headers']['User-Agent'] = 'Addwiki - mediawiki-api-base';
62+
}
63+
}
64+
unset( $this->config['user-agent'] );
5665

57-
if( array_key_exists( 'user-agent', $this->config ) ) {
58-
$ua = $this->config['user-agent'];
59-
} else {
60-
$ua = 'Addwiki - mediawiki-api-base';
66+
if( !array_key_exists( 'handler', $this->config ) ) {
67+
$this->config['handler'] = HandlerStack::create( new CurlHandler() );
6168
}
6269

63-
if( array_key_exists( 'middleware', $this->config ) ) {
64-
foreach( $this->config['middleware'] as $middleware ) {
65-
$handlerStack->push( $middleware );
66-
}
70+
$middlewareFactory = new MiddlewareFactory();
71+
$middlewareFactory->setLogger( $this->logger );
72+
73+
$this->config['middleware'][] = $middlewareFactory->retry();
74+
75+
foreach( $this->config['middleware'] as $name => $middleware ) {
76+
$this->config['handler']->push( $middleware );
6777
}
78+
unset( $this->config['middleware'] );
6879

69-
return new Client( array(
70-
'cookies' => true,
71-
'handler' => $handlerStack,
72-
'headers' => array( 'User-Agent' => $ua ),
73-
) );
80+
return new Client( $this->config );
7481
}
7582

7683
/**
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Mediawiki\Api\Test\Unit\Guzzle;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\ConnectException;
7+
use GuzzleHttp\Handler\MockHandler;
8+
use GuzzleHttp\HandlerStack;
9+
use GuzzleHttp\Psr7\Request;
10+
use GuzzleHttp\Psr7\Response;
11+
use Mediawiki\Api\Guzzle\ClientFactory;
12+
13+
/**
14+
* @author Addshore
15+
*
16+
* @todo test interaction with logger
17+
*
18+
* @covers Mediawiki\Api\Guzzle\ClientFactory
19+
*/
20+
class ClientFactoryTest extends \PHPUnit_Framework_TestCase {
21+
22+
public function testNoConfig() {
23+
$clientFactory = new ClientFactory();
24+
25+
$client = $clientFactory->getClient();
26+
27+
$this->assertSame( $client, $clientFactory->getClient() );
28+
29+
$config = $client->getConfig();
30+
$this->assertEquals( $config['headers']['User-Agent'], 'Addwiki - mediawiki-api-base' );
31+
32+
$this->assertFalse( empty( $config['cookies'] ) );
33+
}
34+
35+
public function testUserAgent() {
36+
$clientFactory = new ClientFactory( array( 'user-agent' => 'Foobar' ) );
37+
38+
$client = $clientFactory->getClient();
39+
40+
$this->assertNull( $client->getConfig('user-agent') );
41+
42+
$config = $client->getConfig();
43+
$this->assertEquals( $config['headers']['User-Agent'], 'Foobar' );
44+
}
45+
46+
public function testHeaders() {
47+
$clientFactory = new ClientFactory( array(
48+
'headers' => array(
49+
'User-Agent' => 'Foobar',
50+
'X-Foo' => 'Bar',
51+
)
52+
) );
53+
54+
$client = $clientFactory->getClient();
55+
56+
$headers = $client->getConfig('headers');
57+
$this->assertCount( 2, $headers );
58+
$this->assertEquals( $headers['User-Agent'], 'Foobar' );
59+
$this->assertEquals( $headers['X-Foo'], 'Bar' );
60+
}
61+
62+
public function testHandler() {
63+
$handler = HandlerStack::create();
64+
65+
$clientFactory = new ClientFactory( array( 'handler' => $handler ) );
66+
67+
$client = $clientFactory->getClient();
68+
69+
$this->assertSame( $handler, $client->getConfig('handler') );
70+
}
71+
72+
public function testMiddleware() {
73+
$invoked = false;
74+
$middleware = function() use (&$invoked) {
75+
return function() use (&$invoked) {
76+
$invoked = true;
77+
};
78+
};
79+
80+
$clientFactory = new ClientFactory( array( 'middleware' => array( $middleware ) ) );
81+
82+
$client = $clientFactory->getClient();
83+
84+
$this->assertNull( $client->getConfig('middleware') );
85+
86+
$request = $this->getMockBuilder( 'Psr\Http\Message\RequestInterface' )->getMock();
87+
88+
$handler = $client->getConfig( 'handler' );
89+
$handler->remove( 'http_errors' );
90+
$handler->remove( 'allow_redirects' );
91+
$handler->remove( 'cookies' );
92+
$handler->remove( 'prepare_body' );
93+
$handler($request, array());
94+
95+
$this->assertTrue( $invoked );
96+
}
97+
}

0 commit comments

Comments
 (0)