fix: fix "undefined variable" error if test consist "case" and "{{ $v…
Fix template parse error ("undefined variable") if test consist
- "case" section
- and any "{{ $variable }}"
For example like this:
- name: Test name
method: POST
path: /api/service/v1/endpoint
headers:
Authorization: "Bearer {{ $jwt }}"
request: >
{
"field1": "{{ .value1 }}",
"field2": "{{ .value2 }}",
}
response:
400: >
{"code":"BadRequestError"}
cases:
- requestArgs:
value1: "non-empty"
value2: "non-empty"
- requestArgs:
value1: ""
value2: "non-empty"
Thank you for your contribution. Please add tests inside runner, like runner/runner_test.go.
@Alexey19, please add more context to the problem you are trying to solve with this PR.
As I understand it, you want to use {{ $varName }} not as a template, but as a plain string to be sent, is that correct?
If that's correct, you can use this way to avoid errors - {{"{{"}}$varName{{"}}"}} - bacause gonkey uses a go templates. It certainly doesn't look good, but at this point, the solution suggested in PR seems to make it harder to understand the tests in gonkey.
As I understand it, you want to use {{ $varName }} not as a template, but as a plain string to be sent, is that correct?
No, I want to use it as template. But in case of "case" $varName starts working differently and generate parsing error.
If that's correct, you can use this way to avoid errors - {{"{{"}}$varName{{"}}"}} - bacause gonkey uses a go templates
I agree that I can do that. But this is not obvious to gonkey users, that after adding a "case", the $varname directive starts working differently. So better has this magic inside the code.
As a workaround, you can use yaml-format features:
method: POST
path: /api/service/v1/endpoint
variables: &vars
header: "Bearer {{ $jwt }}"
headers:
Authorization: "{{ .header }}"
request: >
{
"field1": "{{ .value1 }}",
"field2": "{{ .value2 }}",
}
response:
400: >
{"code":"BadRequestError"}
cases:
- requestArgs:
<<: *vars
value1: "non-empty"
value2: "non-empty"
- requestArgs:
<<: *vars
value1: ""
value2: "non-empty"
As a workaround, you can use yaml-format features:
I understand this part but want to fix gonkey to use usual way, without all this magic in tests.
My point is: test with and without "case" must support same syntax.
в https://github.com/lamoda/gonkey/pull/180 пофикшено гораздо лучше, чем в данном пул-реквесте