Validating API response in Karate framework [duplicate] - karate

This question already has answers here:
Can we parameterize the request file name to the Read method in Karate?
(2 answers)
Closed 1 year ago.
I want help for validating API response using karate framework.
I have API’s which are “Independent” on each other.
I have POST method along with request parameters. when I hit that particular API got the response with different parameters (no single match from request parameter and response parameter).
Now I want to validate response parameter value.
example : request: “method” post
school name: “abcd”
register date : “1:10:2010″
Response:
Principle name : ” pqrs”
Principle Email id “pqrs#gmail.com
now I want to validate that “principle name ” should not be null
I have implemented like this but it it doesn’t work
Feature: School info
Background:
* url baseUrl
Scenario: check Principles info
Given path ‘School info’
And request {school name: “abcd” ,register date : “1:10:2010”}
When method post
Then status 200
And match response.response contains {“type”: “Success”,”code”:20000}
And match response.principle list[*] { “Principle name”: “#notnull”}
whenever I run this file it always passes the API wvwnt if the Principle name filed is null.
It just checks the success message (And match response.response contains {“type”: “Success”, ”code”:20000}) and pass the API

your code for validating principlelist doesn't have proper assertions.
match each will be more convenient for validating json array with a schema
* match each response.principlelist contains {"Principal name" : "#notnull"}

Related

Mule 4: uriParams size is showing as 0 even though it is there

So I created a endpoint inside the raml file such as:
/proxy:
/{proxyDestinationTarget}:
uriParameters:
proxyDestinationTarget:
type: string
example: "myurl.com"
post:
description: Pass through operation that targets IAA
is: [client-credentials-required,standard-error-responses,traceHeaders]
and then inside of the logic.xml in my variable component I have
attributes.uriParams.'proxyDestinationTarget'
when I send the request in postman and debug i am getting a uriParams size of 0
the url i entered in postman is
https://localhost:8092/proxy/uat.something.somethingElse.com/Assign/Assignment.svc
but if i send a request like this :
https://localhost:8092/proxy/uat.something.somethingElse.com
I get a uriParam size = 1 which is what I want. I guess the / is whats causing the problem. how can I pass url as uri param with escape characters???
It looks like you are not sending an URL that matches the RAML definition in the first case.
For the URL:
https://localhost:8092/proxy/uat.something.somethingElse.com/Assign/Assignment.svc
The RAML defined that the API should expect /proxy/{proxyDestinationTarget} but it is receiving something like /proxy/{proxyDestinationTarget}/Assign/Assignment.svc, where {proxyDestinationTarget} is uat.something.somethingElse.com, but nothing matches /Assign/Assignment.svc. The API should include those two last components too to match. It is not escaping them, they probably need to be defined.

How do I pass apikey as authorization in a POST/GET request in Karate Framework feature file [duplicate]

This question already has an answer here:
Is there a way to update the headers in one feature file and use the Auth token from Karate.config.js?
(1 answer)
Closed 1 year ago.
This is the first time I am trying my hands at Karate for my work. I have a feature file that is a GET request and uses an authorization method as an apikey but when I am passing an authorization as a header its giving me an error
"required (...)+ loop did not match anything at input 'Scenario:'"
Below is my feature file for reference
Feature: Get Profile
Background:
* url 'https://csXXX-XXXX.XXXXXXXXX.net'
* header Accept = 'application/json'
* header Authorization = 'apikey XXXX-XXXX-XXXX-XXXX'
Scenario: Profile GET on an id
Given path '/v1/Profile'
And param idProfile='XXXX'
When method get
Then status 200
Any help is appreciated.
Looks like you missed the Feature: at the top of the file.
I suggest you use the quickstart or ZIP release, so you get a ready to use test to try: https://github.com/intuit/karate#getting-started

in Karate DSL, is there a way to have redirects perform a POST request instead of a GET request? [duplicate]

This question already has an answer here:
Post method gets converted to GET after redirection
(1 answer)
Closed 2 years ago.
I have the following Karate script that by default has redirects turned on.
Scenario: First Test
Given path 'somePath'
And request ''
And header Content-Type = 'text/html'
And param _csrf = csrf
And param username = 'username'
And param password = 'password'
When method post
Then status 200
The issue is after getting a 302 from the API, the next request automatically submits a GET request. I would like it to submit a POST request instead.
in cURL, there is an existing parameter that allows users to do that. see below.
--post302 Do not switch to GET after following a 302
is there anyway to do that in Karate DSL?
Yes please read the docs for configure folowRedirects. There is also an example on how to read the Location response header to manually make the request you want.
Scenario: get redirects can be disabled
* configure followRedirects = false
Given path 'redirect'
When method get
Then status 302
And match header Location == demoBaseUrl + '/search'
* def location = responseHeaders['Location'][0]
Given url location

Karate : Passing variable from one feature file to another as a query parameter [duplicate]

This question already has answers here:
Can we parameterize the request file name to the Read method in Karate?
(2 answers)
Closed 1 year ago.
I'm trying to execute a feature file and need which takes a query parameter from another feature.
Here is the feature files :
1. This file call two feature files. First feature file add a record in the database and returns json response. I have to use a property from the response and have to pass to another feature. Please find below for the features files.
Feature:
Background:
Scenario:
Given call read('test_add.feature')
And def query1 = response.name
Given call read('test_get.feature') {'**query**' : #query1}
feature name - test_get.feature
Feature: Add a new Nat bundle device
Background:
url baseUrlWithContext
def headers = { 'Content-Type': 'application/json' }
Scenario: Addition
Given headers headers
And path '/test'
And params query
When method get
Then status 200
Error :
com.intuit.karate.exception.KarateException:
cannot convert to map: query
Instead of And params query
Try this:
And param query = query

ShopStyle API - How to Invoke an HTTP request

How do I Invoke an HTTP request with a particular URL and process the body of the response as XML?
Information Provided by ShopStyle:
HOW TO USE THE API:
Choose the method that returns the data your application needs. For example, the /products method is used to get products that match a given category or brand.Construct a URL for that method with the appropriate host, method name, and query parameters. Invoke the URL as an HTTP GET.
When the HTTP response arrives, extract the required data elements from the response's body.The rest of this document describes the details of constructing the right URL for each of the API methods. The XML format of the responses may be seen by clicking on the sample URLs shown for each method. The responses in JSON format contain identical information, just in a different format.
SHOPSTYLE API URLS
All ShopStyle API URLs have the following form:
http://api.shopstyle.com/api/VERSION/METHOD_NAMEpid=YOUR_API_KEY&format=FORMAT&...
The METHOD_NAME is taken from the list of methods in the various API shown at left (Press Link To View List of Methods-https://www.shopstylecollective.com/api/overview).
COMMON API PARAMETERS
All methods in the API accept these parameters:
API_KEY (my unique key is ******************)
pid Unique API_KEY string that is assigned to the caller. You can find your API Key on the Account Settings page.
FORMAT
The format of the response. Supported values are:
json - The response is in JSON format with UTF-8 encoding. This is the default if the parameter is absent.
xml - The response is in XML format with UTF-8 encoding.
jsonp - The response is in JSON format with UTF-8 encoding wrapped in a JavaScript method called padding. The padding must be specified with the query parameter 'callback'. Only single expressions (function reference, or object property function reference) are accepted as valid padding.
When set to 1 or 'true' the HTTP status will always be 200. Use the field "errorCode" in the response to detect whether the API Call was successful. By default, when an error occur the HTTP Status of the response will be different than 200
Again I am a beginner, so please provide detailed information on how to retrieve CATEGORY data (Examples: Dresses, Tops, Buttoms, etc) in XML format.**
Thank you!!!
Here's a simple example to get your started. Copy the code below and put it into a file, say "cat.php". Then run it by typing "php cat.php" at a command prompt (assumes you have php on your machine):
<?php
// don't show dom parse errors
libxml_use_internal_errors(true);
// grab the xml from the api
$response = file_get_contents("http://api.shopstyle.com/api/v2/categories?pid=TEST&format=xml");
$doc = new DOMDocument();
$doc->loadHTML($response);
// grab all the categories
$elements = $doc->getElementsByTagName('categories');
foreach($elements as $node){
foreach($node->childNodes as $child) {
// get the name out of the category
$nodes = $child->getElementsByTagName("name");
foreach ($nodes as $name) {
echo $name->nodeValue . PHP_EOL;
}
}
}