Skip to content
Merged
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
43 changes: 43 additions & 0 deletions src/DrevOps/BehatPhpServer/ApiServerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,49 @@ public function resetApi(): void {
$this->debug('API server responses and requests have been reset.');
}

/**
* Clear expected responses queue in the API server.
*
* @Given (the )API has no responses
*
* @code
* Given the API has no responses
* @endcode
*
* @see https://github.com/drevops/behat-phpserver/issues/33
*/
public function apiHasNoResponses(): void {
$response = $this->client->request('DELETE', '/admin/responses');

if ($response->getStatusCode() !== 200) {
throw new \RuntimeException('Failed to delete the API responses.');
}

$this->debug('API server responses have been cleared.');
}

/**
* Fetch debug information about API requests made.
*
* @When I debug API requests
*
* @code
* When I debug API requests
* @endcode
*/
public function debugApiRequests(): void {
$response = $this->client->request('GET', '/admin/requests');

if ($response->getStatusCode() !== 200) {
throw new \RuntimeException('Failed to fetch the API requests.');
}

$body = (string) $response->getBody();

$message = json_encode(json_decode($body, TRUE), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
fwrite(STDOUT, "\nAPI Requests Debug Info:\n" . $message . "\n");
}
Comment on lines +157 to +168
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Handle invalid JSON gracefully in debug output.

If the response body is not valid JSON, json_decode($body, TRUE) returns null, and json_encode(null, ...) will output the string "null", which could be confusing during debugging.

Proposed fix
   public function debugApiRequests(): void {
     $response = $this->client->request('GET', '/admin/requests');

     if ($response->getStatusCode() !== 200) {
       throw new \RuntimeException('Failed to fetch the API requests.');
     }

     $body = (string) $response->getBody();

-    $message = json_encode(json_decode($body, TRUE), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
+    $decoded = json_decode($body, TRUE);
+    $message = $decoded !== null
+      ? json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
+      : $body;
     fwrite(STDOUT, "\nAPI Requests Debug Info:\n" . $message . "\n");
   }
🧰 Tools
🪛 PHPMD (2.15.0)

161-161: Missing class import via use statement (line '161', column '17'). (undefined)

(MissingImport)

🤖 Prompt for AI Agents
In `@src/DrevOps/BehatPhpServer/ApiServerContext.php` around lines 157 - 168, The
debugApiRequests method currently assumes the response body is valid JSON;
change debugApiRequests to validate the json_decode result (call
json_decode($body, TRUE) into a variable, then check json_last_error() or that
the result is not null) and when decoding fails write a clear message and the
raw response body to STDOUT instead of encoding null—update references to
$response->getBody(), the json_decode call, and the $message construction so
invalid JSON is reported gracefully while valid JSON continues to be
pretty-printed.


/**
* Put expected response data to the API server.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/behat/features/apiserver.feature
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,37 @@ Feature: API Server
Then the response status code should be 200
And the response should contain "sample text file in the secondary fixtures directory"
And the response header "Content-Type" should contain "text/plain"

#
# Admin operations
#
Scenario: Clear only responses queue without affecting received requests count
Given API server is running
And API server is reset
And the API will respond with JSON:
"""
{"test": "response1"}
"""
And the API will respond with JSON:
"""
{"test": "response2"}
"""
# Make a request to increment received requests count
When I send a GET request to "/someurl" in the API server
Then the API server should have 1 received request
And the API server should have 1 queued response
# Clear only responses - requests count should remain unchanged
Given the API has no responses
Then the API server should have 1 received request
And the API server should have 0 queued responses

Scenario: Debug API requests shows request information
Given API server is running
And API server is reset
And the API will respond with JSON:
"""
{"status": "ok"}
"""
When I send a GET request to "/test/endpoint" in the API server
And I debug API requests
Then the API server should have 1 received request
Loading