Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/WireMock/Client/Authentication/Authenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace WireMock\Client\Authentication;

interface Authenticator
{
/**
* @param list<non-empty-string> $headers
* @return list<non-empty-string>
*/
public function modifyHeaders(array $headers): array;

/**
* @param non-empty-string $url
* @return non-empty-string
*/
public function modifyUrl(string $url): string;
}
16 changes: 16 additions & 0 deletions src/WireMock/Client/Authentication/NullAuthenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace WireMock\Client\Authentication;

final class NullAuthenticator implements Authenticator
{
public function modifyHeaders(array $headers): array
{
return $headers;
}

public function modifyUrl(string $url): string
{
return $url;
}
}
29 changes: 29 additions & 0 deletions src/WireMock/Client/Authentication/TokenAuthenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace WireMock\Client\Authentication;

final class TokenAuthenticator implements Authenticator
{
/**
* @var non-empty-string
*/
private $token;

/**
* @param non-empty-string $token
*/
public function __construct(string $token)
{
$this->token = $token;
}

public function modifyHeaders(array $headers): array
{
return array_merge($headers, [sprintf('Authorization: Token %s', $this->token)]);
}

public function modifyUrl(string $url): string
{
return $url;
}
}
23 changes: 19 additions & 4 deletions src/WireMock/Client/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@

namespace WireMock\Client;

use WireMock\Client\Authentication\Authenticator;
use WireMock\Client\Authentication\NullAuthenticator;

class Curl
{
/**
* @var Authenticator
*/
private $authenticator;

public function __construct(Authenticator $authenticator = null)
{
$this->authenticator = $authenticator ?? new NullAuthenticator();
}

/**
* @param string $url
* @return string The response body
Expand Down Expand Up @@ -51,7 +64,7 @@ public function delete(string $url): string
*/
private function makeCurlRequest(string $method, string $url, ?string $json = null)
{
$ch = curl_init($url);
$ch = curl_init($this->authenticator->modifyUrl($url));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if ($json !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
Expand All @@ -60,10 +73,12 @@ private function makeCurlRequest(string $method, string $url, ?string $json = nu
$contentLength = 0;
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(

$defaultHeaders = [
'Content-Type: application/json',
"Content-Length: $contentLength",
));
sprintf('Content-Length: %s', $contentLength),
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->authenticator->modifyHeaders($defaultHeaders));

$result = curl_exec($ch);

Expand Down
34 changes: 18 additions & 16 deletions src/WireMock/Client/HttpWait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,35 @@

class HttpWait
{
/**
* @var Curl
*/
private $curl;

public function __construct(?Curl $curl = null)
{
$this->curl = $curl ?? new Curl();
}

public function waitForServerToGive200($url, $timeoutSecs = 10, $debug = true)
{
$debugTrace = array();
$startTime = microtime(true);
$serverStarted = false;
while (microtime(true) - $startTime < $timeoutSecs) {
try {
$headers = @get_headers($url, 1);
} catch (\Exception $e) {
$debugTrace[] = "$url not yet up. Error getting headers: " . $e->getMessage();
continue;
}

if (isset($headers) && isset($headers[0]) && strpos($headers[0], '200 OK') !== false) {
$this->curl->get($url);
$serverStarted = true;
break;
} else {
if (!isset($headers)) {
$debugTrace[] = "$url not yet up. \$headers not set";
} else if (!isset($headers[0])) {
$debugTrace[] = "$url not yet up. \$headers[0] not set";
} else {
$debugTrace[] = "$url not yet up. \$headers[0] was {$headers[0]}";
}
} catch (\Exception $e) {
$debugTrace[] = "$url not yet up. " . $e->getMessage();

usleep(100000);

continue;
}
usleep(100000);
}

if (!$serverStarted) {
$time = microtime(true) - $startTime;
if ($debug) {
Expand Down
14 changes: 9 additions & 5 deletions src/WireMock/Client/WireMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class WireMock
private $hostname;
/** @var int */
private $port;
/** @var string */
private $scheme;
/** @var HttpWait */
private $httpWait;
/** @var Curl */
Expand All @@ -44,13 +46,15 @@ public function __construct(
Curl $curl,
Serializer $serializer,
$hostname = 'localhost',
$port = 8080
$port = 8080,
$scheme = 'http'
) {
$this->hostname = $hostname;
$this->port = $port;
$this->httpWait = $httpWait;
$this->curl = $curl;
$this->serializer = $serializer;
$this->hostname = $hostname;
$this->port = $port;
$this->scheme = $scheme;
}

public function isAlive($timeoutSecs = 10, $debug = true)
Expand Down Expand Up @@ -505,9 +509,9 @@ private function doDelete(string $path, ?string $resultType = null)
}
}

private function _makeUrl($path)
private function _makeUrl($path): string
{
return "http://$this->hostname:$this->port/$path";
return sprintf('%s://%s:%d/%s', $this->scheme, $this->hostname, $this->port, $path);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions test/WireMock/Integration/CloudIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace WireMock\Integration;

use PHPUnit\Framework\TestCase;
use WireMock\Client\Authentication\TokenAuthenticator;
use WireMock\Client\Curl;
use WireMock\Client\HttpWait;
use WireMock\Client\WireMock;
use WireMock\Serde\SerializerFactory;

class CloudIntegrationTest extends TestCase
{
public function testConnectToCloudHost(): void
{
$wiremockCloudHost = getenv('WIREMOCK_CLOUD_HOST');
$wiremockCloudToken = getenv('WIREMOCK_CLOUD_TOKEN');

if ($wiremockCloudToken === false || $wiremockCloudHost === false) {
self::markTestSkipped('Env variables WIREMOCK_CLOUD_HOST or WIREMOCK_CLOUD_TOKEN not set');
}

$curl = new Curl(new TokenAuthenticator($wiremockCloudToken));
$client = new WireMock(new HttpWait($curl), $curl, SerializerFactory::default(), $wiremockCloudHost, 443, 'https');

self::assertTrue($client->isAlive());
}
}