Multi param values are not replaced properly in the path - karate

I have multiple parameters in the REST call as shown below
/integration/live/rest/accessProfile?page=0&pageSize=10&sortBy=name&fieldList=name,id,date_created,date_modified,created_id,modified_id&filter=id%20not%20equal%20to%20%27200%27%20AND%20id%20not%20equal%20to%20%27100%27%20AND%20id%20not%20equal%20to%20%27101%27%20AND%20id%20not%20equal%20to%20%27102%27%20AND%20id%20not%20equal%20to%20%27103%27%20&getTotalRecordCo
My code is
* params { page: '0', pageSize: '10',sortBy: 'name', fieldList: ['name','id', 'date_created', 'date_modified', 'created_id', 'modified_id'],filter: 'id%20not%20equal%20to%20%27200%27%20AND%20id%20not%20equal%20to%20%27100%27%20AND%20id%20not%20equal%20to%20%27101%27%20AND%20id%20not%20equal%20to%20%27102%27%20AND%20id%20not%20equal%20to%20%27103%27%20',getTotalRecordCount:true }
And path '/integration/live/rest/accessProfile'
When I am running the test cases the path is not properly replaced in the REST call
After running the actual call sent to the server is
https://vm-trunk-wmic-01.eur.ad.sag/integration/live/rest/accessProfile?page=0&pageSize=10&sortBy=name&fieldList=name&fieldList=id&fieldList=date_created&fieldList=date_modified&fieldList=created_id&fieldList=modified_id&filter=id%2520not%2520equal%2520to%2520%2527200%2527%2520AND%2520id%2520not%2520equal%2520to%2520%2527100%2527%2520AND%2520id%2520not%2520equal%2520to%2520%2527101%2527%2520AND%2520id%2520not%2520equal%2520to%2520%2527102%2527%2520AND%2520id%2520not%2520equal%2520to%2520%2527103%2527%2520&getTotalRecordCount=true
All the params are replaced properly except for 'fieldList' parameter in the path.
I am looking for correct syntax to pass my below REST call
/integration/live/rest/accessProfile?page=0&pageSize=10&sortBy=name&fieldList=name,id,date_created,date_modified,created_id,modified_id&filter=id%20not%20equal%20to%20%27200%27%20AND%20id%20not%20equal%20to%20%27100%27%20AND%20id%20not%20equal%20to%20%27101%27%20AND%20id%20not%20equal%20to%20%27102%27%20AND%20id%20not%20equal%20to%20%27103%27%20&getTotalRecordCount=true

Try:
* params { fieldList: 'name,id,date_created,date_modified,created_id,modified_id' }
EDIT: please note that commas will be URL-encoded as per the HTML spec. If you really want to "see" the commas, build the url yourself.
For an example of the 2 ways to do this, see this commit: https://github.com/intuit/karate/commit/14c6321606bb6bcb626614248f85cc8ea50c61b6

Related

Karate netty/mock schema match not working [duplicate]

I have a request that is hitting my mock server... the request is in json, but one of the values is a string of about 2,000+ characters.. I am wanting to match the request if the string value (of 2,000+ characters) contains a specific substring value...
for example:
Scenario:
pathMatches('/callService') &&
methodIs('post') && request.clientDescription contains 'blue eyes'
(request.clientDescription = string of 2,000+ characters)
It seems that it does not like the key word contains and I can't seem to find any information on the syntax I would use to search through a given string in a request and see if it contains a specific value.
I understand that I could try to match the entire string value using '==', but I am looking for a way to only match if it contains a substring.
Here's a tip, whatever you see on the right of Scenario: is pure JavaScript, and methodIs() etc. happen to be pre-defined for your convenience.
So this should work, using String.includes()
Scenario: request.clientDescription.includes('blue eyes')
Also please refer this answer for other ideas: https://stackoverflow.com/a/57463815/143475
And one more: https://stackoverflow.com/a/63708918/143475
It did not seem to like when I added "&& request.clientDescription.includes('blue eyes')" in the Scenario, but it did lead me in the right direction, and I did find a solution. thanks!
Error : after adding String.includes to Scenario:
com.intuit.karate - scenario match evaluation failed: evaluation (js) failed: pathMatches('/callService') &&
methodIs('post') && request.clientDescription.includes('blue eyes'), javax.script.ScriptException: TypeError: request.clientDescription.includes is not a function in at line number 2
stack trace: jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
A Solution:
defined a function in the background using karate.match
Code Example of Solution:
Background:
* def isBlueEyed = function(){return karate.match("request.clientDescription contains 'Blue Eyes'").pass}
Scenario:
pathMatches('/callService') &&
methodIs('post') && isBlueEyed()
* def response = read('./***/***/**')

