Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Static mocks feature not working in v0.4.1 #153

Open
DawMatt opened this issue Feb 21, 2025 · 13 comments
Open

Static mocks feature not working in v0.4.1 #153

DawMatt opened this issue Feb 21, 2025 · 13 comments
Labels
bug Something isn't working

Comments

@DawMatt
Copy link

DawMatt commented Feb 21, 2025

Issue:

It looks like the fix to issue #150 has disabled the new static mock dir feature.

Steps to reproduce:

  1. Attempt to start static mocking using a command similar to follows:
    wiretap --static-mock-dir .\mocks

Expected result:

Static mocking operates as per documentation page.

Actual result:

wiretap version: 0.4.1 | compiled: Thu, 20 Feb 2025 07:03:39 UTC
Designed and built by Princess Beef Heavy Industries: https://pb33f.io/wiretap

 INFO  No wiretap configuration located. Using defaults

 WARNING  No OpenAPI specification provided. Please provide a path to an OpenAPI specification using the --spec or -s flags.
          Without an OpenAPI specification, wiretap will not be able to validate requests and responses


  ERROR   No redirect URL provided. Please provide a URL to redirect API traffic to using the --url or -u flags.
@DawMatt
Copy link
Author

DawMatt commented Feb 21, 2025

I've also downloaded v0.4.0 and can reproduce similar behaviour with it.

Interestingly enough, if I use a different command line that is a blend of the static and dynamic mocking examples (i.e. adds a -u parameter):

wiretap --static-mock-dir ./mocks -u https://test.com

the actual result indicates the static mock part of the configuration is correct, but instead of mocking requests it proxies them instead. The static mock information provided is not being used and the requests are sent to the target (sorry test.com!).

 INFO  No wiretap configuration located. Using defaults

 WARNING  No OpenAPI specification provided. Please provide a path to an OpenAPI specification using the --spec or -s flags.
          Without an OpenAPI specification, wiretap will not be able to validate requests and responses

Ⓜ️ Static mock directory defined. Requests matching mock definitions in the static-mock-dir will return mocked responses.

 INFO  API Gateway UI booting on port 9090...
 INFO  Monitor UI booting on port 9091...

┌─ wiretap is online! ─────────────────────────────────────┐
|                                                          |
|   ┌─ API Gateway ─────────┐ ┌─ Monitor UI ──────────┐    |
|   | http://localhost:9090 | | http://localhost:9091 |    |
|   └───────────────────────┘ └───────────────────────┘    |
|                                                          |
└──────────────────────────────────────────────────────────┘

 INFO  wiretap is proxying all traffic to 'https://test.com'

@daveshanley daveshanley added the bug Something isn't working label Feb 21, 2025
@daveshanley
Copy link
Member

@akash4393 thoughts?

@DawMatt
Copy link
Author

DawMatt commented Feb 21, 2025

@akash4393 , @daveshanley , PR #155 should resolve the argument checking issue noted above.

In my development environment I still couldn't get the static mock feature to work correctly. I created the directory and file structures exactly as noted in the documentation. When issuing the command:

curl "http://localhost:9090/test"

the monitor UI showed an unending list of requests to the mock server, even though I'd only issued a single request, and no result was returned via curl. This could be a development environment issue or this feature may require some additional work.

@akash4393
Copy link
Contributor

@akash4393 , @daveshanley , PR #155 should resolve the argument checking issue noted above.

In my development environment I still couldn't get the static mock feature to work correctly. I created the directory and file structures exactly as noted in the documentation. When issuing the command:

curl "http://localhost:9090/test"

the monitor UI showed an unending list of requests to the mock server, even though I'd only issued a single request, and no result was returned via curl. This could be a development environment issue or this feature may require some additional work.

@DawMatt Are you trying to run the wiremock server with static mocking and without a redirect URL? I'm looking at the PR and it seems like that is what the code seems to be doing.

