How to provide parameter along with empty square brackets as param in GET method? - karate

I have faced a problem with providing param into GET method.
I have the next URL
https://test.com/api/v1/account/?include[]=tickers&include[]=holdings&include[]=views
How to provide parameter along with empty square brackets as param in GET method in my example?
I saw https://github.com/karatelabs/karate/blob/master/karate-demo/src/test/java/demo/search/dynamic-params.feature , but didnt find my solution(

If you see Karate encoding it as %5B%5D that is actually the correct behavior: https://stackoverflow.com/a/59977660/143475
You can actually try this on httpbin.org and see for yourself that the server is able to decode it correctly. If your server cannot, it is most likely a bug.
* url 'https://httpbin.org/anything'
* param include[] = 'tickers'
* method get
See server response:
1 > GET https://httpbin.org/anything?include%5B%5D=tickers
1 > Host: httpbin.org
1 > Connection: Keep-Alive
1 > User-Agent: Apache-HttpClient/4.5.14 (Java/17.0.6)
1 > Accept-Encoding: gzip,deflate
17:17:10.925 [main] DEBUG com.intuit.karate - response time in milliseconds: 1528
1 < 200
1 < Date: Tue, 07 Feb 2023 11:47:10 GMT
1 < Content-Type: application/json
1 < Content-Length: 436
1 < Connection: keep-alive
1 < Server: gunicorn/19.9.0
1 < Access-Control-Allow-Origin: *
1 < Access-Control-Allow-Credentials: true
{
"args": {
"include[]": "tickers"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.14 (Java/17.0.6)",
"X-Amzn-Trace-Id": "Root=1-63e23a3e-4004ea8e65dba95003ec150e"
},
"json": null,
"method": "GET",
"url": "https://httpbin.org/anything?include[]=tickers"
}
That said you can move everything to the URL:
* url `https://httpbin.org/anything?include[]=tickers`
* method get
And if you still need to parameterize things, you can, see: https://stackoverflow.com/a/75061809/143475

Related

Karate api test is converting # to %40 while making a post call

I have written an api test using karate framework. While doing a post call it's converting the value of email field from TEST#ABC.COM to TEST%40ABC.COM.
Here are few lines from log
1 > Content-Type: application/x-www-form-urlencoded; charset=UTF-8
1 > User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_221)
email=TEST%40ABC.COM&checkDigit=&brandName=
This is correct as per the HTTP spec: https://stackoverflow.com/a/53638335/143475
You can see for yourself, try this:
* url 'http://httpbin.org'
* path 'anything'
* form field username = 'john#smith.com'
* form field password = 'secret'
* method post
Result:
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Content-Length: 41
1 > Content-Type: application/x-www-form-urlencoded; charset=UTF-8
1 > Host: httpbin.org
1 > User-Agent: Apache-HttpClient/4.5.12 (Java/1.8.0_231)
username=john%40smith.com&password=secret
But when you see the response, you can see the server handled it correctly, look at the form in the JSON:
1 < 200
1 < Access-Control-Allow-Credentials: true
1 < Access-Control-Allow-Origin: *
1 < Connection: keep-alive
1 < Content-Length: 555
1 < Content-Type: application/json
1 < Date: Tue, 07 Apr 2020 13:11:58 GMT
1 < Server: gunicorn/19.9.0
{
"args": {},
"data": "",
"files": {},
"form": {
"password": "secret",
"username": "john#smith.com"
},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Content-Length": "41",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.12 (Java/1.8.0_231)",
"X-Amzn-Trace-Id": "Root=1-5e8c7c1e-affe121cae9f4b20399b0884"
},
"json": null,
"method": "POST",
"origin": "49.206.10.30",
"url": "http://httpbin.org/anything"
}

Unable to specify round brackets for Odata URL in Karate

