I am new to Wiremock and am having problem handling a URL that can have one or more path parameters.
For example, I have this URL:
/product/{productname};weight={weight};size={size};isdigital={isdigital};digitalformat={digitalformat};details={details}
Product name is required, but the rest are optional. If isdigital is true, there won't be value for weight and size, etc. The optional values don't have to be in this order either.
I created the following and it's obviously not working. Maybe urlPathTemplate is the wrong thing to use.
{
"request" : {
"method" : "GET",
"urlPathTemplate" : "/product/{productname};weight={weight};size={size};isdigital={isdigital};digitalformat={digitalformat};details={details}",
"pathParameters" : {
"productname" : {
"equalTo" : "First Blood"
},
"isdigital" : {
"equalTo" : "yes"
},
"digitalformat" : {
"equalTo": "dvd"
}
}
},
"response" : {
"status" : 200,
"headers": {
"Content-Type" : "application/json"
},
"bodyFileName": "product-first-blood-digital-dvd.json"
}
}
What's worse is that {details} is an encoded JSON string that contains description, genre, etc. and the values are optional as well. The followings are some of the sample URLs that'll be used and the above won't work for any of them:
/product/first-blood;isdigital=true;format=dvd
/product/first-blood;format=dvd;isdigital=true
/product/first-blood;details=%7B%22description%22%3A%22A%20movie%22%2C%22genre%22%3A%22Action%22%7D;format=dvd;isdigital=true
/product/first-blood;isdigital=false;size=2;weight=1
I have no idea why the URL was created this way, unfortunately I cannot replace path parameters with query parameters. Is there a way I can create a mapping file for a URL like this?
To my knowledge we don't currently have a great answer for this. From looking at the URI Template RFC it looks like you should be able to do a WireMock url template like the following:
/product/{productname}{;isdigital,format,details,weight,size}
WireMock will allow this in the urlPathTemplate
field, but it won't give you much because it doesn't look like WireMock supports any of the matchers with that style of URL template (pathParameters
for example). This is something we should address and I will raise a ticket to take a look at it.
At the moment the only thing I can suggest is to use regex in your body to pull out the values you are looking but I don't know whether that will allow you to achieve what you want with your mocking and it might mean your response templates will get a little long.
For example, the following stub will match your urls and the regex give examples of how to pull those values out:
{
"request": {
"method": "GET",
"urlPathTemplate": "/product/{productname}"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"transformers": [
"response-template"
],
"jsonBody": {
"product": "{{regexExtract request.url '/product/([a-z\\-]+);' 'product' default=''}}{{product}}",
"isdigital": "{{regexExtract request.url 'isdigital=(true|false)' 'isdigital' default=''}}{{isdigital}}",
"format": "{{regexExtract request.url 'format=([^\\;]+)' 'format' default=''}}{{format}}",
"details": "{{regexExtract request.url 'details=([^\\;]+)' 'details' default=''}}{{details}}",
"weight": "{{regexExtract request.url 'weight=([^\\;]+)' 'weight' default=''}}{{weight}}",
"size": "{{regexExtract request.url 'size=([^\\;]+)' 'size' default=''}}{{size}}"
}
}
}