SpectoLabs / hoverfly

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

Simulate and capture-on-miss mode #864

Closed WouldYouKindly closed 4 months ago

WouldYouKindly commented 5 years ago

Hi!

I have a long workflow where I call multiple services. Sometimes I add or modify a call in the middle. I want to update my simulations, and have to re-record everything. It is very time-consuming to prepare real services for capturing.

It would be great to have a mode when Hoverfly simulates the requests that match, while passing the ones that don't to real services. It would allow me to upload a simulation (maybe having removed the changed requests from it beforehand), start my test, and have an updated simulation at the end.

crorodriguezro commented 5 years ago

Do you know if mock-server or wiremock have this feature?

tommysitu commented 5 years ago

Hoverfly has a spy mode that does simulate or pass through, so we can add a flag to change the not-found behaviour to capture, something like this:

hoverctl mode spy --capture-on-miss

or making it a string flag to support other on-miss behaviours in the future.

hoverctl mode spy --on-miss=capture

I don't think mock-server or wiremock has this feature. Hoverfly is a MITM proxy supports both HTTP/HTTPS, it's more flexible in terms of forward proxying.

larsthorup commented 4 years ago

We have a somewhat different use-case where I believe such a capture-on-miss feature could be useful.

We are using Hoverfly (for Java) to auto-mock requests to other services, in a micro-service architecture.

For example, one of our dependent services, D, will call another of our services, S, which in turn depend on some external service X.

When writing tests for service S, we would like to Simulate (or Spy) requests to service X from a previously captured file specific to service X. At the same time (while running these tests) we want to Capture the requests to service S (the service under test), so this capture file can later be used for auto mocking in the tests for service D.

Simulating and Capturing at the same time with a single Hoverfly instance would be very useful here, I believe?

tommysitu commented 4 years ago

@larsthorup I think only simulating the direct dependencies for each SUT is easier. For example,

  1. Test X by simulating S
  2. Then test S by simulating D.
larsthorup commented 4 years ago

Thank you @tommysitu.

I think I might have been unclear in my explanation of the direction of dependencies between the services.

Going with your reply, the services would call each other like this:

X -> S -> D

(so: X call S, and S call D)

In your step 2 we would AT THE SAME TIME capture the test-requests to S WHILE simulating the requests to D, because we would then later in step 1 while testing X use those previously captured requests to S to simulate S.

As far as I can see, this require us to do capture and simulate simultaneously, currently requiring us to orchestrate two Hoverfly instances.

And for this case, wouldn't hoverctl mode spy --on-miss=capture be a more elegant solution?

tommysitu commented 4 years ago

haven't thought of that, but it seems like a fair use case. The only problem is that you will be mixing two simulations in one file.

larsthorup commented 4 years ago

@tommysitu How big a task do you think this would be? Would you be interested in pull-requests to implement this feature?

odorT commented 2 years ago

Hi. I also come up to nearly similar issue (start with spy mode, capture and write response to simulation.json if request is not found in simulation.json). I really liked this solution, hoverctl mode spy --capture-on-miss but seems that it has not been implemented(at least I didn't found something similar in reference/docs). Will you consider it or I should go with larsthorup's workaround?

kapishmalik commented 1 year ago

@tommysitu are we still looking out to build this feature?

kapishmalik commented 1 year ago

@odorT @WouldYouKindly I have raised PR for this feature. Probably in couple of days, we should have this feature.

ayyasaran commented 1 year ago

When will this feature be available? Thanks.

kapishmalik commented 4 months ago

@tommysitu lot of folks are looking out for this feature.

kapishmalik commented 4 months ago

This feature is now available in release v1.9.2.

kapishmalik commented 4 months ago

You can enable capture on miss flag using hoverctl command line or while starting hoverfly.

johenning commented 4 months ago

You can enable capture on miss flag using hoverctl command line or while starting hoverfly.

So hoverctl mode spy --capture-on-miss works for me, but I can't seem to make it work with calling hoverfly directly:

$ hoverfly -spy -capture-on-miss -listen-on-host 0.0.0.0 -log-level error -log-no-color -logs json
flag provided but not defined: -capture-on-miss

Also, the flag is listed in the help command for hoverctl mode, but not with hoverfly.

Did I misunderstand or am I doing it wrong?

kapishmalik commented 4 months ago

@johenning oh yes, my bad. We have not added here. We can enable via Admin API by passing argument with mode and hoverctl cmd for now.

johenning commented 4 months ago

@kapishmalik all good, just wanted to make sure I'm understanding it right. I'm just going to set the flag via the REST API:

PUT /api/v2/hoverfly/mode
{
    "mode": "spy",
    "arguments": {
        "captureOnMiss": true
    }
}
kapishmalik commented 4 months ago

@johenning yea correct. We will be soon adding documentation.