How to create xPath for soap header and body - xpath

I am trying to create a stub for soap request that has header and body following is the soap request:
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns3="http://example.com/ws/Namespaces/CustomerCareProductAndInsurance/Types/Public/CommonDataModel.xsd"
xmlns:inq="http://example.wsproduct.com/ws/Namespaces/Container/Public/InquireProductDetailsRequest.xsd"
xmlns="http://example.wsproduct.com/ws/Namespaces/Types/Public/MessageHeader.xsd">
<soap:Header>
<MessageHeader>
<TrackingMessageHeader>
<version>224</version>
</TrackingMessageHeader>
</MessageHeader>
</soap:Header>
<soap:Body>
<inq:InquireProductDetailsRequest>
<inq:ProductSelector>
<inq:code>013881004138416</inq:code>
</inq:ProductSelector>
</inq:InquireProductDetailsRequest>
</soap:Body>
</soap:Envelope>
Following is the stub that I have created with xPath matcher, however it's not working and I am getting body does not match everytime.
{
"request": {
"method": "POST",
"url": "/inquireProductDetails",
"headers": {
"Content-Type": {
"contains": "text/xml"
}
},
"bodyPatterns": [
{
"matchesXPath": "//version='224'"
},{
"matchesXPath": "//code='013881004138416'"
}
]
},
"response": {
"transformers": [
"response-template"
],
"bodyFileName": "productDetails-Success-Response.xml",
"status": 200
}
}
Can someone help to create matching xPath for above soap request. Thanks in advance!!

Try this
{
"request": {
"method": "POST",
"url": "/inquireProductDetails",
"headers": {
"Content-Type": {
"contains": "text/xml"
}
},
"bodyPatterns": [
{
"matchesXPath": "//*[local-name()='code'][text()='224']"
},{
"matchesXPath": "//*[local-name()='version'][text()='013881004138416']"
}
]
},
"response": {
"transformers": [
"response-template"
],
"bodyFileName": "productDetails-Success-Response.xml",
"status": 200
}
}

Related

Wiremock request.query not working with a paramter that contains dots

I'm using sprint-boot with wiremock-jre8, what i'm trying to to is to get a value from URL params, but this value contains dots "product.productCharacteristic.value"
{
"request": {
"urlPath": "/exampleOfPath",
"method": "GET",
"queryParameters": {
"product.productCharacteristic.name": {
"equalTo": "MSISDN"
},
"product.productCharacteristic.value": {
"matches": ".*"
}
}
},
"response": {
"status": 200,
"jsonBody": {
"key": "the value should be here {{request.query['product.productCharacteristic.value']}}"
},
"transformers": [
"response-template"
],
"headers": {
"Content-Type": "application/json"
}
}
}
i have tested all of those
{{request.query.['product.productCharacteristic.value']}}
{{request.query['product.productCharacteristic.value']}}
{{request.query['product%2EproductCharacteristic%2Evalue']}}
{{request.query.['product%2EproductCharacteristic%2Evalue']}}
{{request.query.product%2EproductCharacteristic%2Evalue}}
{{request.query.product.productCharacteristic.value}}
{{lookup request.query 'product.productCharacteristic.value'}}
{{lookup request.query 'product%2EproductCharacteristic%2Evalue'}}
You can use this format:
{{request.query.[product.productCharacteristic.value]}}
See WireMock - Response Templating - The request model:
request.headers.[<key>] - Header with awkward characters e.g. request.headers.[$?blah]

Wiremock: xpath not working if xmlns is present

I am creating stub in wiremock. If I have xmlns in xml then it doesn't match however, without that It works.
Request
curl -d '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a xmlns="http://www.example.com/namespaces/ad"><b>1</b><c>2</c><d>9407339517</d></a>' -i -H "Content-Type: text/xml" -X POST "http://localhost:8080/test"
Stub Json
{
"request": {
"method": "POST",
"url": "/test",
"headers" : {
"Content-Type" : {
"equalTo" : "text/xml"
}
},
"bodyPatterns" : [ {
"matchesXPath" : "/stuff:a[b='1'][c='2']",
"xPathNamespaces" : {
"stuff" : "http://www.example.com/namespaces/ad"
}
} ]
},
"response": {
"body": "Hello world!",
"status": 200
}
}
Along with above mentioned way, I have tried with local-name() too.
When there is a namespace present on a (grand) parent then on the (grand) children inherit the same namespace. So your /b and /c should be prefixed with /stuff:b or /stuff:c
{
"request": {
"method": "POST",
"url": "/test",
"headers" : {
"Content-Type" : {
"equalTo" : "text/xml"
}
},
"bodyPatterns" : [ {
"matchesXPath" : "/stuff:a[./stuff:b='1'][./stuff:c='2']",
"xPathNamespaces" : {
"stuff" : "http://www.example.com/namespaces/ad"
}
} ]
},
"response": {
"body": "Hello world!",
"status": 200
}
}

swagger: how to validate formData

