diff --git a/src/WireMock/Client/Authentication/Authenticator.php b/src/WireMock/Client/Authentication/Authenticator.php new file mode 100644 index 0000000..ece2a4f --- /dev/null +++ b/src/WireMock/Client/Authentication/Authenticator.php @@ -0,0 +1,18 @@ + $headers + * @return list + */ + public function modifyHeaders(array $headers): array; + + /** + * @param non-empty-string $url + * @return non-empty-string + */ + public function modifyUrl(string $url): string; +} \ No newline at end of file diff --git a/src/WireMock/Client/Authentication/NullAuthenticator.php b/src/WireMock/Client/Authentication/NullAuthenticator.php new file mode 100644 index 0000000..d0390c4 --- /dev/null +++ b/src/WireMock/Client/Authentication/NullAuthenticator.php @@ -0,0 +1,16 @@ +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; + } +} \ No newline at end of file diff --git a/src/WireMock/Client/Curl.php b/src/WireMock/Client/Curl.php index 8969b17..413c0a1 100644 --- a/src/WireMock/Client/Curl.php +++ b/src/WireMock/Client/Curl.php @@ -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 @@ -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); @@ -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); diff --git a/src/WireMock/Client/HttpWait.php b/src/WireMock/Client/HttpWait.php index 761b3d7..6f313f3 100644 --- a/src/WireMock/Client/HttpWait.php +++ b/src/WireMock/Client/HttpWait.php @@ -4,6 +4,16 @@ 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(); @@ -11,26 +21,18 @@ public function waitForServerToGive200($url, $timeoutSecs = 10, $debug = 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) { diff --git a/src/WireMock/Client/WireMock.php b/src/WireMock/Client/WireMock.php index da28a7d..3ef7705 100644 --- a/src/WireMock/Client/WireMock.php +++ b/src/WireMock/Client/WireMock.php @@ -23,6 +23,8 @@ class WireMock private $hostname; /** @var int */ private $port; + /** @var string */ + private $scheme; /** @var HttpWait */ private $httpWait; /** @var Curl */ @@ -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) @@ -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); } /** diff --git a/test/WireMock/Integration/CloudIntegrationTest.php b/test/WireMock/Integration/CloudIntegrationTest.php new file mode 100644 index 0000000..96746c9 --- /dev/null +++ b/test/WireMock/Integration/CloudIntegrationTest.php @@ -0,0 +1,27 @@ +isAlive()); + } +}