Karate SAXParseException when logPrettyResponse is true and response content type is 'text/turtle' - karate

It looks like a bug but asking the question just in case I have missed something.
Karate version is 1.1.0
The post request is as below. Note that content type is text/turtle and I use logPrettyResponse because further requests in test are testing RDF/XML and other serialisations.
* configure logPrettyResponse = true
Given path '/graph'
* text payload =
"""
<http://example.com/a7460f22-561a-4dde-922b-665bd9cf3bd9> <http://schema.org/description> "Test"#en.
"""
And request payload
And header Accept = 'text/turtle'
And header Content-Type = 'text/turtle'
When method POST
Then status 200
And I get below error
ERROR com.intuit.karate - org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 8; Element type "http:" must be followed by either attribute specifications, ">" or "/>"., http call failed
Reason this seems to be happening is that fromString method in JsValue.java is trying to parse turtle as XML because condition case '<' is true when response is turtle.
public static Object fromString(String raw, boolean jsonStrict, ResourceType resourceType) {
String trimmed = raw.trim();
if (trimmed.isEmpty()) {
return raw;
}
if (resourceType != null && resourceType.isBinary()) {
return raw;
}
switch (trimmed.charAt(0)) {
case '{':
case '[':
return jsonStrict ? JsonUtils.fromJsonStrict(raw) : JsonUtils.fromJson(raw);
case '<':
if (resourceType == null || resourceType.isXml()) {
return XmlUtils.toXmlDoc(raw);
} else {
return raw;
}
default:
return raw;
}
}
To solve the problem I have set logPrettyResponse to false. I would love to know if anyone has any other thoughts or if someone can confirm that it is a bug?

Related

karate.match is throwing a TypeError when validating against a string

