Open donholgo opened 6 months ago
Likely due to no shared scope here. karate.call
and karate.callSingle
do not default to shared scope. In fact, I don't think callSingle even has an option for that.
Can you try this instead:
* callonce read('header.feature')
That works indeed, at least in the simplified example files. In my real tests I'd like to have the call configurable, though (we have a development mode that doesn't need it), so it really looks like
* if (useAuthentication) karate.callSingle('header.feature', { username: 'myuser' } )
Any idea how I can have that with callonce
?
Yes, that can be achieved by putting the conditional within the called feature/js instead.
However, this would be arguably less readable since someone would have to check the called file to find out the condition, plus it's a wasted read/call to the file in dev. Tho, maybe that's all you need given that there's no shared scope overload for callSingle.
@ptrthomas , how would you feel about karate.callSingle
having an overload for shared scope to be in parity with karate.call
?
@dvargas46 yes I think that makes sense. interesting why this has not come up before. I was looking at the existing reference example:
1) https://github.com/karatelabs/karate/blob/03a840805ec0db5ec3f4330754545f1eefbcde0b/karate-demo/src/test/java/karate-config.js 2) https://github.com/karatelabs/karate/blob/2e73fc38e397cf4e46c38b5256c0d91261ea5920/karate-demo/src/test/java/demo/headers/headers-single.feature
so even the examples, the pattern is - a) callSingle returns some data b) that data is used to configure headers "centrally" in karate-config.js
@ptrthomas I think you are right, I believe what you outlined would be the recommended pattern to use here too.
For example, it could looks something like this to set the auth header globally instead using callSingle:
karate-config.js
function fn() {
const config = {}
const headers = {}
if (karate.env !== 'dev') {
// configure headers with authentication only in non-dev environments - callSingle only called once
const results = karate.callSingle('header.feature', {username: 'myUsername'})
headers.Authorization = results.authHeader
}
//
// any other headers or config variables can also be set ...
//
karate.configure('headers', headers)
return config
}
header.feature
@ignore
Feature: Header
Scenario: Return Authorization header
# or perform some operation with the input first, like Base64 encoding of credentials, etc.
* def authHeader = 'Basic 12345'
test.feature
Feature: Test
Background:
* url 'http://example.com'
# set any scenario specific headers as needed
* header X-TOKEN = 'token-needed-here'
Scenario: Some test
When method GET
Then status 200
To be able to run my tests with an external authentication tool, I have tried to factor out the authentication request into a separate file, but the
configure headers
in that file does not seem to have any effect in the tests.The problem can already be reproduced with the attached files (without any real authentication and just a dummy call to example.com):
header.feature
test.feature
Running
shows that header.feature is called properly, but the GET call in test.feature does not have the "Authorization" header that should have been set.
testfiles.zip