What is the best way to configuring multiple proxy urls for multiple services in wiremock? - 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.

Related

Avoid logging specific requests in Wiremock

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/.

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/"
}
}
}

API POST not applied to branch with Azure DevOps Server (2019 Update 1)

I'm puzzled with the Azure DevOps API POST. I've successfully created a policy for "Minimum number of reviewers" but it's not being created against the branch I'm specifying. I've done this in Fiddler for the moment, here's my request (private values obfuscated):
POST http://our-tfs-server:8080/tfs/TheCollection/TheProject/_apis/policy/configurations?api-version=5.1 HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: our-tfs-server:8080
Authorization: Basic MyBase64EncodedToken
{
"IsBlocking": true,
"IsEnabled": true,
"Settings": {
"MinimumApproverCount": 2,
"Scope": [
{
"RefName": "refs/heads/master",
"MatchKind": "exact",
"RepositoryId": "7718333c-044e-423a-baa1-45e6a1f0ff41"
}
]
},
"Type": {
"Id": "fa4e907d-c16b-4a4c-9dfa-4906e5d171dd"
}
}
and here's the response body:
{
"createdBy": {
"displayName": "Surname, Firstname",
"url": "http://our-tfs-server:8080/tfs/TheCollection/_apis/Identities/SomeString",
"_links": { "avatar": { "href": "http://our-tfs-server:8080/tfs/TheCollection/_apis/GraphProfile/MemberAvatars/win.SomeString" } },
"id": "7b40f8ab-a933-4b43-bdf8-bf0b179d28e6",
"uniqueName": "MyUsername",
"imageUrl": "http://our-tfs-server:8080/tfs/TheCollection/_api/_common/identityImage?id=SomeString",
"descriptor": "win.SomeOtherString"
},
"createdDate": "2020-01-23T02:09:34.4738854",
"isEnabled": true,
"isBlocking": true,
"isDeleted": false,
"settings": {
"minimumApproverCount": 2,
"creatorVoteCounts": false,
"allowDownvotes": false,
"resetOnSourcePush": false,
"scope": [ { "repositoryId": "7718333c-044e-423a-baa1-45e6a1f0ff41" } ]
},
"_links": {
"self": { "href": "http://our-tfs-server:8080/tfs/TheCollection/8b7e65ed-1136-4b0f-9780-de2a3860447a/_apis/policy/configurations/254" },
"policyType": { "href": "http://our-tfs-server:8080/tfs/TheCollection/8b7e65ed-1136-4b0f-9780-de2a3860447a/_apis/policy/types/fa4e907d-c16b-4a4c-9dfa-4906e5d171dd" }
},
"revision": 1,
"id": 254,
"url": "http://our-tfs-server:8080/tfs/TheCollection/8b7e65ed-1136-4b0f-9780-de2a3860447a/_apis/policy/configurations/254",
"type": {
"id": "fa4e907d-c16b-4a4c-9dfa-4906e5d171dd",
"url": "http://our-tfs-server:8080/tfs/TheCollection/8b7e65ed-1136-4b0f-9780-de2a3860447a/_apis/policy/types/fa4e907d-c16b-4a4c-9dfa-4906e5d171dd",
"displayName": "Minimum number of reviewers"
}
}
Note that "scope": [ { "repositoryId": "7718333c-044e-423a-baa1-45e6a1f0ff41" } ] only has the repositoryId.
In the UI on the policies for the branch the Minimum Number of reviewers option is not ticked and there's no version of the question at a full Repository level. If I call /_apis/policy/configurations then I see the new policy so it has been created. Perhaps this will now guard all branches with the policy, but we don't want it at a global level and given there's no tooling in the DevOps web ui for it I'm pretty sure it's not intended to be like that anyway.
So, is this an oversight in the way the API functions or is the branch to apply the policy configuration to set using another technique?
So...the answer lay in the capitalisation of property names on the json request body. Whilst json is not case sensitive and the request I was making was returning 200 and a valid body internally, the server was falling apart on the setting of the branch to apply the Policy Configuration to. NewtonSoft was serialising property names with capital letters at the beginning and I found that using lowercase fixed the original issue and the returned object which was created then had the refName and matchKind assigned and the MinimumNumberOfReviewers check box was ticked and the number set to 2. Why MS have failed to serialise only one or 2 properties is a little weird, at any rate I applied JsonSerializationSettings globally to avoid the need for adding JsonPropertyAttribute everywhere as well as ignoring null properties.

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

Fetching multiple event details in Office 365

I need to fetch information related to eventId from office 365 for multiple events.
Is there a way I can get that info in a single REST call?
I want specific events only (based on eventId's only)
A batch request may be what you are looking for.
See json Batching Documentation for more information
Keep in mind that batching is currently limited to 20 requests per message (known issues)
Example:
You will need to send a POST Message to the batch endpoint
https://graph.microsoft.com/v1.0/$batch
inside the body you will need to include your requests:
Note: do not include the server url (https://graph.microsoft.com/v1.0/) in the url property or the request will fail with "BadRequest - Invalid request Uri".
Request-Body:
{
"requests": [
{
"id": "1",
"method": "GET",
"url": "/me/calendarview?startdatetime=2018-03-01T18:31:34.206Z&enddatetime=2018-03-12T18:31:34.206Z"
},
{
"id": "2",
"method": "GET",
"url": "/me/events/{someEventId}"
},
]
}
When the server has processed all requests an response array containing the results will be sent back:
Server-Response:
{
"responses": [
{
"id": "2",
"status": 200,
"headers": {
"OData-Version": "4.0",
"Content-Type": "application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8",
"ETag": "W/\"Z+ICSvkiAfZX7XWQAZ6IH==\""
},
"body": {
// the event object
}
},
{
"id": "1",
"status": 200,
"headers": {
"OData-Version": "4.0",
"Content-Type": "application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8"
},
"body": {
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('aUserID')/calendarView",
"value": [
// list of found event-objects
]
}
}
]
}