how to use embedded expression with classpath - karate

I am trying to pass the absolute value of a file to the read function for classpath.
If I pass absolute path along with classpath it works fine. But when I pass embedded expression its not working
My code is as below
Scenario: create swagger first RAD
Given url appServer
Given param creationMethod = 'SWAGGER_FIRST'
And path '/integration/rest/rad'
And header X-CSRF-TOKEN = csrfToken
* cookie JSESSIONID = jsessionid
* cookie route = routevalue
* configure charset = null
print swaggerDetailsinputFile
print swaggerInputJsonFile
Given multipart file inputData = { read: 'classpath: #(swaggerDetailsinputFile)', filename: 'blob', contentType: 'application/json' }
Given multipart file swaggerFile = { read: 'classpath:ic/feature/RAD/swagger.json', filename: 'blob', contentType: 'application/json' }
And header Content-Type = 'multipart/form-data'
When method post
Need a way to pass embedded expression to classpath value for read function

Try this:
read: '#("classpath:" + swaggerDetailsinputFile)'
Make sure you read this part of the docs: https://github.com/intuit/karate#rules-for-embedded-expressions

Related

callSingle() usage in karate-config.js [duplicate]

I have a .feature file that will receive argument from another feature file as __arg to generate token for oAuth
Given url urlRefreshToken
Given def json = __arg
And header Content-Type = 'application/json; charset=utf-8'
And request json
* header Authorization = 'Bearer' + __arg.refresh_token
When method POST
Then status 200
* def bearer = 'Bearer ' + response.access_token
I am trying to centralise the token generation for only one time on karate-config.js. However I cannot seem to be able to use karate.callSingle() with passed parameters.
I use the feature file to generate token on other feature files as:
* def getToken = call read('classpath:features/Utils/GetToken.feature') refreshTokenRaymond
* header Authorization = getToken.bearer
I am trying to invoke the feature file for generating token on karate-config.js to no avail. I tried to pass in the additional parameter like this on karate-config.js:
var config = {
baseUrl: 'url',
urlRefreshToken: 'url',
refreshToken: '{refreshToken: refreshToken}'
};
var token = karate.callSingle('classpath:features/Utils/GetToken.feature', [config, config.refreshToken])
I wonder if it is possible to pass multiple parameter to karate.callSingle() called from karate-config.js?
Any help will be greatly appreciated. Thanks!
call and karate.callSingle() take only one argument, but you can easily create a new JSON out of other JSONs. Actually since you seem to be passing config as the argument - you can easily access config.refreshToken as __arg.refreshToken.
Your code is very confusing - but hope that this hint is what gets you on your way:
Given def json = __arg.refreshToken
If you need to create a JSON out of other data - I think you already know how to do that:
var temp = { config: config, refreshToken: refreshToken };
var token = karate.callSingle('classpath:features/Utils/GetToken.feature', temp);

Advice for implementing custom authentication scheme [duplicate]

First of all, thanks for build karate it's a very useful for test API's and UI's. We are using it to test a lot of our endpoints but we would like to know if there is a way or which is the best approach to handle requests with signature as part of the request in the header.
In our case we have two headers:
ApiKey: this value is always the same
Signature: this value depends on the request body content
Is there any way to inject the signature value just before the request is executed based on the request body content?
Here you can see two samples of the requests
Sample 1:
* url 'https://dev.sample.com'
* path '/api/user/getAll'
* header Content-Type = 'application/json'
* header ApiKey = 'XXX'
* header Signature = 'YYY'
And request { }
When method POST
Then status 200
Sample 2:
* url 'https://dev.sample.com'
* path '/api/user/getAll'
* header Content-Type = 'application/json'
* header ApiKey = 'XXX'
* header Signature = 'ZZZ'
And request { name: 'John' }
When method POST
Then status 200
Thanks
Karate has a "hook" for generating headers, but as of now it is not "aware" of the currently built request body + headers: https://github.com/intuit/karate#configure-headers
We got a similar request here, and are thinking of adding this capability: How to retrieve raw request contents before making a REST call in Karate DSL?
Maybe the OAuth examples will give you the way forward for your case for now: https://stackoverflow.com/a/55055111/143475
Feel free to raise an enhancement request, and we can get this in to the next version (with your help to test it). I'm thinking - what if you are able to call karate.get('request') from within the header JS function.
But for now all you need to do is do something like this:
* def body = { some: 'json' }
* karate.set('requestBody', body)
* url someUrl
* request body
* method post
And in the header.js function
function fn() {
var body = karate.get('requestBody');
var sign = Utils.sign(body);
return { Signature: sign };
}
EDIT: this will be implemented in Karate 1.0 onwards: https://github.com/intuit/karate/issues/1385

