Print keyword displays blank when response includes long encoded chars - karate

I am testing an endpoint that takes a json body as request and return a json object with one field with encoded value for a pdf generation. I get 201 status code, but I could not get to print the response. It looks blank in console.
Sample Request Paylaod might be;
{'name': 'firstName',
'lname': lastname }
and response looks like below;
{ 'getPDF': 'JBKJGHUKSxjsnckdskcnejcnd/vdsv+VFDVfsd..........'}
And I just print the response as below;
* status 201
* print response
but it does not print anything. Is there a way to print the encoded value in the response?
Thanks.

It sounds to me that the response is actually empty. Anyway if it is bytes, you can try convert to a string:
* string response = responseBytes
If that doesn't work - who knows, Karate may have a bug :) So please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Related

Body missing sending array in a request for karate testing with Yaml

I have started working with karate and yml. I have been validating simple requests and responses. right now I'm trying to validate a response but I'm always receiving a 500 error due to "Required request body is missing".
I have been doing the same request with postman and it works correctly, it is also pointing at the correct URL so I think my problem lies in my yml formatting.
The Yml with the structure validation looks like this:
operationId: name
statusCode: 200
params: null
body:
ids: [2, 4]
matchResponse: true
responseMatches: '##string'
responseMatchesEach: {}
I read my request using:
* def Request = read('path/whatever.yml')
My test fail in this line
* match responseStatus == 200
Because I get a 500 response. This 500 response happens when the body is not formatted correctly, and it is also telling me "Required request body is missing".
I tried to send the request with postman and it works perfectly (Selecting raw and JSON)
{
"ids": [2,4]
}
So I'm pretty sure I'm not formatting the body correctly
The correct format ended up being this:
statusCode: 200
params: null
body:
ids:
- 2
- 4
matchResponse: true
responseMatches: '##string'
responseMatchesEach: {}
Everywhere I looked I saw that the format used in the question was also valid, but it didn't work for some reason.

Karate / Post Request : Unable to pass random generator variable in json payload

I am trying to create Karate feature file for API Testing.
It is a post request and we have to pass random number for 1 field i.e. order in request payload every time.
Now, I am trying to use this function in the same feature file to pass that random generated value in json payload before sending post request.
Could someone please have a look at my feature file and help.
Thanks
Also, Is there a way if i want to pass this same random value for a json payload created as a separate request.json payload file
Your requestPayload is within double quotes, so it became a string.
Here's an example that should get you going. Just paste it into a new Scenario and run it and see what happens.
* def temp1 = 'bar'
* url 'https://httpbin.org/anything'
* def payload = { foo: '#(temp1)' }
* request payload
* method post
And please read the documentation and examples, it will save a you a lot of time.

How to pass multiple json records using data driven approach in Karate DSL?

We have gone through the Karate documentation where we can compare the exact JSON object as a response (which contains multiple data records) but how can we pass and read the JSON in a single scenario?
Below is my sample.JSON and I want to read this in the request payload.
[{"name":"John","salary":"10000","age":"25"},
{"name":"Maria","salary":"20000","age":"27"}]
I have tried the JSON structure in the above format, however, I am getting below exception. Kindly help me in this.
status code was: 400, expected: 200, response time: 4315
Kindly suggest how to read and pass it in request payload of single scenario.
Thank you.
Status code 400 means you have made some other mistake with the request. Karate is working fine, it is just an HTTP client, maybe the request was not in the "shape" the server was expecting. Talk to the server-side team if you can or check the documentation of the API.
Here is a simple example that works, paste it and try:
* def body = [{"name":"John","salary":"10000","age":"25"}, {"name":"Maria","salary":"20000","age":"27"}]
* url 'https://httpbin.org/post'
* request body
* method post
* status 200
EDIT: for looping, please read the documentation.
The below example is just one way of doing it - please identify what best you are comfortable with: https://github.com/intuit/karate#data-driven-tests
Feature:
Background:
* def data = [{"name":"John","salary":"10000","age":"25"}, {"name":"Maria","salary":"20000","age":"27"}]
Scenario Outline:
* url 'https://httpbin.org/post'
* request __row
* method post
* status 200
Examples:
| data |

Karate Framework : #notnull and #present are not working in case response is empty

My validation is to verify weather orgId appears in the response or not and that orgId should contain some value
The response that i am getting is
Status code 200 and response body is empty.
Now I have below implementation
Then match $.orgId == '#present'
Then match $.orgId == '#notnull'
In this case the code passes, Ideally it should fail since response body is empty and orgId is not present in response.
My question is why is the code getting passed with #present and #notnull even if the response body is empty
You are definitely missing something. Try this in a fresh scenario and see it work. We are hard-coding response below, which is exactly equivalent to what happens at run-time, and by the way this is a great way for you to test assertions against different types of JSON (without making any HTTP calls):
* def response = {}
Then match $.orgId == '#present'
Then match $.orgId == '#notnull'
And this gives a failure as you expect:
assertion failed: path: $.orgId, actual: null, expected: '#present', reason: actual json-path does not exist
So if you still are stuck, follow this process please: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
EDIT: if your response is an empty string but you were expecting JSON, just do this and it will fail the test, refer type-conversion: https://github.com/intuit/karate#type-conversion
* json response = response
But as mentioned in the docs, you should always try to match "full JSON" so this should work:
* def response = ''
Then match $ contains { orgId: '#notnull' }
EDIT: this will be fixed in 0.9.4 https://github.com/intuit/karate/issues/814

Encoded path is encoded again

I have to make a request:
* url foo
* path bar
* path code
Code is retrieved from another request and I receive it url encoded.
The problem is when I put it in the path, karate encode it again.
Ex: I receive zxc1J%2BV%2FMnb and in path it becomes zxc1J%252BV%252FMnb.
%2Bis replaced by %252B.
When I decode received code and put it in path, it is not encoded.
My javascript function to decode is :
* def codeDecoded = decodeURIComponent(code)
and encoding function is * def codeEncoded = encodeURIComponent(codeDecoded)
Am I missing smth? What is wrong? How can I manage this? Thanks.
Edit:
#Peter Thomas I try my last chance, because I already showed the prb to someone from server and he didn't understand why karate encodes again smth already encoded and doesn't encode smth decoded.
So my first request is a POST request, which returns an encoded code in responseHeaders. Ex: GVkX1%2FKZEi%2FWQ.
In my second request I have to take this code and put it in the path ex: url/GVkX1%2FKZEi%2FWQ.
The problem is that karate transforms it to url/GVkX1%252FKZEi%252FWQ . And I don't need it. And if I decode url/GVkX1%2FKZEi%2FWQ before to put it in path, the url in karate is url/GVkX1/KZEi/WQ. When put in path, the decoded code is not encoded in karate. I hope it is more understandable.
Yes Karate will always encode the path you provide for your convenience. This is what 99% of users expect anyways.
There is nothing wrong with using a custom function to decode and ensure you pass un-encoded URL / path values to Karate, so by all means, please continue to do so !
Edit: quite likely the way you tried to decode may be wrong, try this:
* def encoded = 'zxc1J%2BV%2FMnb'
* def decoded = java.net.URLDecoder.decode(encoded, 'UTF-8')
* print decoded
Which prints:
[print] zxc1J+V/Mnb