Permalink
Browse files

Merge pull request #93 from rybakit/auth_listener

Add BasicAuthListener
  • Loading branch information...
2 parents 2ba5da1 + 4cee0eb commit 988bf14201ffbed940f23b42f87302e6adec4c21 @kriswallsmith committed Aug 17, 2012
Showing with 47 additions and 0 deletions.
  1. +27 −0 lib/Buzz/Listener/BasicAuthListener.php
  2. +20 −0 test/Buzz/Test/Listener/BasicAuthListenerTest.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Buzz\Listener;
+
+use Buzz\Message\MessageInterface;
+use Buzz\Message\RequestInterface;
+
+class BasicAuthListener implements ListenerInterface
+{
+ private $username;
+ private $password;
+
+ public function __construct($username, $password)
+ {
+ $this->username = $username;
+ $this->password = $password;
+ }
+
+ public function preSend(RequestInterface $request)
+ {
+ $request->addHeader('Authorization: Basic '.base64_encode($this->username.':'.$this->password));
+ }
+
+ public function postSend(RequestInterface $request, MessageInterface $response)
+ {
+ }
+}
@@ -0,0 +1,20 @@
+<?php
+
+namespace Buzz\Test\Listener;
+
+use Buzz\Listener\BasicAuthListener;
+use Buzz\Message;
+
+class BasicAuthListenerTest extends \PHPUnit_Framework_TestCase
+{
+ public function testBasicAuthHeader()
+ {
+ $request = new Message\Request();
+ $this->assertEmpty($request->getHeader('Authorization'));
+
+ $listener = new BasicAuthListener('foo', 'bar');
+ $listener->preSend($request);
+
+ $this->assertEquals('Basic '.base64_encode('foo:bar'), $request->getHeader('Authorization'));
+ }
+}

0 comments on commit 988bf14

Please sign in to comment.