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

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"
}

Related

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

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

HTTP range requests are not cached by Firefox nor Chrome

This range request seems to never be cached neither by Firefox 102.0.1 nor Chrome 103.0.5060.53:
await fetch("https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/2020/S2A_36QWD_20200701_0_L2A/TCI.tif", {
"credentials": "omit",
"headers": {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Range": "bytes=0-5",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site"
},
"referrer": "http://localhost:8080/",
"method": "GET",
"mode": "cors"
});
Returns (in Firefox):
HTTP/1.1 206 Partial Content
x-amz-id-2: jGkETlSznrL2heZz48jtyNuZelyqgCBJFDGgGMlzkWeQ3Eyha16y8bs1A4kJbcyXCOpPtmGmFis=
x-amz-request-id: B0NZTXZGYV6D6YPT
Date: Tue, 12 Jul 2022 09:49:45 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: HEAD, GET
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
Last-Modified: Wed, 01 Jul 2020 15:33:02 GMT
ETag: "1cfd50c6d5047f6e76a2c8b848e2866c-28"
Cache-Control: public, max-age=31536000, immutable
Accept-Ranges: bytes
Content-Range: bytes 0-5/234841786
Content-Type: image/tiff; application=geotiff; profile=cloud-optimized
Server: AmazonS3
Content-Length: 6
Is there something wrong with the request ?

Karate multi part file upload getting error from Mulesoft

Mulesoft is rejected a file upload saying:
"Unable to get name from form-data"
According to this link: MuleSoft Support they suggest naming convention of
following: Content-Disposition, instead of what is being sent as content-disposition.
Feature:
Given url 'https:someurlpath'
And header Authorization = 'Basic ' + 'feefff'
And header Content-Type = 'multipart/form-data'
And header Accept-Encoding = 'gzip, deflate, br'
And multipart field callingApplicationName = 'Karate'
And multipart file binaryContent = { read: 'response.tif', filename: 'response.tif', contentType: 'image/tiff' }
And multipart field properties = '[{"displayName":"SR Number","value":["1234","12345"]},{"displayName":"Document Type","value":"CLAI"},{"displayName":"Ingestion Source","value":"FILENETTOOLS"},{"displayName":"Received Date","value":"2022-01-14T00:52:50.837650Z"},{"displayName":"Pages","value":"0"}]'
When method Post
Then status 200
And print response
And match response.status == 'ACTIVE'
Here is the request:
1 > POST https://api.maha.com/documents-process-api/v1/documentC/specialR/documents/
1 > Authorization: Basic foooooooobooooooo
1 > Content-Type: multipart/form-data; boundary=231ef49a739286f9
1 > Accept-Encoding: gzip, deflate, br
1 > Content-Length: 214420
1 > Host: api.maha.com
1 > Connection: Keep-Alive
1 > User-Agent: Apache-HttpClient/4.5.13 (Java/14.0.2)
content-disposition: form-data; name="callingApplicationName"; filename=""
content-type: text/plain; charset=UTF-8
content-length: 6
Completed: true
IsInMemory: true
Mixed: content-disposition: form-data; name="binaryContent"; filename="response.tif"
content-type: image/tiff; charset=UTF-8
content-length: 213642
Completed: true
IsInMemory: false
RealFile: /Users/req/workspace/hlx/sasquatch-automated-tests/src/test/groovy/com/maha/gpa/document/storage/service/response.tif DefaultDeleteAfter: true
content-disposition: form-data; name="properties"; filename=""
content-type: text/plain; charset=UTF-8
content-length: 278
Completed: true
IsInMemory: true
Yes this is a known issue with a workaround, refer this thread please: https://github.com/karatelabs/karate/issues/1647
Meanwhile you are most welcome to contribute code to Karate to fix this :)

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

Content-Type = 'application/x-www-form-urlencoded' in request is changed to Content-Type: application/json; in Karate version 0.9.2

When i send the Content-Type = 'application/x-www-form-urlencoded' as header it is changed to Content-Type: application/json; in Karate version 0.9.2 .
In Karate version 0.7.0 it is sent as Content-Type = 'application/x-www-form-urlencoded' only .
Given url "https://foo.bar.buzz"
And header Content-Type = 'application/x-www-form-urlencoded'
And form field username = "check"
And form field password = "Password"
And request {}
And header Content-Type = 'application/json'
When method POST
Then status 200
In the 3rd last line you have the header set ?
And you should never use a request body for a form submit.
Try the below, it works fine:
Given url 'https://postman-echo.com/post'
And form field foo = 'bar'
When method post
Result:
1 > POST https://postman-echo.com/post
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Content-Length: 7
1 > Content-Type: application/x-www-form-urlencoded; charset=UTF-8
1 > Host: postman-echo.com
1 > User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_112)
foo=bar