I am doing some UI automation using Karate. In this particular scenario, the goal is to navigate to a page that has a table with pagination. If there is a "next page" button, to click it, and to verify the pagination changes as expected (pagination will always return either an 8 or a 9).
I have the following code:
* def getPagination =
"""
function(){
var nameVal = []
var nextPageBtn = false
do {
nextPageBtn = driver.exists('skipToNext a');
if (nextPageBtn {
driver.click('skipToNext a');
var getPagination = (driver.text(li[class='active'] a)).trim()
karate.log("The current page value is:", getPagination)
karate.match (getPagination == '8' || '9')
var getNameVal = driver.scriptAll('#names', '_.textContent')
nameVal.push(getNameVal)
}
} while (nextPageBtn)
return(nameVal);
}
I am getting the following error on the match:
org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (match) on com.intuit.karate.core.ScenarioBridge#5d32d8f failed due to: no applicable overload found (overloads: [Method[public java.lang.Object com.intuit.karate.core.ScenarioBridge.match(java.lang.String)], Method[public java.lang.Object com.intuit.karate.core.ScenarioBridge.match(java.lang.Object,java.lang.Object)]], arguments: [true (Boolean)])
- <js>.:anonymous(Unnamed:17)
Any ideas?

Is there a way to assert and fail a request after polling in karate?

I have a request where i get Processing or Submitted in a response parameter if the request is in process or passed respectively.
I am able to poll and get if the status is "Processing" or"Submitted" but after that I am unable to fail the request if still i am not getting the expected status after polling for 5 times.
How can i fail request after certain retries do not provide me expected response?
The answer is in your question,
I assume you are polling using a js function,
If so you can add a boolean return from that, if you condition not met return false or if condition met return true then assert the value returned from your feature file.
* def pollingFunc =
"""
function(x) {
// your polling logic which retrives status
if (status == x) {
return true;
}
else{
return false;
}
}
"""
In feature
* def statusFound = pollingFunc("Processed" )
* assert (statusFound == true)
If the expected status not obtained after polling the assert will fail the test

Getting junk value(com.intuit.karate.ScriptObjectMap#XXXX ) when i call karate feature( using Karate.call) in javascript

I am trying to call a karate feature in javascript and capture its response as below, but while doing, the response from karate.call is showing junk value(com.intuit.karate.ScriptObjectMap#XXXX ) . kindly help to get the actual values from karate.call or suggest me any best idea ?
function RequestMandator(featurepath,data) {
var Mandator = [];
data.forEach(function(data){
var TransferId = data.TransferID;
var FocusKey = data.TransferID + ':';
var TimeStamp = data.LastUpdate;
var result = karate.call(featurepath, { input: [TransferId, FocusKey,TimeStamp ] });
karate.log('Added Mandator :', result);
Mandator.push(result);
})
return Mandator;
}
Output:
11:32:53.307 [main] WARN com.intuit.karate - xml parsing failed, response data type set to string: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 15; Open quote is expected for attribute "border" associated with an element type "table".
11:32:53.310 [main] INFO com.intuit.karate - Added Mandator : com.intuit.karate.ScriptObjectMap#102d92c4
Error:
com.intuit.karate.exception.KarateFileNotFoundException: C:\XXXXXXXX\com.intuit.karate.ScriptObjectMap#7808fb9,com.intuit.karate.ScriptObjectMap#25d958c6,com.intuit.karate.ScriptObjectMap#5eeedb60,com.intuit.karate.ScriptObjectMap#6ad6fa53,com.intuit.karate.ScriptObjectMap#6f099cef,com.intuit.karate.ScriptObjectMap#2d66530f,com.intuit.karate.ScriptObjectMap#25b865b5 (The filename, directory name, or volume label syntax is incorrect)
at com.intuit.karate.FileUtils.getFileStream(FileUtils.java:146)
at com.intuit.karate.FileUtils.readFile(FileUtils.java:110)
at com.intuit.karate.ScriptBridge.read(ScriptBridge.java:67)
Please refer to the documentation on type-conversion: https://github.com/intuit/karate#type-conversion
It is not possible to figure out based on the incomplete information you have provided. Still let me try, I think you have done some mistake in string concatenation before calling this function. And the value of featurepath is completely wrong.
In the example below, see how a string concatenation within a JS function leads to what you call "junk value":
* def fun = function(){ var temp = { hello: 'world' }; return temp + '' }
* def bar = fun()
* print "bar:", bar
Results in output:
13:52:50.912 [main] INFO com.intuit.karate - [print] bar: [object Object]
If you still are stuck, the only suggestion I have is follow the instructions here please: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Why messages are not added in Json response like error messages

If I use 'web-send-json-response="true"' while calling any service, it returns json response using sendJsonResponse(Object responseObj) method of WebFacadeImpl.groovy.
Where It adds 'errors' to json response if 'eci.getMessage().hasError()' true. But, Is there any reason why messages are not added in Json Response?
As I am willing to have messages in json response, I added few lines in sendJsonResponse() method.
The code block where I added few lines to add messages to json response:
} else if (responseObj != null) {
responseObj.put("messages", eci.message.messages)
JsonBuilder jb = new JsonBuilder()
jb.call(responseObj)
jsonStr = jb.toString()
response.setStatus(HttpServletResponse.SC_OK)
} else {
jsonStr = ""
if (eci.message.messages) {
responseObj.put("messages", eci.message.messages)
JsonBuilder jb = new JsonBuilder()
jb.call(responseObj)
jsonStr = jb.toString()
}
response.setStatus(HttpServletResponse.SC_OK)
}
It works fine and I am getting messages in json response.

i am having a issue with json codeigniter rest its not closing the tag

i am having a problem with json codeigniter rest
i am making this call to the server and the problem its that its not closing the json tags
s, USA","clientUID":"7","email":null,"idipad":"2","dateModified":null},{"id":"19","uid":null,"name":"Wayne Corporation, Inc.","phone":"932345324","address":"Second st. 312, Gotham City","clientUID":"7","email":"waynecorp#gmail.com","idipad":"1","dateModified":null}]
its missing the final }
this is the code that creates the response :
$this->response(array('login'=>'login success!','user_admin_id'=>$user_id,'client'=>$client,'users'=>$users,'projects'=>$projects,'plans'=>$plans,'meetings'=>$meetings,'demands'=>$demands,'tasks'=>$tasks,'presences'=>$presences,'contractors'=>$contractors,'companies'=>$companies), 200);
this is the client call using curl :
$this->curl->create('http://dev.onplans.ch/onplans/index.php/api/example/login/format/json');
// Option & Options
$this->curl->option(CURLOPT_BUFFERSIZE, 10);
$this->curl->options(array(CURLOPT_BUFFERSIZE => 10));
// More human looking options
$this->curl->option('buffersize', 10);
// Login to HTTP user authentication
$this->curl->http_login('admin', '1234');
// Post - If you do not use post, it will just run a GET request
//$post = array('remember'=>'true','email'=>'admin.architect#onplans.ch','password'=>'password');
$post = array('remember'=>'true','email'=>'admin.architect#onplans.ch','password'=>'password');
$this->curl->post($post);
// Cookies - If you do not use post, it will just run a GET request
$vars = array('remember'=>'true','email'=>'manuel#ffff.com','password'=>'password');
$this->curl->set_cookies($vars);
// Proxy - Request the page through a proxy server
// Port is optional, defaults to 80
//$this->curl->proxy('http://example.com', 1080);
//$this->curl->proxy('http://example.com');
// Proxy login
//$this->curl->proxy_login('username', 'password');
// Execute - returns responce
echo $this->curl->execute();
// Debug data ------------------------------------------------
// Errors
$this->curl->error_code; // int
$this->curl->error_string;
print_r('error :::::LOGINN REMOTE:::::'.$this->curl->error_string);
// Information
$this->curl->info; // array
print_r('info :::::::::::::'.$this->curl->info);
the response belong to the rest api codeigniter from phil
/**
* Response
*
* Takes pure data and optionally a status code, then creates the response.
*
* #param array $data
* #param null|int $http_code
*/
public function response($data = array(), $http_code = null)
{
global $CFG;
// If data is empty and not code provide, error and bail
if (empty($data) && $http_code === null)
{
$http_code = 404;
// create the output variable here in the case of $this->response(array());
$output = NULL;
}
// If data is empty but http code provided, keep the output empty
else if (empty($data) && is_numeric($http_code))
{
$output = NULL;
}
// Otherwise (if no data but 200 provided) or some data, carry on camping!
else
{
// Is compression requested?
if ($CFG->item('compress_output') === TRUE && $this->_zlib_oc == FALSE)
{
if (extension_loaded('zlib'))
{
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
{
ob_start('ob_gzhandler');
}
}
}
is_numeric($http_code) OR $http_code = 200;
// If the format method exists, call and return the output in that format
if (method_exists($this, '_format_'.$this->response->format))
{
// Set the correct format header
header('Content-Type: '.$this->_supported_formats[$this->response->format]);
$output = $this->{'_format_'.$this->response->format}($data);
}
// If the format method exists, call and return the output in that format
elseif (method_exists($this->format, 'to_'.$this->response->format))
{
// Set the correct format header
header('Content-Type: '.$this->_supported_formats[$this->response->format]);
$output = $this->format->factory($data)->{'to_'.$this->response->format}();
}
// Format not supported, output directly
else
{
$output = $data;
}
}
header('HTTP/1.1: ' . $http_code);
header('Status: ' . $http_code);
// If zlib.output_compression is enabled it will compress the output,
// but it will not modify the content-length header to compensate for
// the reduction, causing the browser to hang waiting for more data.
// We'll just skip content-length in those cases.
if ( ! $this->_zlib_oc && ! $CFG->item('compress_output'))
{
header('Content-Length: ' . strlen($output));
}
exit($output);
}
This answer was referenced from Github issue. Also raised by Pedro Dinis, i guest.
I met this problem today and take me long hours to search for the solution. I share here with hope to help someone like me.
The key is to replace around line 430 in the library file: REST_Controller.php :
header('Content-Length: ' . strlen($output));
by
header('Content-Length: ' . strlen("'".$output."'"));
UPDATE: The problem was solved here
Or you can just comment out the code, it will run fine. :)