gonkey icon indicating copy to clipboard operation
gonkey copied to clipboard

fix: fix "undefined variable" error if test consist "case" and "{{ $v…

Open lansfy opened this issue 3 years ago • 5 comments

Fix template parse error ("undefined variable") if test consist

  1. "case" section
  2. 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"

lansfy avatar Jan 08 '22 05:01 lansfy

Thank you for your contribution. Please add tests inside runner, like runner/runner_test.go.

Cdayz avatar Jan 10 '22 08:01 Cdayz

@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.

Cdayz avatar Jan 10 '22 09:01 Cdayz

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.

lansfy avatar Jan 10 '22 15:01 lansfy

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"

leorush avatar Jan 10 '22 15:01 leorush

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.

lansfy avatar Jan 10 '22 17:01 lansfy

в https://github.com/lamoda/gonkey/pull/180 пофикшено гораздо лучше, чем в данном пул-реквесте

lansfy avatar Dec 17 '22 17:12 lansfy