if !(mockMode || len(config.MockModeList) > 0) && len(config.StaticMockDir) == 0 && len(staticMockDir) == 0 && redirectURL == "" && harFlag == "" {

So I think if static mocking is enabled, I was thinking we would still have the rest of wiretap work as it would without static mocking. So if mockMode is not enabled and no redirect url is provided, we still want to throw an error since not all urls will be served by the static mock definitions.

@akash4393
Copy link
Contributor

akash4393 commented Feb 21, 2025

@DawMatt Can you also share the mocks you are defining? I'm able to see the responses from static mocks using curl as you.

curl "http://localhost/test"

Here's my mock definition

{
  "request": {
    "method": "GET",
    "urlPath": "/test"
  },
  "response": {
    "statusCode": 200,
    "body": "{\"test\": \"hello\"}"
  }
}

And this is how I am starting the wiremock server

wiremock -u "http://localhost:8089" --static-mock-dir "/path/to/mocks" --port 80

@akash4393
Copy link
Contributor

@DawMatt After thinking some more, I think it makes sense to merge your PR #155
Shared my thoughts here

@DawMatt
Copy link
Author

DawMatt commented Feb 21, 2025

@DawMatt Are you trying to run the wiremock server with static mocking and without a redirect URL? I'm looking at the PR and it seems like that is what the code seems to be doing.

if !(mockMode || len(config.MockModeList) > 0) && len(config.StaticMockDir) == 0 && len(staticMockDir) == 0 && redirectURL == "" && harFlag == "" {

Yes. I'm trying to run static mock dir feature without a redirect URL.

The change I made to the if statement above was to allow this. Currently it would only work if you set the mockMode flag AND staticMockDir. The documentation for this feature indicates only the staticMockDir argument should be provided so I altered this check to align with that. Otherwise the command errors because no URL was specified.

Alternatively I did try specifying a URL. In that instance it insisted on proxying the URL and didn't use the static mocks at all.

So I think if static mocking is enabled, I was thinking we would still have the rest of wiretap work as it would without static mocking. So if mockMode is not enabled and no redirect url is provided, we still want to throw an error since not all urls will be served by the static mock definitions.

Can you please explain what you mean by "the rest of wiretap work as it would without static mocking"?

Also can you please share the command line you normally use when using static mock dir? Maybe this is a documentation issue rather than a code issue.

@DawMatt
Copy link
Author

DawMatt commented Feb 21, 2025

@DawMatt Can you also share the mocks you are defining? I'm able to see the responses from static mocks using curl as you.

I'm actually using the command line and mocks included in the documentation without any changes. I started with this to see how the feature works, and then was going to adjust for my purposes. Unfortunately I couldn't get this to work. I've attached the files that I created basd upon the doco.

20250222-mocks.zip

I chose the example without the embedded JSON because this is how we'd want to use this.

The command line used was:

wiretap --static-mock-dir ./mocks

Interestingly enough the command line you used includes a URL, but the documentation indicates the new argument shouldn't require a URL to be supplied.

@DawMatt
Copy link
Author

DawMatt commented Feb 21, 2025

@akash4393 , a suggestion. Can you please look at the documentation page and verify that if someone steps through that example that the feature will work? Maybe the feature has evolved slightly since the docs were written. I tried to step through the example and couldn't get the feature to work.

@akash4393
Copy link
Contributor

akash4393 commented Feb 21, 2025

@akash4393 , a suggestion. Can you please look at the documentation page and verify that if someone steps through that example that the feature will work? Maybe the feature has evolved slightly since the docs were written. I tried to step through the example and couldn't get the feature to work.

@DawMatt you're 100% right. I revisited the doc and it's confusing since it seems to imply that you can run wiretap just as a static mock server. I will work on updating the doc to make it clearer.

That said, if you launch it with a redirect url then it should work, as I described in the comment above

So I just tested with the mocks you shared above and it works as expected for me. Note that the static mocks will be served when the entire request definition is matched. So you would need to curl with the expected query params and body.

This works -

curl --location --request GET 'http://localhost/test?test=ok&arr=1&arr=2' \
--header 'Content-Type: application/json' \
--data '{
    "test": "ok"
}'

But if I just do this -

curl "http://localhost/test"

Then it won't work since this request doesn't match the definition you have.

Another thing to note, in your json body file, you have the entire response instead of just the json of the body, so the API returns with a body

{
    "statusCode": 200,
    "header": {
        "something-header": "test-ok"
    },
    "body": "{\"test\": \"2\"}"
}

Maybe that is what you wanted, but I just wanted to call it out.

@DawMatt
Copy link
Author

DawMatt commented Feb 21, 2025

That said, if you launch it with a redirect url then it should work, as I described in the comment above

Thanks for providing the additional context here. It allowed me to get the current v0.4.1 release working, without my PR applied. I effectively used the command line from your comment, just needed to change wiremock to wiretap and then that worked OK. Combining that with the curl command you provided produced the expected results.

So I just tested with the mocks you shared above and it works as expected for me. Note that the static mocks will be served when the entire request definition is matched. So you would need to curl with the expected query params and body.

OK, that makes sense but wasn't something I understood initially. If the docs had a "quick start" section stepping through this example (and mentioning this point) it would be super helpful.

Another thing to note, in your json body file, you have the entire response instead of just the json of the body, so the API returns with a body

{
"statusCode": 200,
"header": {
"something-header": "test-ok"
},
"body": "{"test": "2"}"
}
Maybe that is what you wanted, but I just wanted to call it out.

OK, it appears I copied the wrong sample JSON from the docs into the test.json file. I actually meant to use the following:

{
  "test": "${queryParams.arr.[1]}"
}

which returns a more useful result:

{
        "test": "2"
}

Something else I noticed now that I've managed to successfully test the feature. Requests served by the static mock feature don't appear in the monitor UI. Those that don't match a static mock definition, and thus are proxied to the redirection URL, do appear in the monitor UI. From memory the regular wiretap mocking feature does display its requests in the monitor UI. It would seem more consistent if these static mock requests would also show in the monitor UI, but this is a "nice to have".

In summary:

  • staticMockDir feature only currently works if a URL is provided as well
  • static mock values will be returned if the request precisely matches the static mock definition, otherwise the request will be proxied to the redirection URL
  • static mock responses don't show in the monitor UI currently. Only the requests that "miss" all static mock definitions, and are proxied to the redirection URL, show in the monitor UI
  • some updates to the docs are necessary to reflect how the feature is intended to work. A "quick start" section with some worked examples would be extremely helpful.

@DawMatt
Copy link
Author

DawMatt commented Feb 22, 2025

@akash4393 , I've submitted a PR for the doco ( #156 ) to reflect your feedback here. In it the example has been expanded to include the command lines used to test the example and also show the expected response.

I think that should be sufficient to help anyone else using the feature for the first time.

@akash4393
Copy link
Contributor

Something else I noticed now that I've managed to successfully test the feature. Requests served by the static mock feature don't appear in the monitor UI. Those that don't match a static mock definition, and thus are proxied to the redirection URL, do appear in the monitor UI. From memory the regular wiretap mocking feature does display its requests in the monitor UI. It would seem more consistent if these static mock requests would also show in the monitor UI, but this is a "nice to have".

In summary:

  • staticMockDir feature only currently works if a URL is provided as well
  • static mock values will be returned if the request precisely matches the static mock definition, otherwise the request will be proxied to the redirection URL
  • static mock responses don't show in the monitor UI currently. Only the requests that "miss" all static mock definitions, and are proxied to the redirection URL, show in the monitor UI
  • some updates to the docs are necessary to reflect how the feature is intended to work. A "quick start" section with some worked examples would be extremely helpful.

This is a great summary @DawMatt. I totally agree with you that the static mocking feature can use a lot of improvements. Adding those calls to the monitor UI would be a great first step.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants