generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
767e95c
commit 18c9b02
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
use CleaniqueCoders\KongAdminApi\Configuration; | ||
use CleaniqueCoders\KongAdminApi\Connector; | ||
use Saloon\Enums\Method; | ||
use Saloon\Http\Faking\MockClient; | ||
use Saloon\Http\Faking\MockResponse; | ||
use Saloon\Http\Request; | ||
|
||
// Custom test request class | ||
class TestRequest extends Request | ||
{ | ||
protected Method $method = Method::GET; | ||
|
||
public function resolveEndpoint(): string | ||
{ | ||
return 'resource'; | ||
} | ||
} | ||
|
||
it('initializes the connector with the provided configuration', function () { | ||
$configuration = new Configuration( | ||
base: 'https://kong-admin.com', | ||
uri: 'api', | ||
apiKey: 'test-api-key', | ||
keyName: 'apikey' | ||
); | ||
|
||
$connector = new Connector($configuration); | ||
|
||
expect($connector)->toBeInstanceOf(Connector::class); | ||
}); | ||
|
||
it('sends a request with default headers and authentication', function () { | ||
// Set up a mock client with a mock response | ||
$mockClient = new MockClient([ | ||
MockResponse::make(['status' => 'success'], 200, [ | ||
'Content-Type' => 'application/json', | ||
'Accept' => 'application/json', | ||
'apikey' => 'test-api-key', | ||
]), | ||
]); | ||
|
||
// Configuration with specific headers | ||
$configuration = new Configuration( | ||
base: 'https://kong-admin.com', | ||
uri: 'api', | ||
apiKey: 'test-api-key', | ||
keyName: 'apikey', | ||
headers: [ | ||
'Content-Type' => 'application/json', | ||
'Accept' => 'application/json', | ||
] | ||
); | ||
|
||
// Initialize Connector with MockClient | ||
$connector = new Connector($configuration); | ||
$connector->withMockClient($mockClient); | ||
|
||
// Create and send the test request using Connector | ||
$request = new TestRequest; | ||
$response = $connector->send($request); | ||
|
||
expect($response->headers()->get('Content-Type'))->toBe('application/json') | ||
->and($response->headers()->get('Accept'))->toBe('application/json') | ||
->and($response->headers()->get('apikey'))->toBe('test-api-key') | ||
->and($response->status())->toBe(200) | ||
->and($response->json())->toBe(['status' => 'success']); | ||
}); |