Skip to content

Commit 6323206

Browse files
committed
feat: fetch data from response using jq
Signed-off-by: Vitor Mattos <[email protected]>
1 parent a796a67 commit 6323206

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ The alias `price` could be used in a path or body of a request:
118118
| field | <data.0.foo> |
119119
```
120120

121+
#### Fetch field using jq
122+
123+
You can assign the return of a jq to your field using a jq like as the follow pattern:
124+
125+
```gherkin
126+
And fetch field "(foo)(jq).value" from prevous JSON response
127+
```
128+
129+
This will retrieve a specific value from json response and assign this to your desided field.
130+
121131
## Parse response using jq
122132

123133
You can use [jq](https://jqlang.github.io/jq/manual/) expression casting to check a value in a json response body of a request. To do this you will need to install the jq command.

features/test.feature

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ Feature: Test this extension
8686
| key | value |
8787
| data | [{"foo":"<FIELD_FOO>"}] |
8888

89+
Scenario: Test get field from json response using jq
90+
When set the response to:
91+
"""
92+
{
93+
"data": [
94+
{
95+
"foo":"bar"
96+
}
97+
]
98+
}
99+
"""
100+
And sending "POST" to "/"
101+
And fetch field "(FIELD_FOO)(jq).data[0].foo" from prevous JSON response
102+
# After fetch the field, you can use the value of field like this:
103+
And sending "POST" to "/?foo=<FIELD_FOO>"
104+
| field | <data.0.foo> |
105+
Then the response should be a JSON array with the following mandatory values
106+
| key | value |
107+
| data | [{"foo":"<FIELD_FOO>"}] |
108+
89109
Scenario: Test initial state with string
90110
When set the response to:
91111
"""

src/NextcloudApiContext.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,25 @@ private function validateAsJsonQuery(string $expected, string $actual): void {
340340
*/
341341
public function fetchFieldFromPreviousJsonResponse(string $path): void {
342342
$this->response->getBody()->seek(0);
343-
$responseArray = json_decode($this->response->getBody()->getContents(), true);
344-
if (preg_match('/(?<alias>\([^)]*\))(?<patch>.*)/', $path, $matches)) {
343+
$body = $this->response->getBody()->getContents();
344+
345+
// Is json query
346+
if (preg_match('/(?<alias>\([^)]*\))\(jq\)(?<path>.*)/', $path, $matches)) {
347+
$this->fields[$matches['alias']] = $this->testAndGetActualValue(
348+
['key' => '(jq)' . $matches['path']],
349+
$body
350+
);
351+
return;
352+
}
353+
354+
// Is array with alias
355+
if (preg_match('/(?<alias>\([^)]*\)){1,}(?<path>.*)/', $path, $matches)) {
345356
$alias = $matches['alias'];
346-
$path = $matches['patch'];
357+
$path = $matches['path'];
347358
}
348359
$keys = explode('.', $path);
349-
$value = $responseArray;
360+
$value = json_decode($body, true);
350361
foreach ($keys as $key) {
351-
$body = json_encode($responseArray);
352-
Assert::assertIsString($body);
353362
Assert::assertArrayHasKey($key, $value, 'Key [' . $key . '] of path [' . $path . '] not found at body: ' . $body);
354363
$value = $value[$key];
355364
}

0 commit comments

Comments
 (0)