I'm trying to create the path for the Odata URL in Karate.
The path looks like: '/opu/odata/srt/ZQ_SRV/ZQ_BI_Q001(OPT_1='0013076036',OPT_To='0013076036')/Results'
It seems like Karate can't read special characters like round brackets () and ' '. And it cuts the url after opu/odata/srt/ZQ_SRV/ZQ_BI_Q001 right before the round brackets starts. And the rest of the url (OPT_1='0013076036',OPT_To='0013076036')/Results looks like a text.
I've tried to use %28 for ( and 29% for ) and %27 for ' but it didn't help.
P.S.When running the same url in Postman the call went successfully
Running the test url:
Background:
* url "https://httpbin.org/anything/opu/odata/srt/ZQ_SRV/ZQ_BI_Q001(OPT_1='0013076036',OPT_To='0013076036')/Results"
Scenario: test check
* method get
Try building the url fully by hand and don't use param or path:
* url "http://myhost/opu/odata/srt/ZQ_SRV/ZQ_BI_Q001(OPT_1='0013076036',OPT_To='0013076036')/Results"
If that still doesn't work, it is likely your server does not handle encoded URL-s correctly which could be a bug: https://stackoverflow.com/a/54477346/143475
EDIT: just try these 2 lines to prove there is nothing wrong with Karate / or see this simpler example: https://stackoverflow.com/a/67068873/143475
* url "https://httpbin.org/anything/opu/odata/srt/ZQ_SRV/ZQ_BI_Q001(OPT_1='0013076036',OPT_To='0013076036')/Results"
* method get
This is the result:
Running com.intuit.karate.junit4.dev.TestRunner
23:11:06.404 [main] DEBUG com.intuit.karate - request:
1 > GET https://httpbin.org/anything/opu/odata/srt/ZQ_SRV/ZQ_BI_Q001(OPT_1='0013076036',OPT_To='0013076036')/Results
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Host: httpbin.org
1 > User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_231)
23:11:08.154 [main] DEBUG com.intuit.karate - response time in milliseconds: 1745.46
1 < 200
1 < Access-Control-Allow-Credentials: true
1 < Access-Control-Allow-Origin: *
1 < Connection: keep-alive
1 < Content-Type: application/json
1 < Date: Wed, 22 Jan 2020 17:41:07 GMT
1 < Referrer-Policy: no-referrer-when-downgrade
1 < Server: nginx
1 < X-Content-Type-Options: nosniff
1 < X-Frame-Options: DENY
1 < X-XSS-Protection: 1; mode=block
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.5 (Java/1.8.0_231)"
},
"json": null,
"method": "GET",
"origin": "49.206.14.183, 49.206.14.183",
"url": "https://httpbin.org/anything/opu/odata/srt/ZQ_SRV/ZQ_BI_Q001(OPT_1='0013076036',OPT_To='0013076036')/Results"
}
Resolved issue by putting * method get instead of When method GET
Then status 200

Calling https://graph.microsoft.com/v1.0/users/<ID Calendar>/calendar/calendarView? doesn't return appointments, and there is appointments

