diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 74fa641..3b34859 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -139,6 +139,14 @@ public function setTheResponseTo(PyStringNode $response): void { )); } + #[Given('set the response with :header header :value to:')] + public function setTheResponseWithHeaderTo(string $header, string $value, PyStringNode $response): void { + $this->mockServer->setDefaultResponse(new MockWebServerResponse( + (string) $response, + [$header => $value] + )); + } + /** * @inheritDoc */ diff --git a/features/test.feature b/features/test.feature index 8c1d86f..ea21f48 100644 --- a/features/test.feature +++ b/features/test.feature @@ -315,3 +315,27 @@ Feature: Test this extension Scenario: Wait for seconds When wait for 1 seconds When past 1 second since wait step + + Scenario: Test response header assertion matches expected value + When set the response with "Content-Type" header "application/json" to: + """ + {"ok":true} + """ + And sending "POST" to "/" + Then the response header "Content-Type" should contain "application/json" + + Scenario: Test response body is not empty + When set the response to: + """ + hello + """ + And sending "POST" to "/" + Then the response body should not be empty + + Scenario: Test response body matches a regular expression + When set the response to: + """ + %PDF-1.4 binary content here + """ + And sending "POST" to "/" + Then the response body should match the regular expression "^%PDF" diff --git a/src/NextcloudApiContext.php b/src/NextcloudApiContext.php index 5fb310a..2b0cddb 100644 --- a/src/NextcloudApiContext.php +++ b/src/NextcloudApiContext.php @@ -359,6 +359,25 @@ public function theResponseShouldHaveStatusCode(string $code): void { Assert::assertEquals($code, $currentCode, $this->response->getBody()->getContents()); } + #[Given('the response header :header should contain :value')] + public function theResponseHeaderShouldContain(string $header, string $value): void { + $actual = strtolower($this->response->getHeaderLine($header)); + Assert::assertStringContainsString(strtolower($value), $actual, sprintf('Response header "%s" does not contain "%s"', $header, $value)); + } + + #[Given('the response body should not be empty')] + public function theResponseBodyShouldNotBeEmpty(): void { + $this->response->getBody()->rewind(); + Assert::assertNotSame('', $this->response->getBody()->getContents(), 'Response body is empty'); + } + + #[Given('the response body should match the regular expression :pattern')] + public function theResponseBodyShouldMatchTheRegularExpression(string $pattern): void { + $this->response->getBody()->rewind(); + $content = $this->response->getBody()->getContents(); + Assert::assertMatchesRegularExpression('#' . $pattern . '#', $content, sprintf('Response body does not match pattern "%s"', $pattern)); + } + /** * @throws \InvalidArgumentException */