Avoid logging specific requests in Wiremock - scala

I'm using Wiremock 2.28.1 in a Scala 2.13 project and I would like to skip logging specific requests/responses in Wiremock because they are just liveness/readiness HTTP calls targeting health/ready and health/alive endpoints:
{
"mappings": [
{
"request": {
"method": "GET",
"url": "/api/v1/health/ready"
},
"response": {
"status": 200,
"body": "",
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET",
"url": "/api/v1/health/alive"
},
"response": {
"status": 200,
"body": "",
"headers": {
"Content-Type": "text/plain"
}
}
}
]
}
The WireMock server configuration is:
new WireMockConfiguration()
.bindAddress(configOptions.host)
.port(configOptions.port)
.stubCorsEnabled(configOptions.enableCors)
.disableRequestJournal() // avoid JVM heap exhaustion for recorded requests
.usingFilesUnderClasspath(s"$confBasePath/${configOptions.mappingsFolder}")
.notifier(new ConsoleNotifier(configOptions.verboseNotifier)) // <-- TRUE
.httpsPort(...)
Is there a way to tell WireMock to simply skip logging the /health/* API calls?

There are a couple of ways you could do this:
Write your own Notifier implementation that filters messages based on their content and use this in place of ConsoleNotifier in your startup options.
Make the healthcheck an admin API function by implementing AdminApiExtension and registering it in the startup options (this will mean the URL will have to be under /__admin/.

Related

Implement different response with WireMock when no request(s) match

I'm trying to stub a RESTful API. One of the resources return the details when the resource is (indeed) found, or an HTTP 404 (Not Found) when, eventually, there is no resource for the given URL.
This is my simplified stub/mapping:
{
"mappings": [
{
"name": "Retrieve Items",
"request": {
"headers": {
"accept": {
"caseInsensitive": true,
"equalTo": "application/json"
}
},
"method": "GET",
"urlPathPattern": "/api/items/[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}"
},
"response": {
"bodyFileName": "items-api/responses/retrieve/{{ request.pathSegments.[2] }}.json",
"headers": {
"content-type": "application/json"
},
"status": 200
}
}
]
}
Then I have several JSON files (in /home/wiremock/__files/items-api/responses/retrieve/ to match the requests against — but I can't find a way to implement the HTTP 404 (Not Found) scenario:
{
"timestamp": {{ now }},
"status": 404,
"error": "Not Found",
"message": null,
"path": "{{ request.path }}"
}
With this config I get back (the expected, but not useful for my use case) response from WireMock that the file name uuid-sent-in-request.json is not found.
Is there a way to implement this behavior currently?
Tom's answer will work as well. I think the benefits to his solution are that they aren't tied to specific request URLs, but my suggestion is to have a specific mapping for the files that will match with their specific JSON files, and a catch-all mapping for un-matched files. By assigning the requests with JSON responses a higher priority, WireMock will check those first, and if the request does not match any of the values specified in that mapping, will then go on to check if the second mapping matches, and return a 404.
{
"mappings": [
{
"name": "Retrieve Items - Success",
"priority": 1, // arbitrary number lower than the second priority
"request": {
"headers": {
"accept": {
"caseInsensitive": true,
"equalTo": "application/json"
}
},
"method": "GET",
"urlPathPattern": "/api/items/(UUID1|UUID2|UUID3|UUID4)"
},
"response": {
"bodyFileName": "items-api/responses/retrieve/{{ request.pathSegments.[2] }}.json",
"headers": {
"content-type": "application/json"
},
"status": 200
}
},
{
"name": "Retrieve Items - 404 Not Found",
"priority": 5, // arbitrary number that is higher than 1
"request": {
"headers": {
"accept": {
"caseInsensitive": true,
"equalTo": "application/json"
}
},
"method": "GET",
"urlPathPattern": "/api/items/[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}"
},
"response": {
"status": 404
}
}
]
}
Currently you would need to write a ResponseDefinitionTransformer to get the behaviour you're looking for.
It would need to check whether the ResponseDefinition passed in the parameter required a file, then if so check whether the file exists by doing something like:
try {
fileSource.getBinaryFileNamed(bodyFileName).readContents();
} catch (FileNotFoundException e) {
return WireMock.notFound().withBody("custom body");
}
// Otherwise return the unmodified response definition

Wiremock how to map a 302 response in JSON for standalone run

In wiremock, if you are using it for testing, you can programately return 302 using temporaryRedirect(String destination) with a detination path
stubFor(post(urlEqualTo("/firsturl"))
.willReturn(temporaryRedirect("https://somehost:8080/test/")))
My question is: How can I do the same in standalone running mode, using json mapping file
{
"request": {
"method": "POST",
"url": "/api/test"
},
"response": {
"status": 302,
????? <- how to return the path?
}
}
Was not able to find anything about this.
If you want to redirect to a separate url, you can use the WireMock's proxying functionality.
It would look something like...
{
"request": {
"method": "POST",
"url": "/api/test"
},
"response": {
"status": 302,
"proxyBaseUrl": "https://somehost:8080/test/"
}
}
temporaryRedirect("https://somehost:8080/test/") is a wrapper around aResponse().withStatus(302).withHeader(LOCATION, "https://somehost:8080/test/")
In json configuration it will be:
{
"request": {
"method": "POST",
"url": "/api/test"
},
"response": {
"status": 302,
"headers": {
"Location": "https://somehost:8080/test/"
}
}
}

Wiremock Capture path param and return in the response body

I am trying to create dynamic mocks using WireMock. I have a situation where if I specify URL like :
http://localhost:8089/api/account/abc#abc.com
then I should receive response like:
{
"account" : "abc#abc.com"
}
In brief, the path param is returned in the response body. I am able to make the request URL generic by using urlPathPattern set to /api/account/([a-z]*) however, I am not sure how should I capture abc#abc.com and return this in the response using regular expressions.
In WireMock the regular expressions can be used to recognize the email format in the Request Matching. For the purpose of this example I used a very crude example. Your production implementation may require a more robust approach.
This request:
http://localhost:8181/api/account/someone#somewhere.net
Is matched by this rule:
{
"request": {
"method": "GET",
"urlPathPattern": "/api/account/([a-z]*#[a-z]*.[a-z]*)"
},
"response": {
"status": 200,
"jsonBody": {
"account": "{{request.path.[2]}}"
},
"transformers": ["response-template"],
"headers": {
"Content-Type": "application/json"
}
}
}
And returns this response:
{
"account": "someone#somewhere.net"
}
It makes use of a Response Template processing functionality in WireMock. The Request Model variables [{{request.path.[2]}}] can be used to obtain sections from the request.
The same can be done using WireMock.Net - Response-Templating
The rule looks like:
{
"Request": {
"Path": {
"Matchers": [
{
"Name": "RegexMatcher",
"Pattern": "^/api/account/([a-z]*#[a-z]*.[a-z]*)$"
}
]
},
"Methods": [
"get"
]
},
"Response": {
"StatusCode": 200,
"BodyAsJson": {
"account": "{{request.PathSegments.[2]}}"
},
"UseTransformer": true,
"Headers": {
"Content-Type": "application/json"
}
}
}

WireMock not mapping request by body contains

My http request send to this: https://myhost.com/ap
My http request with body :
{
"Body": {
"CommandName": "GetApplicationProfile"
},
"Header": {
"Command": "GetApplicationProfile",
}
}
I want to mapping this request by WireMock.
Here WireMock's mapping file.
{
"request": {
"url": "/my_host/ap",
"bodyPatterns": [
{
"contains": "GetApplicationProfile"
}
]
},
"response": {
"headers": {
"Content-Type": "application/json"
},
"status": 200,
"bodyFileName": "get_profile.json"
}
}
I start wireMock like this:
java -jar wiremock-standalone-2.18.0.jar --port 8080 --enable-browser-proxying -verbose
But when request was started the WireMock not map this request. Nothing happened.
why?
The problem you're having is that you shouldn't have the hostname in the url part. This is not needed. Your example message can be sent and will be matched using the following rule.
{
"request": {
"url": "/app",
"bodyPatterns": [
{
"contains": "GetApplicationProfile"
}
]
},
"response": {
"headers": {
"Content-Type": "application/json"
},
"status": 200,
"body": "ddd"
}
}
The URL should not contain the host name. It should only contain the resource path.
The url format should start with "/" e.g. /https://myhost.com/ap.
Now if u r trying this on localhost then the URL should be localhost:<port>/https://myhost.com/ap.
The file should be present at src/test/resources/__files
If not it will give error file not present.
I see 2 issues here:
1. you need to remove the host name from the url in the mapping file.
2. your request is HTTPS which means you need to start your wiremock port with https: --https-port 8080 or you change your request to HTTP

What is the best way to configuring multiple proxy urls for multiple services in wiremock?

I am using WireMock for service virtualization. I am using proxying functionality to forward all the unmatched requests to the live url. The problem is I am virtualizing 5 different services, how can I provide proxy urls for each service to forward it to corresponding live url in case of mismatch?
As of now I am using 5 different json files with proxyBaseUrl and high priority and setting them by default after starting the service. Is there any better way to configure multiple proxy urls for multiple services?
{
"request": {
"method": "POST",
"urlPattern": "/a/.*"
},
"response": {
"proxyBaseUrl" : "https://srvc1.com"
},
"priority": 10
}
{
"request": {
"method": "POST",
"urlPattern": "/b/.*"
},
"response": {
"proxyBaseUrl" : "https://srvc2.com"
},
"priority": 10
}
{
"request": {
"method": "POST",
"urlPattern": "/e/.*"
},
"response": {
"proxyBaseUrl" : "https://srvc5.com"
},
"priority": 10
}
There is currently no support for importing multiple request/response pairs in the same json file.