How to handle requests with signatures on karate tests?

First of all, thanks for build karate it's a very useful for test API's and UI's. We are using it to test a lot of our endpoints but we would like to know if there is a way or which is the best approach to handle requests with signature as part of the request in the header.
In our case we have two headers:
ApiKey: this value is always the same
Signature: this value depends on the request body content
Is there any way to inject the signature value just before the request is executed based on the request body content?
Here you can see two samples of the requests
Sample 1:
* url 'https://dev.sample.com'
* path '/api/user/getAll'
* header Content-Type = 'application/json'
* header ApiKey = 'XXX'
* header Signature = 'YYY'
And request { }
When method POST
Then status 200
Sample 2:
* url 'https://dev.sample.com'
* path '/api/user/getAll'
* header Content-Type = 'application/json'
* header ApiKey = 'XXX'
* header Signature = 'ZZZ'
And request { name: 'John' }
When method POST
Then status 200
Thanks
Karate has a "hook" for generating headers, but as of now it is not "aware" of the currently built request body + headers: https://github.com/intuit/karate#configure-headers
We got a similar request here, and are thinking of adding this capability: How to retrieve raw request contents before making a REST call in Karate DSL?
Maybe the OAuth examples will give you the way forward for your case for now: https://stackoverflow.com/a/55055111/143475
Feel free to raise an enhancement request, and we can get this in to the next version (with your help to test it). I'm thinking - what if you are able to call karate.get('request') from within the header JS function.
But for now all you need to do is do something like this:
* def body = { some: 'json' }
* karate.set('requestBody', body)
* url someUrl
* request body
* method post
And in the header.js function
function fn() {
var body = karate.get('requestBody');
var sign = Utils.sign(body);
return { Signature: sign };
}
EDIT: this will be implemented in Karate 1.0 onwards: https://github.com/intuit/karate/issues/1385

How to pass multiple parameters to callSingle karate on karate-config.js

I have a .feature file that will receive argument from another feature file as __arg to generate token for oAuth
Given url urlRefreshToken
Given def json = __arg
And header Content-Type = 'application/json; charset=utf-8'
And request json
* header Authorization = 'Bearer' + __arg.refresh_token
When method POST
Then status 200
* def bearer = 'Bearer ' + response.access_token
I am trying to centralise the token generation for only one time on karate-config.js. However I cannot seem to be able to use karate.callSingle() with passed parameters.
I use the feature file to generate token on other feature files as:
* def getToken = call read('classpath:features/Utils/GetToken.feature') refreshTokenRaymond
* header Authorization = getToken.bearer
I am trying to invoke the feature file for generating token on karate-config.js to no avail. I tried to pass in the additional parameter like this on karate-config.js:
var config = {
baseUrl: 'url',
urlRefreshToken: 'url',
refreshToken: '{refreshToken: refreshToken}'
};
var token = karate.callSingle('classpath:features/Utils/GetToken.feature', [config, config.refreshToken])
I wonder if it is possible to pass multiple parameter to karate.callSingle() called from karate-config.js?
Any help will be greatly appreciated. Thanks!
call and karate.callSingle() take only one argument, but you can easily create a new JSON out of other JSONs. Actually since you seem to be passing config as the argument - you can easily access config.refreshToken as __arg.refreshToken.
Your code is very confusing - but hope that this hint is what gets you on your way:
Given def json = __arg.refreshToken
If you need to create a JSON out of other data - I think you already know how to do that:
var temp = { config: config, refreshToken: refreshToken };
var token = karate.callSingle('classpath:features/Utils/GetToken.feature', temp);

How can I change the filename in a fine-uploader blob request

Using the basic fine-uploader and addFile to insert a blob based entry.Everything goes ok until the server reject the request due to "security issues".
If I convert the blob to a file and send it to the fine-uploader the server is quite happy. The only difference between the two requests is the filename in the request header.
The file-uploader setName() doesn't appear to change to "blob" name.
fails:
Content-Disposition: form-data; name="qqfile"; filename="blob"
Content-Type: image/jpeg
Any way to replace the 'filename="blob"' with a name that contains the correct extension - blob.jpg, blob.gif,etc?
var myblob = new Blob([outputBuffer], {type: "image/jpeg"});
myblob.name = ofile;
var myfile = new File([myblob], 'ImageTest.jpg', {
lastModified: new Date(0), // optional - default = now
type: "image/jpeg" // optional - default = ''
});
fineUploader.addFiles({blob:myblob, name:ofile});
console.log(fineUploader.getName(0));
fineUploader.setName(0,ofile);
console.log(fineUploader.getName(0));
fineUploader.addFiles(myfile);