Closed cjol closed 3 years ago
N.B. if the template is stored as a string, we cannot manipulate it very easily (e.g. to retrieve body.foo.bar
inside the JSON document). In places where we need to do such manipulation, I suggest that we parse the JSON document on-demand (after applying twig substitutions) instead of "pre-parsing" it and storing the structured object in the database.
JSON is the expected format for most of our payloads, so it makes sense that we have prioritised it, but we should not force the payloads to always be JSON (e.g. in the common case of an empty response).
I think the visual JSON editor as part of the test case editor is nice, but it is less important to me than supporting the patterns above. So if that means that it is necessary to remove the JSON editor, then I'm happy to stick with a plaintext textarea
.
Hi Christopher, @cjol
Just wanted to check if our previous work on #343 still makes sense. Variables support branch contains the next features:
{ "first": 1, "second": 2, "third": "{{ 1 + 2 }}"}
{"body": {"amount": "{{ 1 + ${ENV_VALUE} }}" }}
- calculations using env. variables{"body": {"amount": "{{ 1 + steps.2.request.body.amount} }}" }}
- calculations using Test Step variables{"body": {"amount": "{{ ${ENV_VALUE} + steps.2.request.body.amount} }}" }}
- calculations using both, env. and Test Step, variables{"body": {"transactionRequestId": "{{ uuidv4() }}" }}
- a custom function that generates uuid v4 (if need more functions e.g. to generate someOtherOrRandomValue()
- they are to be written additionally)test_request_scripts
, test_response_scripts
, request
, and response
. description
or components
) and TS main fields (e.g. pattern
, source
or trigger
).{ "body": {{ test_steps[0].request.body }} }
is not supported as right now we can't get objects/arrays using Twig. Only 'values' can be withdrawn e.g. if body.amount = 1, we can get it using test_steps[0].request.body.amount
.FEE
= 2, fee
= 1, AMOUNT
= 1, and amount
= 1It's not there, but should be possible to be developed pretty fast (e.g. <1d)
It's not there, but should be possible to be developed pretty fast (e.g. ~1-2d). Maybe we are to add some syntax for YAML and for the existing TC edit UI
Looks like both options can be added pretty quickly w/o global changes to the existing backend and UI.
Not there and need to think about it as it requires pretty big changes to the way of data storing (now we store JSONs in db).
Same as 5.
Seems like, from the technical perspective, 'not valid JSON' and 'non-JSON payload' are almost the same - i.e. require adding some way to handle non-JSON data, as Postman does. We need to investigate how much work is it, but it looks like a bit slice of existing code to be changed... These features are to be used in request.body
and response.body
only, right?
Please let us know if this works. Thank you!
Hi Anton,
I'm a little bit disappointed because I did anticipate these problems - but it looks like I didn't get my concerns across at the right time. Quoting from #343:
- Define one big template string for the request body in the YAML
- Store the template as a string in the database
In terms of moving forward, there are still some oustanding practical problems with 1-2, which I will add detail to below. 4 is also a practical problem, but we can add a workaround as you describe it. 3, 5 & 6 are lower in importance because they are "theoretical" problems that we can avoid in our current test case roadmap.
I am a little bit surprised that supporting non-JSON payloads is a big change because I didn't think we did any processing to the payload (I thought we just read it from the test case and dump it into the request). But you all know the codebase better than I do, so I will defer to your judgement!
For now, let's add a workaround to allow us to construct empty bodies (4), and I can tolerate the remaining problems for now. I would also be thankful if you can find some time to do the investigation of work required for supporting non-JSON payloads so that I can think about when to include it in our roadmap.
Another note: it looks like we are preserving the old syntax for injecting environment variables and the new syntax (amount: '{{ ${FEE} + ${AMOUNT} + ${fee} + ${amount} }}'
). That seems redundant - we can simplify things by only supporting the new syntax:
amount: '{{ env.FEE + env.AMOUNT + env.fee + env.amount }}'`
(N.B. the environment variables are strings but we need numeric data - luckily Twig will automatically convert the data type for us when we're doing maths)
Currently, I think request/response templates are parsed from the YAML file as structured objects, and stored in the DB as the same. My expectation was that we would store these templates/examples as strings so that we can have more control over the exact payload that the simulators send. Here are some example payloads that I think we can't achieve with the current setup:
Inject a numeric value
Input:
Expected Output:
Problem: Testcase will fail to parse because
{{ 1 + 2 }}
is not valid JSON.Inject a non-primitive value
Input:
Expected Output:
Problem: Testcase will fail to parse because
{{ test_steps[0].request.body }}
is not valid JSON.Inject a variable into key position
Input:
Expected Output:
Problem: JSON keys are not currently examined for variable substitution, so the output will contain
{{ environment.RESULT_KEY }}
Create empty request body
Input:
Expected Output:
Problem: Testcase will fail to parse because "" is not valid JSON (and cannot be created through a visual JSON editor)
Create malformed JSON (e.g. to test graceful response to errors)
Input:
Expected Output:
Problem: Testcase will fail to parse because the document is not valid JSON (and cannot be created through a visual JSON editor)
Create non-JSON payload (e.g. an XML response for a future API)
The problem here is that we won't be able to parse the testcase when we import it (or create it through the JSON editor), if the template is expecting a valid JSON object. Input:
Expected Output:
Problem: Testcase will fail to parse because the document is not valid JSON (and cannot be created through a visual JSON editor)