SpectoLabs / hoverfly

Lightweight service virtualization/ API simulation / API mocking tool for developers and testers
https://hoverfly.io
Apache License 2.0
2.35k stars 208 forks source link

Support multifile middleware #713

Open benjih opened 6 years ago

benjih commented 6 years ago

Currently, when setting middleware, Hoverfly expects one file to be set as your script. This means any middleware that is split over multiple files is currently incompatible with Hoverfly.

An example of this came up in #557 with a user trying to import another file in Javascript, although this issue is not specfic to JavaScript.

Right now, the script Hoverfly takes in is very basic, hoverctl will read the script and send its contents over HTTP, allowing Hoverfly to persist it. In the past Hoverfly worked on hard path to the script. For a while now though, Hoverfly has written the script to the tmp dir. During this setting process, the script loses the filename. Making things a slightly more complicated.

Workarounds

A workaround to this issue is to convert your multifile middleware into a HTTP server, run that in parallel to Hoverfly and configure Hoverfly with remote middleware, pointing to your localhost middleware HTTP server. The complexity of this workaround is dependant on your language choice. Languages such as Python, Go NodeJS are the easiest to convert into HTTP middleware servers.

Polyhedron-ZXero commented 4 years ago

Hi, any updates on this? Is there any plan to add this support?

ns3777k commented 2 years ago

@tommysitu would this be an option to create an additional flag to hoverctl like --directory that requires a path to a directory with middleware files. inside that directory a file like hoverfly_middleware.json must exist with following contents (example for nodejs):

{
  "entrypoint": "main.js"
}

hoverfly_middleware.json can be extended in future.

hoverctl would zip this directory and send it over to hoverfly. hoverfly would unzip it to a temp directory and set its middleware binary to the one passed with --binary flag and and script to the entrypoint from hoverfly_middleware.json.

if that seems ok, i can dig deeper.

tommysitu commented 2 years ago

@ns3777k thanks for looking into this. But I think the problem is that some multi-file scripts may not work after being moved to a temp folder due to file path changes. However if this is not an issue, we can add something simple to support it, as you mentioned, a new flag to support loading a directory. For the entry point, you can just pass it through the --script flag, assuming that it's relative to the root folder.

For example:

hoverctl middleware --binary node --script 'main.js` --root-folder `/home/my-middleware`

where main.js is located at /home/my-middleware/main.js

Provided all the files don't reference each other with an absolute path, they should be ok after being moved as well, but I could be too optimistic.