The call to Graph API is not working properly since yesterday :
https://graph.microsoft.com/v1.0/users/<my calendar ID>/calendar/calendarView?startDateTime=2019-10-04T19:10:24.0000000&endDateTime=2019-11-03T19:10:24.0000000&top=100
Is returning 0 events - appointments, and there are appointments to list on
That call was working ok until yesterday, every 8 minutes, 24x7 since last month :
// List events.
string fini = string.Format("{0}-{1}-{2}T{3}:{4}:{5}.0000000",
UpDateTime.Year.ToString("0000"),
UpDateTime.Month.ToString("00"),
UpDateTime.Day.ToString("00"),
UpDateTime.Hour.ToString("00"),
UpDateTime.Minute.ToString("00"),
UpDateTime.Second.ToString("00")
);
string ffin = string.Format("{0}-{1}-{2}T{3}:{4}:{5}.0000000", //
EndTime.Year.ToString("0000"),
EndTime.Month.ToString("00"),
EndTime.Day.ToString("00"),
EndTime.Hour.ToString("00"),
EndTime.Minute.ToString("00"),
EndTime.Second.ToString("00")
);
String _requestURI = _GraphURLEvents.Replace("events", "calendarView") + string.Format("?startDateTime={0}&endDateTime={1}&top=100", fini, ffin);
_requestURI = String.Format(_requestURI, _userId);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, _requestURI);
//Authentication token
request.Headers.Add("Prefer", "outlook.timezone=\"Romance Standard Time\"");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", System.Web.HttpUtility.UrlEncode(access_token));
HttpClient client = new HttpClient();
var response = await client.SendAsync(request)
var returnURL = await
response.Content.ReadAsStringAsync();
Get kind CALL returns:
{StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Transfer-Encoding: chunked
request-id: <r>
client-request-id: <t>
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"West Europe","Slice":"SliceC","Ring":"5","ScaleUnit":"003","RoleInstance":"AGSFE_IN_52","ADSiteName":"WEU"}}
Preference-Applied: outlook.timezone="Romance Standard Time"
OData-Version: 4.0
Duration: 66.8258
Strict-Transport-Security: max-age=31536000
Cache-Control: private
Date: Fri, 04 Oct 2019 17:11:31 GMT
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true; IEEE754Compatible=false; charset=utf-8
}}
Content: {System.Net.Http.StreamContent}
Headers: {Transfer-Encoding: chunked
request-id: <x>
client-request-id: <y>
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"West Europe","Slice":"SliceC","Ring":"5","ScaleUnit":"003","RoleInstance":"AGSFE_IN_52","ADSiteName":"WEU"}}
Preference-Applied: outlook.timezone="Romance Standard Time"
OData-Version: 4.0
Duration: 66.8258
Strict-Transport-Security: max-age=31536000
Cache-Control: private
Date: Fri, 04 Oct 2019 17:11:31 GMT
}
IsSuccessStatusCode: true
ReasonPhrase: "OK"
RequestMessage: {Method: GET, RequestUri: 'https://graph.microsoft.com/v1.0/users/<mycalendar>/calendar/calendarView?startDateTime=2019-10-04T19:10:24.0000000&endDateTime=2019-11-03T19:10:24.0000000&top=100', Version: 1.1, Content: <null>, Headers:
{
Prefer: outlook.timezone="Romance Standard Time"
Authorization: Bearer <my authorization bearer>
}}
StatusCode: OK
Version: {1.1}
That call must return something like :
https://learn.microsoft.com/en-us/graph/api/user-list-calendarview?view=graph-rest-1.0&tabs=http
HTTP/1.1 200 OK
Content-type: application/json
Content-length: 354
{
"value": [
{
"originalStartTimeZone": "originalStartTimeZone-value",
"originalEndTimeZone": "originalEndTimeZone-value",
"responseStatus": {
"response": "",
"time": "datetime-value"
},
"iCalUId": "iCalUId-value",
"reminderMinutesBeforeStart": 99,
"isReminderOn": true
}
]
}
... and since yesterday the result is :
{\"#odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#users('<calendarId>')/calendar/calendarView\",\"value\":[]}
Based on my test the url should be:
1. To get the calendar of a specific user
https://graph.microsoft.com/v1.0/users/<user_id>/calendar/calendarView?startDateTime=2019-10-06T16:00:00Z&endDateTime=2019-10-10T16:00:00Z&top=100
or
https://graph.microsoft.com/v1.0/users/<user_id>/calendars/<calendar_id>/calendarView?startDateTime=2019-10-06T16:00:00Z&endDateTime=2019-10-10T16:00:00Z&top=100
2. To get the calendar of me
https://graph.microsoft.com/v1.0/me/calendar/calendarView?startDateTime=2019-10-06T16:00:00Z&endDateTime=2019-10-10T16:00:00Z&top=100
or
https://graph.microsoft.com/v1.0/me/calendars/<calendar_id>/calendarView?startDateTime=2019-10-06T16:00:00Z&endDateTime=2019-10-10T16:00:00Z&top=100
I can get successful responses from the above urls.
By the way, please check if you have set the correct time period. The timestamp should be UTC formatted.

Hide passwords in logs

The following code :
Background:
* url "https://www.google.fr"
Scenario: hide password
Given path "login"
And form field username = 'john'
And form field password = 'secret'
When method post
Then status 200
Gives the following output :
10:38:34.710 request:
1 > POST https://www.google.fr/login
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Content-Length: 29
1 > Content-Type: application/x-www-form-urlencoded; charset=UTF-8
1 > Host: www.google.fr
1 > User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_191)
username=john&password=secret
Is there a way to hide the password here, while keeping the logs at the DEBUG level? If that means also hiding the username, that's not a problem.
This may need to be a feature request if * configure report = { showLog: false } does not do it already.

Restler, writing test in behat for POST-Method

