Skip to content

Commit

Permalink
Added service class
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- committed Sep 13, 2014
1 parent c3dcdf7 commit 5047a8b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/Nekland/YoutubeApi/Http/Auth/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function auth(RequestEvent $requestEvent)
if (!$request->hasHeader('Authorization')) {
$this->client = $requestEvent->getClient();
$token = $this->getToken();
$request->setHeader('Authorization', 'Bearer '.$token);
$request->setHeader('Authorization', 'Bearer ' . $token);
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/Nekland/YoutubeApi/Http/Auth/PublicApiAccessSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function it_should_add_a_key_to_the_url_of_the_request(RequestEvent $requ
$requestEvent->getRequest()->willReturn($request);

$request->getPath()->willReturn('http://foo.bar/api');
$request->setPath('http://foo.bar/api?key=my_custom_api_key')->shouldBeCalled();
$request->setParameter('key','my_custom_api_key')->shouldBeCalled();

$this->setOptions(['key' => 'my_custom_api_key']);
$this->auth($requestEvent);
Expand Down
44 changes: 44 additions & 0 deletions spec/Nekland/YoutubeApi/Http/Auth/ServiceSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace spec\Nekland\YoutubeApi\Http\Auth;

use Nekland\BaseApi\Http\AbstractHttpClient;
use Nekland\BaseApi\Http\Event\RequestEvent;
use Nekland\BaseApi\Http\Request;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ServiceSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType('Nekland\YoutubeApi\Http\Auth\Service');
$this->shouldHaveType('Nekland\BaseApi\Http\Auth\AuthStrategyInterface');
}

public function it_should_do_nothing_if_header_already_defined(RequestEvent $requestEvent, Request $request)
{
$requestEvent->getRequest()->willReturn($request);
$request->hasHeader('Authorization')->willReturn(true);
$requestEvent->getClient()->shouldNotBeCalled();

$this->auth($requestEvent);
}

public function it_should_send_a_request_and_add_a_header(RequestEvent $requestEvent, Request $request, AbstractHttpClient $client)
{
$requestEvent->getRequest()->willReturn($request);
$requestEvent->getClient()->willReturn($client);

$request->hasHeader(Argument::any())->willReturn(false);
$request->setHeader('Authorization', 'Bearer foobar')->shouldBeCalled();
$client->send(Argument::type('Nekland\BaseApi\Http\Request'), Argument::any())->willReturn('{"access_token": "foobar"}');

// Notice: this certificate is a fake one
$this->setOptions([
'cert_file' => __DIR__ . '/../../../../fixtures/cert.p12',
'email' => 'myemail_for_google_service@googleapi_etc.com'
]);
$this->auth($requestEvent);
}
}
File renamed without changes.

0 comments on commit 5047a8b

Please sign in to comment.