Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Sep 16, 2024
1 parent 2339f15 commit a00e1e8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
26 changes: 25 additions & 1 deletion src/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use Closure;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use InvalidArgumentException;

final class AccessToken
Expand Down Expand Up @@ -63,7 +64,7 @@ public function getHttpClient(PendingRequest $httpClient): PendingRequest
self::TOKEN_TYPE_BEARER => $httpClient->withToken($this->accessToken),
self::TOKEN_TYPE_QUERY => $httpClient->withQueryParameters([$this->tokenName => $this->accessToken]),
self::TOKEN_TYPE_CUSTOM => $this->resolveCustomAuth($httpClient),
default => throw new InvalidArgumentException('Invalid auth type')
default => throw new InvalidArgumentException('Invalid auth type')
};
}

Expand All @@ -75,4 +76,27 @@ protected function resolveCustomAuth(PendingRequest $httpClient): PendingRequest

return ($this->customCallback)($httpClient);
}

public static function parseQueryTokenFromResponse(Response $response, string $queryKey = 'token'): ?string
{
$uri = $response->effectiveUri();

if (! $uri) {
return null;
}

return self::parseTokenFromQueryString($response->effectiveUri()?->getQuery(), $queryKey);
}

public static function parseQueryTokenFromUrl(string $url, string $queryKey = 'token'): string
{
return self::parseTokenFromQueryString(parse_url($url, PHP_URL_QUERY), $queryKey);
}

public static function parseTokenFromQueryString(string $queryString, string $queryKey = 'token'): string
{
parse_str($queryString, $output);

return $output[$queryKey];
}
}
10 changes: 3 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Pelmered\LaravelHttpOAuthHelper\AccessToken;
use Pelmered\LaravelHttpOAuthHelper\LaravelHttpOAuthHelperServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
Expand All @@ -17,7 +18,7 @@ protected function setUp(): void
Http::preventStrayRequests();

Http::fake(
static function (Request $request) {
function (Request $request) {
if ($request->url() === 'https://example.com/oauth/token') {
if ($request->token = 'my_refresh_token') {
return Http::response([
Expand Down Expand Up @@ -49,10 +50,7 @@ static function (Request $request) {
}

if (Str::of($request->url())->startsWith('https://example.com/api?token=')) {

$url = parse_url($request->url(), PHP_URL_QUERY);
parse_str($url, $output);
$token = $output['token'];
$token = AccessToken::parseQueryTokenFromUrl($request->url());

return Http::response([
'data' => 'some data with query string token',
Expand All @@ -63,8 +61,6 @@ static function (Request $request) {
return Http::response([], 200);
}
);

//Cache::spy();
}

protected function defineEnvironment($app)
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ArchTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

arch()->preset()->laravel();
arch()->preset()->security();
arch()->preset()->security()->ignoring('parse_str');;

arch()->expect('dd')->not->toBeUsed();
44 changes: 43 additions & 1 deletion tests/Unit/RefreshTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\Client\Factory;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Request;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Pelmered\LaravelHttpOAuthHelper\AccessToken;
Expand Down Expand Up @@ -262,7 +263,7 @@
->and($accessToken->getCustomCallback())->toBeNull();
});

test('token type custom', function () {
test('custom token type', function () {

$accessToken = app(RefreshToken::class)(
'https://example.com/oauth/token',
Expand All @@ -285,4 +286,45 @@
expect($options['headers']['MyCustomAuthHeader'])->toBe('my_custom_token');
});

test('custom auth token type', function () {

app(RefreshToken::class)(
'https://example.com/oauth/token',
new Credentials(function (PendingRequest $httpClient) {
return $httpClient->withHeader('MyCustomAuthHeader', 'my_custom_token');
}),
new Options(
scopes: ['scope1', 'scope2'],
),
);
Http::assertSent(static function (Request $request) {
return $request->hasHeader('MyCustomAuthHeader', 'my_custom_token')
&& $request->url() === 'https://example.com/oauth/token';
});
});
test('auth type query', function () {

app(RefreshToken::class)(
'https://example.com/oauth/token',
new Credentials('my_query_token'),
new Options(
scopes: ['scope1', 'scope2'],
authType: Credentials::AUTH_TYPE_QUERY,
tokenName: 'custom_token_name',
accessToken: function (Response $response) {
return AccessToken::parseQueryTokenFromResponse($response, 'custom_token_name');
}
),
);
Http::assertSent(function (Request $request) {

$token = AccessToken::parseQueryTokenFromUrl($request->url(), 'custom_token_name');

expect($token)->toBe('my_query_token');


return $request->url() === 'https://example.com/oauth/token?custom_token_name=my_query_token';
});
});

})->done(assignee: 'pelmered');

0 comments on commit a00e1e8

Please sign in to comment.