I want to write a TestScenario in Behat for a POST Method that takes an Object as input.
Feature: Testing The ChildController Endpoint
Scenario: Trying to create an Child by POSTing vars with an existing Id
Given that I want to make a new "Child"
And the request is sent as JSON
And its "screening_id" is "999888777666"
And his "first_name" is "John"
And his "last_name" is "Doe"
And his "birth_date" is "1979-01-01"
And his "gender" is "m"
When I request "/v1/child"
Then the response status code should be 409
And the response should be JSON
And the response has a "error" property
My Method is like:
/**
* Child Endpoint v1
*
* #url POST child/
* #status 201
*
* #param Child $child JSON data
* #return application/json
* #throws RestException
*/
function insertChild(Child $child){......}
My JSON Object that I pass looks like this:
{
"child": {
"screening_id": "",
"first_name": "",
"last_name": "",
"birth_date": "",
"gender": ""
}
}
Now... when I make a request with Postman with only:
{
"screening_id": "",
"first_name": "",
"last_name": "",
"birth_date": "",
"gender": ""
}
it works fine.
But when I'm running the Behat Test, it says that I specified an invalid value for 'child' and I get:
"error": {
"code": 400,
"message": "Bad Request: Invalid value specified for 'child'. Expecting an item of type `v1\\models\\Child`"
}
It works from everywhere without specifying the 'child' only from BeHat not.
Solution is to change the order of things in your behat feature file
It works this way because of the way RestContext.php bootstrap file is defined.
Currently what you have and the test result
Feature: Testing The ChildController Endpoint
Scenario: Trying to create an Child by POSTing vars with an existing Id # features/test.feature:3
Given that I want to make a new "Child" # RestContext::thatIWantToMakeANew()
And the request is sent as JSON # RestContext::theRequestIsSentAsJson()
And its "screening_id" is "999888777666" # RestContext::thatItsStringPropertyIs()
And his "first_name" is "John" # RestContext::thatItsStringPropertyIs()
And his "last_name" is "Doe" # RestContext::thatItsStringPropertyIs()
And his "birth_date" is "1979-01-01" # RestContext::thatItsStringPropertyIs()
And his "gender" is "m" # RestContext::thatItsStringPropertyIs()
When I request "/v1/child" # RestContext::iRequest()
| POST /behat_test/v1/child HTTP/1.1
| Host: localhost
| User-Agent: Guzzle/3.1.2 curl/7.43.0 PHP/5.6.15
| Content-Type: application/json; charset=utf-8
| Content-Length: 2
|
| []
| HTTP/1.1 400 Bad Request
| Date: Sun, 20 Dec 2015 04:21:56 GMT
| Server: Apache/2.4.16 (Unix) PHP/5.5.30
| X-Powered-By: Luracast Restler v3.0.0rc5
| Vary: Accept
| Cache-Control: no-cache, must-revalidate
| Expires: 0
| Content-Language: en
| Content-Length: 468
| Connection: close
| Content-Type: application/json; charset=utf-8
|
| {
| "error": {
| "code": 400,
| "message": "Bad Request: Invalid value specified for `child`. Expecting an item of type `Child`"
| },
If you look closely the request is sending only an empty array
| POST /behat_test/v1/child HTTP/1.1
| Host: localhost
| User-Agent: Guzzle/3.1.2 curl/7.43.0 PHP/5.6.15
| Content-Type: application/json; charset=utf-8
| Content-Length: 2
|
| []
Fix is to move And the request is sent as JSON below all property definitions. See below
Feature: Testing The ChildController Endpoint
Scenario: Trying to create an Child by POSTing vars with an existing Id # features/test.feature:3
Given that I want to make a new "Child" # RestContext::thatIWantToMakeANew()
And its "screening_id" is "999888777666" # RestContext::thatItsStringPropertyIs()
And his "first_name" is "John" # RestContext::thatItsStringPropertyIs()
And his "last_name" is "Doe" # RestContext::thatItsStringPropertyIs()
And his "birth_date" is "1979-01-01" # RestContext::thatItsStringPropertyIs()
And his "gender" is "m" # RestContext::thatItsStringPropertyIs()
And the request is sent as JSON # RestContext::theRequestIsSentAsJson()
When I request "/v1/child" # RestContext::iRequest()
| POST /behat_test/v1/child HTTP/1.1
| Host: localhost
| User-Agent: Guzzle/3.1.2 curl/7.43.0 PHP/5.6.15
| Content-Type: application/json; charset=utf-8
| Content-Length: 108
|
| {"screening_id":"999888777666","first_name":"John","last_name":"Doe","birth_date":"1979-01-01","gender":"m"}