Karate framework default path - karate

In my test all requests use the same url ('http://localhost:8080/example')
I defined that url in background section:
Feature: Some feature
Background:
* url 'http://localhost:8080/example'
Is there a better way to write this?
Scenario:
Given path ''
...
Thanks!

You don't need the Given path ''. You can start with the * request {}.

Related

Why karate dsl replace "space" for "+" in param

i have this code:
Scenario: Get Token
Given url 'https://localhost/api/accessToken'
And param scope = 'collections payments'
Log:
1 > POST https://localhost/api/accessToken?scope=collections+payments
This Post faild for me.
Please, i need this:
https://localhost/api/accessToken?scope=collections%20payments
Karate is doing the right thing and your server probably has a bug.
Refer: https://stackoverflow.com/a/1634293/143475
But if you want to send it the way you are asking, put it as the URL itself, and don't use param:
* url 'https://httpbin.org/anything?foo=one%20two'
* method get
Explained here: https://stackoverflow.com/a/59977660/143475

How do use the same variable in background and feature being called

With Karate 1.1.0, you could define a variable in the background, ex "* path = 'www.google.ca', call a "helper" feature at the top of of a scenario, which was using the same variable name inside it, then later in your scenario use the same variable again. Ex., helper feature would have "* path = 'www.google.ca' in it's 1 and only scenario, which would say something like "Given path '/help.html'". Then in your main feature, after you have called the helper feature you would use the variable again, something like "Given path '/fun.html'". When everything ran it would be fine, the helper feature would point to it's path and the main feature would point to it's path.
Now with Karate 1.2.0.RC6, I don't have to declare path in the helper feature, but with that change the path is being concatenated in the main feature/scenario. So when the scenario executes, it calls the helper feature fine, but then instead of it's path being "www.google.ca/fun.html" it comes "www.google.ca/help.html/fun.html". Any ideas why or better how to resolve it?
Here's the actual code:
Feature: Update calls on an account
Background:
* url url
* header Authorization = token
* path 'care/v1.1/account/'
Scenario: theScenario
* def fullResponse = call read('init/movein_ADD.feature') { payloadFilename: 'data/account_id_call_ADD_MOVEIN_No_GovtId.json'}
* def updatePayload = read('data/account_id_call_add_MOVEIN_Both_GovtId.json')
* set updatePayload.callNumber = fullResponse.response.callNumber
* set updatePayload.id = fullResponse.response.id
* set updatePayload.orderNumber = fullResponse.response.orderNumber
* set updatePayload.stagedServices = null
* header Accept = json
* header Content-Type = json
Given path ENCODE('16382-8') + '/call/update'
And request updatePayload
When method POST
Then status 200
Helper feature:
#ignore
Feature: Helper feature file to create Move-In Service order request
Scenario: helper Scenario
* url url
* path 'care/v1.1/account/'
* def payload = read(payloadFilename)
#set up request and execute
* header Accept = json
* header Content-Type = json
Given path 'MTYzODJAQDg=/call/add'
And request payload
When method POST
Then status 200
The answer, change to declare a variable in the background of the main feature file, set the path to that variable in the helper feature scenario and again the main feature scenario right after the call to the helper feature file has worked. Going to cause some big refactoring on our part based on this change.

In karate mocking (karate-netty), how can we override request header value?

Objective:
We want few API calls should go to mock-server(https://192.x.x.x:8001) and others should go to an actual downstream application server(https://dev.api.acme.com).
Setup :
On local, mock server is up with standalone jar on port 8001. e.g https://192.x.x.x:8001
In application config file (config.property)downstream system(which need to mock) defined with mockserver IP i.e https://192.x.x.x:8001
Testing scenario and problem:
1.
Scenario: pathMatches('/profile/v1/users/{id}/user')
* karate.proceed('https://dev.api.acme.com')
* def response = read ('findScope.json')
* def responseStatus = 200ˀˀ
* print 'created response is: ' + response
Now, when we hit API request via postman or feature file then it does karate.proceed properly to https://dev.api.acme.com/profile/v1/users/123/user instead of 192.x.x.x. However, in this request, host is referring to https://192.x.x.x:8001 instead of https://dev.api.acme.com which create a problem for us.
How can we override request header in this case? I did try with karate.set and also with header host=https://192.x.x.x:8001 but no luck.
Thanks!
Please see if the 1.0 version works: https://github.com/intuit/karate/wiki/1.0-upgrade-guide
Unfortunately https proxying may not work as mentioned. If you are depending on this, we may need your help (code contribution) to get this working
If the Host header is still not mutable, that also can be considered a feature request, and here also I'd request you to consider contributing code

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

Use comma in path in karate test

With the following test case
Background:
* callonce read('auth.feature')
* url java.lang.System.getenv('TEST_URL')
Scenario: Call the file endpoint without authorization
Given path 'files/123695_11,8'
When method get
Then status 401
I get a parser error about mismatching quotes. The reason is probably that the "path" is confused by the comma, as that can also be used to denote sub-paths.
I thought about just changing the , to %2C, but then karate calls the URL with the % encoded to %25, resulting in a wrong URL 'files/123695_11%252C8' which decodes to literally 'files/123695_11%2C8'.
How can I make this work correctly?
Simplest option, merge into url:
* url 'https://httpbin.org/anything/files/123695_11,8'
* method get
I know you may want to "re-use" stuff in the background, so use variables:
Background:
* def baseUrl = 'https://httpbin.org/anything'
Scenario:
* url baseUrl + '/files/123695_11,8'
* method get
Hacky workaround:
* url 'https://httpbin.org/anything'
* def temp = 'files/123695_11,8'
* path temp
* method get