Skip to content

Commit 622be19

Browse files
authored
Merge pull request #93 from LibreSign/feat/fetch-data-from-response-using-jq
feat: fetch data from response using jq
2 parents d781cd3 + d45bf8f commit 622be19

3 files changed

Lines changed: 51 additions & 12 deletions

File tree

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Given /^set the display name of user "([^"]*)" to "([^"]*)"$/
5656
Given /^set the email of user "([^"]*)" to "([^"]*)"$/
5757
Given sending :verb to ocs :url
5858
Given the response should have a status code :code
59-
Given fetch field :path from prevous JSON response
59+
Given fetch field :path from previous JSON response
6060
Given the response should contain the initial state :name with the following values:
6161
Given the response should contain the initial state :name json that match with:
6262
Given the following :appId app config is set
@@ -83,7 +83,7 @@ Given sending "post" to ocs "/apps/libresign/api/v1/request-signature"
8383
| file | {"base64":""} |
8484
```
8585

86-
### Step: `fetch field :path from prevous JSON response`
86+
### Step: `fetch field :path from previous JSON response`
8787

8888
If the json response is an array, you can fetch specific values using this step. The fetched values is stored to be used by other steps.
8989

@@ -112,12 +112,22 @@ The alias `price` could be used in a path or body of a request:
112112
}
113113
"""
114114
And sending "POST" to "/"
115-
And fetch field "(foo)data.0.foo" from prevous JSON response
115+
And fetch field "(foo)data.0.foo" from previous JSON response
116116
# After fetch the field, you can use the value of field like this:
117117
And sending "POST" to "/?foo=<foo>"
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 previous 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.
@@ -150,7 +160,7 @@ If you need to:
150160

151161
Implement a method `parseText` like the follow code and remember to call parent method.
152162

153-
This methods can works together with `fetch field :path from prevous JSON response`
163+
This methods can works together with `fetch field :path from previous JSON response`
154164
```php
155165
protected function parseText(string $text): string {
156166
$patterns = [

features/test.feature

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,27 @@ Feature: Test this extension
7878
}
7979
"""
8080
And sending "POST" to "/"
81-
And fetch field "(FIELD_FOO)data.0.foo" from prevous JSON response
81+
And fetch field "(FIELD_FOO)data.0.foo" from previous JSON response
82+
# After fetch the field, you can use the value of field like this:
83+
And sending "POST" to "/?foo=<FIELD_FOO>"
84+
| field | <data.0.foo> |
85+
Then the response should be a JSON array with the following mandatory values
86+
| key | value |
87+
| data | [{"foo":"<FIELD_FOO>"}] |
88+
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 previous JSON response
82102
# After fetch the field, you can use the value of field like this:
83103
And sending "POST" to "/?foo=<FIELD_FOO>"
84104
| field | <data.0.foo> |

src/NextcloudApiContext.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,28 @@ private function validateAsJsonQuery(string $expected, string $actual): void {
322322
Assert::assertTrue($result, 'The jq "' . $expected . '" do not match with: ' . $actual);
323323
}
324324

325-
#[Given('fetch field :path from prevous JSON response')]
325+
#[Given('fetch field :path from previous JSON response')]
326326
public function fetchFieldFromPreviousJsonResponse(string $path): void {
327327
$this->response->getBody()->seek(0);
328-
$responseArray = json_decode($this->response->getBody()->getContents(), true);
329-
if (preg_match('/(?<alias>\([^)]*\))(?<patch>.*)/', $path, $matches)) {
328+
$body = $this->response->getBody()->getContents();
329+
330+
// Is json query
331+
if (preg_match('/(?<alias>\([^)]*\))\(jq\)(?<path>.*)/', $path, $matches)) {
332+
$this->fields[$matches['alias']] = $this->testAndGetActualValue(
333+
['key' => '(jq)' . $matches['path']],
334+
$body
335+
);
336+
return;
337+
}
338+
339+
// Is array with alias
340+
if (preg_match('/(?<alias>\([^)]*\)){1,}(?<path>.*)/', $path, $matches)) {
330341
$alias = $matches['alias'];
331-
$path = $matches['patch'];
342+
$path = $matches['path'];
332343
}
333344
$keys = explode('.', $path);
334-
$value = $responseArray;
345+
$value = json_decode($body, true);
335346
foreach ($keys as $key) {
336-
$body = json_encode($responseArray);
337-
Assert::assertIsString($body);
338347
Assert::assertArrayHasKey($key, $value, 'Key [' . $key . '] of path [' . $path . '] not found at body: ' . $body);
339348
$value = $value[$key];
340349
}

0 commit comments

Comments
 (0)