Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Updating tests and READ me
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Clifford committed May 4, 2020
1 parent 2f7d70e commit 77a6204
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 48 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,26 @@ $purge = $fastly->send('POST', 'service/'. $fastly_service_id .'/purge_all');

```php

$response = $fastly->send_private_key($cert_d_1);
$private_keys = $fastly->private_keys;

$get_keys = $private_keys->get_private_keys();

$key = $private_keys->get_private_key($id);

$new_key = $private_keys->send_private_key($key, $name = '');

```

### Certificates

```php
$certificates = $fastly->certificates;

$response = $fastly->send_tls_certificate($cert_d_1);
$certificates->get_tls_certificates();
$certificates->get_tls_certificate("1JP0gerEJXIxImRnRLckug");
$certificates->send_tls_certificate($cert);
$certificates->update_tls_certificate($id, $certificate);
$certificates->delete_tls_certificate($id);

```

18 changes: 16 additions & 2 deletions src/Fastly/Adapter/FastlyRequestAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class FastlyRequestAdapter {

public $output = [];
public $error = [];
public $statusCode = [];

/**
* @param string $token
Expand Down Expand Up @@ -64,7 +65,15 @@ public function send($method, $uri, array $options = []) {
$pool = new Pool($this->client, $requests($uri), [
'concurrency' => 100,
'fulfilled' => function (ResponseInterface $response) {
$this->output[] = $this->getBody($response);
$body = $this->getBody($response);
$this->statusCode[] = $response->getStatusCode();

if ($body === '' || $body === null) {
$this->output[] = "No Content";
}
else {
$this->output[] = $body;
}
},
'rejected' => function (RequestException $e) {
$this->error[] = $e->getMessage();
Expand Down Expand Up @@ -92,7 +101,12 @@ public function getError() {
* @return string
*/
private function getBody(ResponseInterface $response) {
return (string)$response->getBody();
if ($response->getStatusCode() === "204") {
return "204 No Content";
}
else {
return (string)$response->getBody();
}
}

/**
Expand Down
7 changes: 2 additions & 5 deletions src/Fastly/Types/FastlyPrivateKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ public function __construct($token, $entrypoint) {
/**
* Post private key to Fastly.
*
* @param $uri
* @param $private_key
* @param string $name
*
* @return array|string
*/
public function send_private_key($uri, $private_key, $name = '') {
$endpoint = $this->build_endpoint($uri);
public function send_private_key($private_key, $name = '') {
$endpoint = $this->build_endpoint('tls/private_keys');

$options = [
"data" => [
Expand Down Expand Up @@ -83,6 +82,4 @@ public function get_private_keys() {
public function delete_private_key($id) {
return $this->send('DELETE', $this->build_endpoint('tls/private_keys/') . $id);
}


}
39 changes: 0 additions & 39 deletions tests/Fastly/FastlyTLSCertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,6 @@ protected function setUp(): void {
$this->signed_cert_out = $certout;
}

public function testGetTLSKeysResponse()
{
$response = $this->fastly->send('GET', 'tls/private_keys');

$this->assertArrayHasKey('data', $response);
}

public function testGetSpecificTLSKeyResponse()
{
$id = '2RZmS0uEBI4mnyXI0ztqx0';
$response = $this->fastly->send('GET', 'tls/private_keys/'.$id);

$this->assertArrayHasKey('data', $response);
}

public function testGetPrivateKeys()
{
$keys = $this->fastly->private_keys;
$response = $keys->get_private_keys();

$this->assertArrayHasKey('data', $response);
}

// public function testUploadPrivateKeys()
// {
// $response = $this->fastly->send_private_key('tls/private_keys', $this->private_key);
//
// $this->assertEquals('tls_private_key', $response['data']['type']);
// $this->assertArrayHasKey('id', $response['data']);
// $this->assertArrayHasKey('attributes', $response['data']);
// }

// public function testDeletePrivateKeys()
// {
// $id = '1LHnhX5YI6EYjtfwVFTvBT';
// $response = $this->fastly->send('DELETE', 'tls/private_keys/'.$id);
// }

public function testGetCertificates()
{
$certificates = $this->fastly->certificates;
Expand All @@ -103,7 +65,6 @@ public function testGetCertificates()
public function testGetCertificateByID()
{
$certificates = $this->fastly->certificates;

$get_certificate = $certificates->get_tls_certificate("1JP0gerEJXIxImRnRLckug");

$this->assertArrayHasKey('id', $get_certificate);
Expand Down
68 changes: 68 additions & 0 deletions tests/Fastly/FastlyTLSPrivateKeysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Fastly\Tests;

use Dotenv\Dotenv;
use Fastly\Fastly;

class FastlyTLSPrivateKeysTest extends \PHPUnit\Framework\TestCase {

private $fastly;
private $fastly_service_id;

protected function setUp(): void {
$dotenv = Dotenv::createImmutable('./');
$dotenv->load();

$fastly_api_token = getenv('FASTLY_API_KEY');
$this->fastly_service_id = getenv('FASTLY_SERVICE_ID');

$this->fastly = new Fastly($fastly_api_token, $this->fastly_service_id);

// Generate a new private (and public) key pair
$privkey = openssl_pkey_new(array(
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
));

openssl_pkey_export($privkey, $pkeyout);
$this->private_key = $pkeyout;
}

public function testGetTLSPrivateKeys()
{
$keys = $this->fastly->private_keys;
$response = $keys->get_private_keys();

$this->assertArrayHasKey('data', $response);
}

public function testGetSpecificTLSPrivateKey()
{
$id = '2RZmS0uEBI4mnyXI0ztqx0';
$keys = $this->fastly->private_keys;

$get_key = $keys->get_private_key($id);
$this->assertArrayHasKey('data', $get_key);
}

// public function testUploadPrivateKeys()
// {
// $keys = $this->fastly->private_keys;
// $response = $keys->send_private_key($this->private_key, $name = '');
//
// $this->assertEquals('tls_private_key', $response['data']['type']);
// $this->assertArrayHasKey('id', $response['data']);
// $this->assertArrayHasKey('attributes', $response['data']);
// }

// public function testDeletePrivateKeys()
// {
// $id = "6bWQlIGscXMA86GChdi7q9";
// $keys = $this->fastly->private_keys;
// $response = $keys->delete_private_key($id);
//
// $this->assertStringContainsString('No Content', $response[0]);
// }

}

0 comments on commit 77a6204

Please sign in to comment.