gatling checking if soap response contains node - gatling

I use Gatling to test against an API responding with this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<sam:HentAktiveSamtykkerMedPartrefResponse xmlns:sam="urn:gjensidige:forsikring:samtykke">
<sam:samtykke>
<sam:idNr>01018448285</sam:idNr>
<sam:partref>0005009147</sam:partref>
<sam:utfortAv></sam:utfortAv>
<sam:navn></sam:navn>
<sam:navnUtvidet></sam:navnUtvidet>
<sam:svar>J</sam:svar>
How can I script that the response must contain the element:
<sam:svar>
?
I guess something in the lines of:
.check(xpath("whathere").exists)
But what should be stated as "whathere"?

The xpath would look something like : /*/SOAP-ENV:Body/descendant::*[name()='sam:navn']/text() this should extract the value from <sam:navn></sam:navn>.

Related

Karate: combine data driven with request in external file? [duplicate]

This question already has an answer here:
Karate: Using data-driven embedded template approach for API testing
(1 answer)
Closed 1 year ago.
I am looking for a way to use data driven in combination with an external request file.
So my feature file looks like this:
Feature: EPOS UNIT test - GetSpendingLimit
Background:
* url 'http://xxx-yyy-zzz'
* def GetSpendingLimit_request = read('classpath:examples/EPOS/request/GetSpendingLimit-dd-request.xml')
* def GetSpendingLimit_data = read('classpath:examples/EPOS/data/GetSpendingLimit.csv')
Scenario Outline: GetSpendingLimit External Request Datadriven
Given request GetSpendingLimit_request
When soap action 'TotalAmount'
Then status 200
# define a variable to check the response
* def total_amount = /Envelope/Body/GetSpendingLimitResponse/spendingLimit/totalAmount
# to print the result to the report
* print '\nTotal Amount is: ', total_amount
Examples:
|read('classpath:examples/EPOS/data/GetSpendingLimit.csv')|
And my request looks like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://xxx-yyy-zzz/GetSpendingLimit/Request" xmlns:com="http://xxx-yyy-zzz/Common" xmlns:ser="http://xxx-yyy-zzz/Common/ServiceContext">
<soapenv:Header/>
<soapenv:Body>
<req:GetSpendingLimitRequest>
<ser:productServiceContext>
<ser:conversationId>GetSpendingLimit_1_1</ser:conversationId>
<ser:deviceTypeId>1</ser:deviceTypeId>
<ser:entityId>nl</ser:entityId>
<ser:product>
<ser:id><product-id></ser:id>
</ser:product>
<ser:user>
<ser:id><user-id></ser:id>
</ser:user>
</ser:productServiceContext>
</req:GetSpendingLimitRequest>
</soapenv:Body>
</soapenv:Envelope>
Notice that I have added product-id and user-id as variable which should be replaced by data driven input from the csv. But I get the following error:
[Fatal Error] :10:43: The element type "product-id" must be terminated by the matching end-tag "</product-id>".
11:35:18.404 [main] ERROR com.intuit.karate - src/test/java/examples/EPOS/GetSpendingLimit-external-request-datadriven.feature:5
* def GetSpendingLimit_request = read('classpath:examples/EPOS/request/GetSpendingLimit-dd-request.xml')
js failed:
>>>>
01: read('classpath:examples/EPOS/request/GetSpendingLimit-dd-request.xml')
<<<<
I have tried the request in the feature file and this works fine. But the request in external file fails.
The error is clearly saying the XML is not well-formed. Use some XML tool and fix it, for e.g. something like this: https://www.freeformatter.com/xml-formatter.html
For example this should be:
<ser:id><product-id/></ser:id>

PrettyPrint feature does not work in Apache Camel

I have been trying to leverage the PrettyPrint feature to display the result of my API that is using Apache Camel. Here is the context. I have this route in my code
// Route Definition for processing Health check request
from("direct:processHealthCheckRequest")
.routeId("health")
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
.setBody(constant(healthCheckResponse));
When I'm using Postman to test my API, the display is in pretty mode even though it is not set to true, like so
{
"status": "UP"
}
Now when I'm using the following code to set the PrettyPrint to false, I'm still getting the same result. It looks like the PrettyPrint feature is not working as it is supposed to
// Route Definition for processing Health check request
from("direct:processHealthCheckRequest")
.routeId("health")
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
.setBody(constant(healthCheckResponse))
.unmarshal()
.json(JsonLibrary.Jackson, HealthCheckResponse.class, false);
I'm expecting the result to be displayed on one line like here without changing the type from JSON to string.
{"status": "UP"}
Could someone please advice on this?
I've bumped into the same issue always when manually setting the HTTP_RESPONSE_CODE header. I don't know why it technically happens - without it the HTTP response always returns proper JSON for me.
Setting CONTENT_TYPE header to application/json has solved it:
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
The solution that finally worked was to set the following in my application.properties file.
camel.rest.data-format-property.prettyPrint=false
or not to provide that property at all.
Try this:
<removeHeaders id="removeHeaders_http*" pattern="CamelHttp*"/>
<setHeader headerName="Content-type" id="content_setHeader">
<constant>application/x-www-form-urlencoded</constant>
</setHeader>
Same with Java DSL:
.removeHeaders("CamelHttp*")
.setHeader("Content-type", constant("application/x-www-form-urlencoded"))

WSO2 ESB File connector 2 search returns empty response

I use file connector 2 provided by WSO2 ESB (v 5.0.0) to search for a file in the given directory. isFileExist function returns true but when I search for the same file, it returns empty response. Is this a bug or am I missing something?
This is my code
<fileconnector.isFileExist>
<source>file:///home/test/abc.OUT</source>
</fileconnector.isFileExist>
<log level="full"/>
<fileconnector.search>
<source>file:///home/test/</source>
<filePattern>abc.OUT</filePattern>
<recursiveSearch>false</recursiveSearch>
</fileconnector.search>
<log level="full"/>
This is the response I get
To: , WSAction: mediate, SOAPAction: mediate, MessageID: urn:uuid:2391811e-5c83-4b98-a801-a60fe55b6fd0, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><fileExist>true</fileExist></soapenv:Body></soapenv:Envelope> {org.apache.synapse.mediators.builtin.LogMediator}
To: , WSAction: mediate, SOAPAction: mediate, MessageID: urn:uuid:2391811e-5c83-4b98-a801-a60fe55b6fd0, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:result xmlns:ns="http://org.wso2.esbconnectors.FileConnector"/></soapenv:Body></soapenv:Envelope> {org.apache.synapse.mediators.builtin.LogMediator}
I get the same result when I use .*\.txt file pattern in search too.
Please help, TIA
Can you try "abc.out" for file pattern in the search operation?
<filePattern>abc.out</filePattern>

apache camel spring dsl check if body contains string

I am trying to check :
<simple>${body} contains 'verification'</simple>
Body is the json:
{"verification": {"email": "bb#wp.pl", "code": "1234"}}
But this condition doesn't work. I've tried as well:
<simple>${body} contains 'verification'</simple>
<simple>${bodyAs(String)} contains 'verification'</simple>
<simple>${body.verification} != null</simple>
Could you please suggest me something?
I guess the body is maybe not a String, then try with
<simple>${bodyAs(String)} contains 'verification'</simple>
And btw what version of Camel do you use?
Actually this case:
<simple>${bodyAs(String)} contains 'verification'</simple>
didn't work cause:
In Camel the message body can be of any types. Some types are safely readable multiple times, and therefore do not 'suffer' from becoming 'empty'.
It fixes by Stream caching

How to get right attribute which contain "&" through libxml2

When I try to get the attribute of URL in a test XML:
<Test> <Item URL="http://127.0.0.1?a=1&b=2"/>
</Test>
After I call: attr=xmlGetProp(cur, BAD_CAST "URL");
The libxml2 give a message: Entity: line 1: parser error : EntityRef: expecting ';'
and return value of attr is "http://127.0.0.1?a=1=2"
How can I get the completion attribution of URL? Thanks
You cannot get the “correct” URL here because the XML file is not well-formed. the & should have been written as &. You have to ask the creator of the XML file to create a syntactically valid, well-formed XML file.
XML is not created by just putting strings together, they also have to be encoded properly.

Resources