]> BookStack Code Mirror - bookstack/blobdiff - app/Http/HttpRequestService.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Http / HttpRequestService.php
index 8318474aa9cc97b03698f716b7e9684c4f657fc5..f59c298a62b8e2596f422c0a0d86766115bec8ac 100644 (file)
@@ -7,6 +7,7 @@ use GuzzleHttp\Handler\MockHandler;
 use GuzzleHttp\HandlerStack;
 use GuzzleHttp\Middleware;
 use GuzzleHttp\Psr7\Request as GuzzleRequest;
+use GuzzleHttp\Psr7\Response;
 use Psr\Http\Client\ClientInterface;
 
 class HttpRequestService
@@ -16,7 +17,7 @@ class HttpRequestService
     /**
      * Build a new http client for sending requests on.
      */
-    public function buildClient(int $timeout, array $options): ClientInterface
+    public function buildClient(int $timeout, array $options = []): ClientInterface
     {
         $defaultOptions = [
             'timeout' => $timeout,
@@ -40,8 +41,16 @@ class HttpRequestService
      * Returns history which can then be queried.
      * @link https://docs.guzzlephp.org/en/stable/testing.html#history-middleware
      */
-    public function mockClient(array $responses = []): HttpClientHistory
+    public function mockClient(array $responses = [], bool $pad = true): HttpClientHistory
     {
+        // By default, we pad out the responses with 10 successful values so that requests will be
+        // properly recorded for inspection. Otherwise, we can't later check if we're received
+        // too many requests.
+        if ($pad) {
+            $response = new Response(200, [], 'success');
+            $responses = array_merge($responses, array_fill(0, 10, $response));
+        }
+
         $container = [];
         $history = Middleware::history($container);
         $mock = new MockHandler($responses);