stubby4node comma separated query string implementation issue

340 views Asked by At

I have a URL (GET REQUEST) of the following pattern

  • ^/testpath/1/test?pathid=1
  • ^/testpath/1/test?pathid=1,2
  • ^/testpath/1/test?pathid=1,2,5

where pathid query string parameters are comma separated

I have the following stubby mappings to match these url patterns

- request:
    url: ^/testpath/(.*)/test
    query:
      pathid: '1'
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-1.json

- request:
    url: ^/testpath/(.*)/test
    query:
      pathid: '1,2'
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-2.json

- request:
    url: ^/testpath/(.*)/test
    query:
      pathid: '1,2,5'
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-3.json

but I can't get this URL mapping to properly deliver different payloads based on different parameter combinations.

  • 1 -> payload1
  • 1,2 -> payload2
  • 1,2,5 -> payload3

how can this be done?

2

There are 2 answers

0
Sanath On BEST ANSWER

The trick here is to change the order of request/response mappings in the yaml file.

- request:
    url: ^/testpath/(.*)/test
    query:
      **pathid: '1,2,5'**
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-3.json

- request:
    url: ^/testpath/(.*)/test
    query:
      **pathid: '1,2'**
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-2.json

- request:
    url: ^/testpath/(.*)/test
    query:
      **pathid: '1'**
    method: GET
  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/path-1.json

note the path ids added in the descending order, so that more deeper matches will be executed first.

This way I was able to achieve this following requirement, of passing different path ids separated by a comma

  • 1 -> payload1
  • 1,2 -> payload2
  • 1,2,5 -> payload3
4
Alexander Zagniotov On

@Sanath, short answer - yes, stubby4j matches on different stubbed query parameters combinations.

Few questions:

  1. What version of stubby4j are you running?
  2. Also, are you running stubby4j as a standalone JAR or as one of the pre-built stubby4j Docker containers?

I wrote a test to validate the YAML configuration you provided in your question: https://github.com/azagniotov/stubby4j/pull/434/files (do note, I did change the url a little, but it is still a regex similar to your posted YAML).

The requests that test makes do match the stubs. (I did try without $ and with a $, like @code suggested). In addition, I also tested with cURL against a running standalone JAR:

curl -X GET  http://localhost:8882/stackoverflow/70417269/1/test?pathid=1,2,5

and

curl -X GET  http://localhost:8882/stackoverflow/70417269/1/test?pathid=1,2

Again, I got the expected responses from the server.

If the above does not help you, please feel free to raise a bug report https://github.com/azagniotov/stubby4j/issues/new/choose