So far I'm able to do swagger validation if the parameters are from "in": "body" or if the input expected is in a json format.
However, I can't find how to validate a simple string entered as formData.
Below is my swagger script (in json format)
v1swag = {
"cancels_post": {
"tags": ["/api/v1"],
"parameters": [
{
"name": "token",
"in": "formData",
"type": "string",
"required": True,
"description": "Cancels the provided token.",
}
],
"responses": {
"200": {
"description": "Success!",
}
}
}
}
I removed the schema as it seems to only work for "in": "body"
I've been searching the net but can't seem to find the light.
Though I will still be searching... Any hints would be greatly appreciated.
Thank you very much in advance.
A different source media type has to be consumed here. Specify "consumes" member to include media type of application/x-www-form-urlencoded.
v1swag = {
"cancels_post": {
"tags": ["/api/v1"],
"consumes": [
"application/x-www-form-urlencoded"
],
"parameters": [
{
"name": "token",
"in": "formData",
"type": "string",
"required": True,
"description": "Cancels the provided token.",
}
],
"responses": {
"200": {
"description": "Success!",
}
}
}
}

Location response header returned from web-api to Swagger-ui

I am returning a Uri as the location header of the response from my web-api controller, as shown below:
[HttpPost]
public HttpResponseMessage PostTenant([FromBody] JObject value)
{
string tenantName;
try
{
tenantName = value.GetValue("name").ToString();
}
catch (Exception e)
{
throw new HttpResponseException(
this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
}
try
{
DbInteract.InsertTenant(tenantName.Replace(" ", string.Empty));
var uri = Url.Link("TenantHttpRoute2", new { tenantName });
var response = new HttpResponseMessage(HttpStatusCode.Created);
response.Headers.Location = new Uri(uri);
return response;
}
catch...
}
The swagger-ui client makes the POST request, but the response headers don't contain the Location uri. It appears like this:
POST request from Swagger-ui
As you can see in the image, the RESPONSE HEADERS are
{
"content-type": null
}
The swagger JSON for this post request is:
"post": {
"tags": [
"Tenant"
],
"summary": "Add a new tenant",
"description": "Adds a tenant and returns admin user account information to be used in further calls.",
"consumes": [
"application/json",
"application/xml"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "body",
"name": "Tenant",
"description": "Name of the tenant must be alphanumeric without any special characters.",
"required": true,
"schema": {
"$ref": "#/definitions/CreateTenant"
}
}
],
"responses": {
"201": {
"description": "Tenant Inserted",
"headers": [
{
"description": "Location",
"type": "string"
}
]
},
"400": {
"description": "Invalid JSON Format of input"
},
"405": {
"description": "Tenant by this name already exists; Use PUT instead"
},
"500": {
"description": "InternalServerError"
}
}
}
What's wrong here? Why can't I see the response header in swagger-ui? Please let me know if you need more information. Thanks
The intent seems clear enough: a successful response returns a 201 Created with a Location header pointing at the newly created resource. Should work ...
I might remove the "produces" attribute, since you haven't defined any response anywhere.
Try with:
"headers" : { "Location" : { "description": "...", "type" : "string" } }
Instead of:
"headers" : []
so object instead of arrays.

Can't get merge tags to work - Mandrill/Parse

Here's my Parse Cloud Code call:
Mandrill.sendTemplate({
"template_name": "start-conversation",
"template_content": [{
"name": "example name",
"content": "example content" //Those are required but they are ignored
}],
"message": {
"to": [{
"email": request.params.toUserEmail,
"name": request.params.toUserName
}],
"important": true,
"merge": true,
"global_merge_vars": [
{
"rcpt": request.params.toUserEmail,
"vars": [
{
"name": "TOUSERNAME",
"content": request.params.toUserName
},
{
"name": "FROMUSERNAME",
"content": request.params.fromUserName
},
{
"name": "TOPICNAME",
"content": request.params.topicName
},
{
"name": "LANGUAGE",
"content": request.params.language
}
]
}
],
},
"async": true
},{
success: function(httpResponse) {
console.log(httpResponse);
response.success("mandrillStartConvoRequest -- success -- Email sent!");
},
error: function(httpResponse) {
console.error(httpResponse);
response.error("mandrillStartConvoRequest -- error -- Uh oh, something went wrong");
}
});
Here's the <span> with the tags in my Mandrill Template:
<span style="line-height:20.7999992370605px">
*|TOUSERNAME|*
<br><br>
*|FROMUSERNAME|* would like to start a conversation with you about *|TOPICNAME|* in *|LANGUAGE|* </span>
The e-mail sends fine but no merge =(:
as far as I know the built-in Mandrill.sendTemplate method does not work.
so you should try to call mandrill API yourself, just do a HTTP POST
https://parse.com/questions/send-mandrill-template-email-from-cloud-code-example-code-required
UPDATE:
what I am using in my project is something like this, notice that I am using merge_vars but you are using global_merge_vars
var params = {
key: "xxxxxxxxxxxx",
template_name: "$template_name",
template_content: [],
message: {
to: [
{
email: email
}
],
merge_vars : [{
rcpt: email,
vars:[
{
"name" : "from",
"content" : "Test"
}
]
}]
},
async: true
};
Parse.Cloud.httpRequest({
method: "POST",
headers: {
"Content-Type": "application/json",
},
url: "https://mandrillapp.com/api/1.0/messages/send-template.json",
body: params,
success: function(httpResponse) {
response.success("email sent");
},
error: function(httpResponse) {
console.error(httpResponse);
response.error("Uh oh, something went wrong");
}
});

Resources