Wiremock not able to match the url - wiremock

I have very basic mapping.json
{
"mappings": [
{
"priority": 1,
"request": {
"method": "GET",
"url": "/your/url?and=query"
},
"response": {
"status": 200,
"statusMessage": "query param test"
}
},
{
"priority": 2,
"request": {
"method": "GET",
"url": "/your"
},
"response": {
"status": 200,
"statusMessage": "no query param"
}
}
]
}
It's the exact same example as given in the documentation.
Result:
admin ~ % curl -i http://localhost:8081/your
HTTP/1.1 200 no query param
Matched-Stub-Id: 6ff84303-8abb-48d0-bd27-679de118afc7
Transfer-Encoding: chunked
Server: Jetty(9.2.z-SNAPSHOT)
admin ~ % curl -i http://localhost:8081/your/url?and=query
zsh: no matches found: http://localhost:8081/your/url?and=query
admin ~ %
Cannot figure out what I am doing wrong here. It's exactly the same example give in the documentation. I tried putting query parameter like this:
"queryParameters" : {
"search_term" : {
"equalTo" : "WireMock"
}
},
This also didn't help.
TIA

Check out the answer and comment from this question, but the tl;dr is that if you want to include query parameters in a CURL request, you have to have the URL in quotes.
That would explain why Postman worked, and the CURL request without query parameters also worked, but the CURL request with query parameters did not.
curl -i 'http://localhost:8081/your/url?and=query' should be enough to solve your problem (might need double quotes instead of single?)

Related

get a particular object from jsonBody with query param of request url in wiremock

What should be the mapping object if
1) I need to pass my query
2) the query should be used to send one object out of an array of objects
curl -X POST --data '
{ "request":
{ "url": "/jsons?id=someID", "method": "GET" },
"response":
{ "status": 200, "jsonBody": {"objs":[{"id":"1","name":"abc"},{"id":"2","name":"cde"
{"id":"someID","name":"efg"}]}}}
'http://localhost:8080/__admin/mappings/new
I want the above url to return just {"id":"someID","name":"efg"}
How should i change the above mapping to get the desired output
The response should have one object for that particular get request with query param and not array of objects.
For your example it should be something like this
curl -X POST --data '
{ "request":
{ "url": "/jsons?id=someID", "method": "GET" },
"response":
{ "status": 200, "jsonBody": {"objs": {"id":"someID","name":"efg"}}}}
'http://localhost:8080/__admin/mappings/new

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

Wiremock - How can I apply response templating to the header name?

I am trying to provide a MOCK service that takes a headerName and value from the query and returns it as a (dynamic) header with the response. I am using the following response definition:
"response" : {
"status" : 200,
"statusMessage": "OK",
"headers" : {
"Content-Type" : "application/json",
"{{request.query.debugHeader}}" : "{{request.query.debugHeaderValue}}"
},
"jsonBody" : {
"headerSent": "{{request.query.debugHeader}} {{request.query.debugHeaderValue}}"
},
"transformers": ["response-template"],
"base64Body" : ""
}
The header value is correctly evaluated and put into the response template, however I can't get the header name to be taken from the request.
When sending a request:
http://localhost:8090/example?debugHeader=name&debugHeaderValue=value
The result headers I get back are:
HTTP/1.1 200 OK
Content-Type: application/json
{{request.query.debugHeader}}: value
However I want {{request.query.debugHeader}} to be replaced with the actual request parameter value ("name" in the example above).
Any ideas?
Thanks in advance
Alex
This is supported in WireMock.Net.
The request which you need to specify looks like this:
{
"Guid": "90356dba-b36c-469a-a17e-669cd84f1f05",
"Priority": 0,
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/trans",
"IgnoreCase": false
}
]
},
"Methods": [
"get"
]
},
"Response": {
"StatusCode": 200,
"BodyDestination": "SameAsSource",
"Body": "{\"msg\": \"Hello world : {{request.path}}\" }",
"UseTransformer": true,
"Headers": {
"Content-Type": "application/json",
"Transformed-Postman-Token_{{request.path}}": "token is {{request.headers.Postman-Token}}"
}
}
}
This will add the transformed header Transformed-Postman-Token_{{request.path}} in the response.
Presently this type of variability is not part of the out-of-the-box WireMock application and would have to be custom added. In the WireMock documentation the section Extending WireMock and in particular the part on Transforming Responses.

SugarCRM API - Filter POST - Related Module Fields

I'm trying to retrieve, using REST API, a list of records and one of its related module fields. Let's assume Accounts and Opportunities.
So, API Documentation (in the GET Filter) talks about defining related module in the fields parameter:
According to the same documentation, it would provide a result similiar to:
That's exactly what I need, but I'm trying to achieve this using the POST method. So, following the same path, I'm sending (using Postman):
PS: I tried all combinations of double quotes. Escaped, not escaped, with or without it, all of them give me the same result, that is:
The message is in pt-BR but it means "One of your request parameters is wrong". The HTTP status code is 422 - Unprocessable Entity.
What am I doing wrong? I tried everything and just don't know how to make it work. Looks like the documentation talks about something that simply don't work or doesn't exist.
Okay, so after some research, it seems that Postman uses something similar to Chrome and it isn't really possible to send GET requests with the values in the body. However, you can...
Encode the values into the URL and send it through Postman:
https://yoursite.com/rest/v10/Accounts?filter%5B0%5D%5Bopportunities.date_modified%5D%5B%24gte%5D%3D2016-02-29T00%3A00%3A00&fields=cpf_c,opportunities&max_num=10
Use curl:
curl -X GET -H Host:yoursite.com -H OAuth-Token:d49c8fd4-0ae0-d9fb-7ab8-5846e5a3fa86 -H Cache-Control:no-cache -d '{"filter":[{"opportunities.date_modified":{"$gte":"2016-02-29T00:00:00"}}],"fields":["cpf_c","opportunities"],"max_num":"10"}' https://yoursite.com/rest/v10/Accounts
Or build the HTTP request directly, and include the body:
GET https://yoursite.com/rest/v10/Accounts HTTP/1.1
Host: yoursite.com
OAuth-Token:d49c8fd4-0ae0-d9fb-7ab8-5846e5a3fa86
Cache-Control:no-cache
{"filter":[{"opportunities.date_modified":{"$gte":"2016-02-29T00:00:00"}}],"fields":["cpf_c","opportunities"],"max_num":"10"}
To test this, I created 3 Accounts, 2 of which had an Opportunity linked. The response was this when I used "name" instead of your "cpf_c" field:
{
"next_offset": -1,
"records": [{
"id": "64417139-459c-852f-3a73-5846ed1245c2",
"name": "another account with opp",
"date_modified": "2016-12-06T16:54:29+00:00",
"opportunities": {
"next_offset": -1,
"records": [{
"id": "32d1d320-c560-92d6-7def-5846eda786da",
"date_modified": "2016-12-06T16:55:06+00:00",
"_acl": {
"fields": {}
},
"_module": "Opportunities"
}]
},
"_acl": {
"fields": {}
},
"_module": "Accounts"
}, {
"id": "48dc47dd-bbf1-816d-b0ac-5846e6dd9e21",
"name": "test with opp",
"date_modified": "2016-12-06T16:23:33+00:00",
"opportunities": {
"next_offset": -1,
"records": [{
"id": "79c3bf6f-6c2b-7945-09f7-5846e6c610d7",
"date_modified": "2016-12-06T16:24:20+00:00",
"_acl": {
"fields": {}
},
"_module": "Opportunities"
}]
},
"_acl": {
"fields": {}
},
"_module": "Accounts"
}]
}
Hope this helps.