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
25 changes: 18 additions & 7 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Step\Given;
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\RequestInfo;
use donatj\MockWebServer\Response as MockWebServerResponse;
Expand All @@ -23,17 +22,17 @@ public function __construct(?array $parameters = []) {

/**
* @inheritDoc
* @psalm-suppress MissingOverrideAttribute
*/
#[\Override]
public function setCurrentUser(string $user): void {
parent::setCurrentUser($user);
Assert::assertEquals($this->currentUser, $user);
}

/**
* @inheritDoc
* @psalm-suppress MissingOverrideAttribute
*/
#[\Override]
public function assureUserExists(string $user): void {
parent::assureUserExists($user);
$lastRequest = $this->getLastREquest();
Expand All @@ -58,16 +57,17 @@ private function getLastRequest(): RequestInfo {
* When whe run the test suit of this repository at GitHub Actions, is
* necessary to consider that we haven't Nextcloud installed and mock
* the real path of files.
* @psalm-suppress MissingOverrideAttribute
*/
#[\Override]
public static function findParentDirContainingFile(string $filename): string {
return __DIR__;
}

/**
* @inheritDoc
* @param TableNode|PyStringNode|array|null $body
* @psalm-suppress MissingOverrideAttribute
*/
#[\Override]
public function sendRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
parent::sendRequest($verb, $url, $body, $headers, $options);
$lastRequest = $this->getLastRequest();
Expand Down Expand Up @@ -96,6 +96,15 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
if (array_key_exists('json', $this->requestOptions)) {
Assert::assertEquals($this->requestOptions['json'], $parsedInput);
}

// Raw body payload
if (array_key_exists('body', $this->requestOptions)) {
Assert::assertEquals($this->requestOptions['body'], $lastRequest->getInput());
Assert::assertStringContainsString(
'application/json',
(string)($lastRequest->getHeaders()['Content-Type'] ?? $lastRequest->getHeaders()['CONTENT_TYPE'] ?? '')
);
}
}

private function getParsedInputFromRequest(RequestInfo $requestInfo): array {
Expand All @@ -122,7 +131,9 @@ private function hasNestedPayload(array $payload): bool {
return false;
}

#[Given('set the response to:')]
/**
* @Given set the response to:
*/
public function setTheResponseTo(PyStringNode $response): void {
// Mock response to be equal to body of request
$this->mockServer->setDefaultResponse(new MockWebServerResponse(
Expand All @@ -132,8 +143,8 @@ public function setTheResponseTo(PyStringNode $response): void {

/**
* @inheritDoc
* @psalm-suppress MissingOverrideAttribute
*/
#[\Override]
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table): void {
$lastRequest = $this->getLastRequest();
$parsedInput = $this->getParsedInputFromRequest($lastRequest);
Expand Down
25 changes: 25 additions & 0 deletions features/test.feature
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ Feature: Test this extension
| key | value |
| status | true |

Scenario: Test POST with raw json body via PyStringNode
When set the response to:
"""
{
"status": {
"nested": true
}
}
"""
And sending "POST" to "/"
"""
{
"status": {
"nested": true
},
"events": [
"created",
"updated"
]
}
"""
Then the response should be a JSON array with the following mandatory values
| key | value |
| (jq).status.nested | true |

Scenario: Test response of POST is json
When set the response to:
"""
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
ensureOverrideAttribute="false"
findUnusedBaselineEntry="true"
findUnusedCode="false"
resolveFromConfigFile="true"
Expand Down
Loading
Loading