Skip to content

Commit 617ffaa

Browse files
authored
Merge pull request #111 from LibreSign/feat/response-header-body-assertions
feat: add response header and body assertion steps
2 parents 6dc5a65 + 4e0dc02 commit 617ffaa

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

features/bootstrap/FeatureContext.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ public function setTheResponseTo(PyStringNode $response): void {
139139
));
140140
}
141141

142+
#[Given('set the response with :header header :value to:')]
143+
public function setTheResponseWithHeaderTo(string $header, string $value, PyStringNode $response): void {
144+
$this->mockServer->setDefaultResponse(new MockWebServerResponse(
145+
(string) $response,
146+
[$header => $value]
147+
));
148+
}
149+
142150
/**
143151
* @inheritDoc
144152
*/

features/test.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,27 @@ Feature: Test this extension
315315
Scenario: Wait for seconds
316316
When wait for 1 seconds
317317
When past 1 second since wait step
318+
319+
Scenario: Test response header assertion matches expected value
320+
When set the response with "Content-Type" header "application/json" to:
321+
"""
322+
{"ok":true}
323+
"""
324+
And sending "POST" to "/"
325+
Then the response header "Content-Type" should contain "application/json"
326+
327+
Scenario: Test response body is not empty
328+
When set the response to:
329+
"""
330+
hello
331+
"""
332+
And sending "POST" to "/"
333+
Then the response body should not be empty
334+
335+
Scenario: Test response body matches a regular expression
336+
When set the response to:
337+
"""
338+
%PDF-1.4 binary content here
339+
"""
340+
And sending "POST" to "/"
341+
Then the response body should match the regular expression "^%PDF"

src/NextcloudApiContext.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,25 @@ public function theResponseShouldHaveStatusCode(string $code): void {
359359
Assert::assertEquals($code, $currentCode, $this->response->getBody()->getContents());
360360
}
361361

362+
#[Given('the response header :header should contain :value')]
363+
public function theResponseHeaderShouldContain(string $header, string $value): void {
364+
$actual = strtolower($this->response->getHeaderLine($header));
365+
Assert::assertStringContainsString(strtolower($value), $actual, sprintf('Response header "%s" does not contain "%s"', $header, $value));
366+
}
367+
368+
#[Given('the response body should not be empty')]
369+
public function theResponseBodyShouldNotBeEmpty(): void {
370+
$this->response->getBody()->rewind();
371+
Assert::assertNotSame('', $this->response->getBody()->getContents(), 'Response body is empty');
372+
}
373+
374+
#[Given('the response body should match the regular expression :pattern')]
375+
public function theResponseBodyShouldMatchTheRegularExpression(string $pattern): void {
376+
$this->response->getBody()->rewind();
377+
$content = $this->response->getBody()->getContents();
378+
Assert::assertMatchesRegularExpression('#' . $pattern . '#', $content, sprintf('Response body does not match pattern "%s"', $pattern));
379+
}
380+
362381
/**
363382
* @throws \InvalidArgumentException
364383
*/

0 commit comments

Comments
 (0)