Karate: Question mark gets encoded as part of path

I am testing a search API. I need to send different query params depending on the test case name. Code like below works, but it sends both params for all test cases.
Given path '/search'
And param merchantId = 'abc'
And param email = 'abc#gmail.com'
I want one param to be sent with one test case and another param with another test case. So I tried like below, but the question mark after "search" gets encrypted and sent to the server.
* if('<testcaseName>' == 'search by merchantId') karate.set('pathVar','/search?merchantId=' + merchantId);
* if('<testcaseName>' == 'search by email') karate.set('pathVar','/search?email=' + email);
Given path pathVar
Is there any other way to send them?
Use the params keyword: https://github.com/intuit/karate#params
Any null values will NOT be sent. See this example: dynamic-params.feature
So you can do something like this:
Given path '/search'
And params { merchantId: '#(a)', email: '#(b)' }
So depending on the values of the variables a and b you can control your test the way you want.

Can Karate generate multiple query parameters with the same name?

I need to pass multiple query parameters with the same name in a URL, but I am having problems getting it to work with Karate. In my case, the URL should look like this:
http://mytestapi.com/v1/orders?sort=order.orderNumber&sort=order.customer.name,DESC
Notice 2 query parameters named "sort". I attempted to create these query string parameters with Karate, but only the last "sort" parameter gets created in the query string. Here are the ways I tried to do this:
Given path 'v1/orders'
And param sort = 'order.orderNumber'
And param sort = 'order.customer.name,DESC'
And header Authorization = authInfo.token
And method get
Then status 200
And:
Given path 'v1/orders'
And params sort = { sort: 'order.orderNumber', sort: 'order.customer.name,DESC' }
And header Authorization = authInfo.token
And method get
Then status 200
And:
Given path 'v1/order?sort=order.orderNumber&sort=order.customer.name,DESC'
And header Authorization = authInfo.token
And method get
Then status 200
The first two ways provide the same query string result: ?sort=order.customer.name%2CDESC
The last example does not work because the ? get encoded, which was expected and explained in this post - Karate API Tests - Escaping '?' in the url in a feature file
It's clear that the second "sort" param is overriding the first and only one parameter is being added to the URL. I have gone through the Karate documentation, which is very good, but I have not found a way to add multiple parameters with the same name.
So, is there a way in Karate to set multiple URL query parameters with the same name?
Yes you can generate multiple query parameters with the same name in karate
All values of similar key should be provided in an array.
Given path 'v1/orders'
And params {"sort":["order.orderNumber","order.customer.name,DESC"]}
And header Authorization = authInfo.token
And method get
Then status 200
And for setting single parameter using param it will be like
And param sort = ["order.orderNumber","order.customer.name,DESC"]

m.request: use URL which contains colons

I have a m.request call like this:
mCmdName = "cn:cmd:list:deselectAll";
m.request({
method : "POST",
url : "/testing/cmd/" + mCmdName,
data: data
});
Now m.request calls
xhrOptions.url = parameterizeUrl(xhrOptions.url, xhrOptions.data);
and tries to replace all ':[name]' parts with data[name] which results in 'undefined' as data doesn't contain any of the keys. Data is just the data object of the XHR request.
Is there a way to prohibit this default behavior?
Thanks, Stefan
PS: I'm asking here and not in mithril mailing list because I can't post there for incomprehensible reasons. Maybe somebody can give me a hint on this.
Have you tried
encodeURIComponent("cn:cmd:list:deselectAll")
which gives you
cn%3Acmd%3Alist%3AdeselectAll
If necessary you can decode on the server.

QUnit and urlencode

I'm trying to test a utility method I have that creates urlencoded query strings. It somehow decodes "expected" in to: ?foo=foo val&bar=bar&val ... so it's decoding the urlencoding!
test("test make_params properly url encodes", function() {
var o = {"foo":'foo val',"bar":'bar&val'};
var actual = make_params(o);
equals('?foo=foo+val&bar=bar%26val', actual, "Expected urlencoded string built to be" + '?foo=foo+val&bar=bar%26val');
});
Results in:
1. Expected urlencoded string built to be?foo=foo+val&bar=bar%26val, expected:
"?foo=foo val&bar=bar&val" result: "?foo=foo+val&bar=bar%26val", diff: "?foo=foo val&bar=bar&val" "?foo=foo+val&bar=bar%26val"
Is this a bug in qunit or am I overlooking something?
One minor issue: equals expect the actual value as the first argument, the expected as the second. And equals is now deprecated in favor of equal.
Based on that its likely that the test works fine, but the make_params method doesn't actually encode anything.