hoverfly icon indicating copy to clipboard operation
hoverfly copied to clipboard

chore(hoverfly): add form processing for requests

Open petkostas opened this issue 2 years ago • 5 comments

Adds support for form processing in Hoverfly. I recently encountered a case where I needed to parse form data for one of the virtual APIs. This became painful and required me to introduce a middleware. Using the ParseForm of GO we can easily extend the request details with this logic.

⚠️ This is a draft, possibly there are other places we could add functionality (e.g. matchers?) ⚠️

To be added:

  • Tests
  • Extend request matchers (?)

petkostas avatar Sep 20 '22 11:09 petkostas

@petkostas I have fixed the tests already, you can try to rebase on the latest master.

Because form present in the request body, I think we should just create a new form matcher for the body like this:

          "body": [
            {
              "matcher": "form",
              "value": {
                "foo": [
                  {
                    "value": "bar",
                    "matcher": "exact"
                  }
                ],
                "low": [
                  {
                    "value": "bar",
                    "matcher": "exact"
                  }
                ]
              }
            }
          ],

what do you think?

tommysitu avatar Sep 21 '22 21:09 tommysitu

related to https://github.com/SpectoLabs/hoverfly/issues/903

tommysitu avatar Sep 21 '22 21:09 tommysitu

@tommysitu I agree with the matcher (that is also why I included it with a ? in the initial post), would help a lot and avoid unnecessary middleware's for this kind of scenarios. Rebasing and will try to get the matcher in today as well.

petkostas avatar Sep 22 '22 16:09 petkostas

@tommysitu Haven't abandoned this, I will most probably revisit this next week in order to push the changes.

petkostas avatar Sep 29 '22 16:09 petkostas

@tommysitu picked this up again, will modify the matcher to the proposal (I initially performed a separate one, but your suggestion makes more sense).

petkostas avatar Dec 02 '22 15:12 petkostas

@tommysitu I have the first draft, though as I am still fresh with GO would be best if you can check and recommend maybe a different way, I had a horrible time (and a lovely GO enlightening experience for a Python developer) finding a proper way to transform the nested Matchers.

petkostas avatar Dec 09 '22 18:12 petkostas

Found a small issue with the caching (as it does not include the form data) will make a change to that accordingly as well

petkostas avatar Dec 10 '22 13:12 petkostas

@petkostas Seems like a simple elegant solution, all existing tests passed 👍 I will poke around a bit more, hopefully nothing break. I will let you know.

tommysitu avatar Dec 10 '22 23:12 tommysitu

TODO:

  • [x] Fix capture mode
  • [x] Add form data to concatenate
  • [x] Update documentation

petkostas avatar Dec 23 '22 11:12 petkostas