Skip to content

Commit

Permalink
Added Connector Test
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrulhazim committed Nov 5, 2024
1 parent 767e95c commit 18c9b02
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tests/ConnectorTest.php
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']);
});

0 comments on commit 18c9b02

Please sign in to comment.