Skip to content

Commit 5a2c0e4

Browse files
authored
Merge pull request #108 from LibreSign/feat/raw-json-http-body-support
feat: support raw JSON bodies in send step
2 parents 7876b05 + e448f9e commit 5a2c0e4

4 files changed

Lines changed: 120 additions & 38 deletions

File tree

features/bootstrap/FeatureContext.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

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

2423
/**
2524
* @inheritDoc
25+
* @psalm-suppress MissingOverrideAttribute
2626
*/
27-
#[\Override]
2827
public function setCurrentUser(string $user): void {
2928
parent::setCurrentUser($user);
3029
Assert::assertEquals($this->currentUser, $user);
3130
}
3231

3332
/**
3433
* @inheritDoc
34+
* @psalm-suppress MissingOverrideAttribute
3535
*/
36-
#[\Override]
3736
public function assureUserExists(string $user): void {
3837
parent::assureUserExists($user);
3938
$lastRequest = $this->getLastREquest();
@@ -58,16 +57,17 @@ private function getLastRequest(): RequestInfo {
5857
* When whe run the test suit of this repository at GitHub Actions, is
5958
* necessary to consider that we haven't Nextcloud installed and mock
6059
* the real path of files.
60+
* @psalm-suppress MissingOverrideAttribute
6161
*/
62-
#[\Override]
6362
public static function findParentDirContainingFile(string $filename): string {
6463
return __DIR__;
6564
}
6665

6766
/**
6867
* @inheritDoc
68+
* @param TableNode|PyStringNode|array|null $body
69+
* @psalm-suppress MissingOverrideAttribute
6970
*/
70-
#[\Override]
7171
public function sendRequest(string $verb, string $url, $body = null, array $headers = [], array $options = []): void {
7272
parent::sendRequest($verb, $url, $body, $headers, $options);
7373
$lastRequest = $this->getLastRequest();
@@ -96,6 +96,15 @@ public function sendRequest(string $verb, string $url, $body = null, array $head
9696
if (array_key_exists('json', $this->requestOptions)) {
9797
Assert::assertEquals($this->requestOptions['json'], $parsedInput);
9898
}
99+
100+
// Raw body payload
101+
if (array_key_exists('body', $this->requestOptions)) {
102+
Assert::assertEquals($this->requestOptions['body'], $lastRequest->getInput());
103+
Assert::assertStringContainsString(
104+
'application/json',
105+
(string)($lastRequest->getHeaders()['Content-Type'] ?? $lastRequest->getHeaders()['CONTENT_TYPE'] ?? '')
106+
);
107+
}
99108
}
100109

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

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

133144
/**
134145
* @inheritDoc
146+
* @psalm-suppress MissingOverrideAttribute
135147
*/
136-
#[\Override]
137148
public function theResponseShouldBeAJsonArrayWithTheFollowingMandatoryValues(TableNode $table): void {
138149
$lastRequest = $this->getLastRequest();
139150
$parsedInput = $this->getParsedInputFromRequest($lastRequest);

features/test.feature

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ Feature: Test this extension
4242
| key | value |
4343
| status | true |
4444

45+
Scenario: Test POST with raw json body via PyStringNode
46+
When set the response to:
47+
"""
48+
{
49+
"status": {
50+
"nested": true
51+
}
52+
}
53+
"""
54+
And sending "POST" to "/"
55+
"""
56+
{
57+
"status": {
58+
"nested": true
59+
},
60+
"events": [
61+
"created",
62+
"updated"
63+
]
64+
}
65+
"""
66+
Then the response should be a JSON array with the following mandatory values
67+
| key | value |
68+
| (jq).status.nested | true |
69+
4570
Scenario: Test response of POST is json
4671
When set the response to:
4772
"""

psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0"?>
22
<psalm
33
errorLevel="2"
4+
ensureOverrideAttribute="false"
45
findUnusedBaselineEntry="true"
56
findUnusedCode="false"
67
resolveFromConfigFile="true"

0 commit comments

Comments
 (0)