I made a Test Case:
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.testobject.TestObjectProperty
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import internal.GlobalVariable
// I have a Test Object "Object Repository/GET localhost"
// which points "http://localhost/" with no Authorization inforation
TestObject tObj = findTestObject("GET localhost")
// convert it into a RequestObject
RequestObject reqObj = injectAuthenticationHeader((RequestObject)tObj, GlobalVariable.username, GlobalVariable.password)
// send HTTP request
ResponseObject response = WS.sendRequest(reqObj);
RequestObject injectAuthenticationHeader(RequestObject request, String username, String password) {
// construct a "username:password" string using GlobalVariable
String credential = "${GlobalVariable.username}:${GlobalVariable.password}"
println "credential: ${credential}"
// encode it with Base64
String encoded = Base64.getEncoder().encodeToString(credential.getBytes())
println "encoded: ${encoded}"
List<TestObjectProperty> headers = removeAuthorizationHeader(request.getHttpHeaderProperties())
headers.add(new TestObjectProperty("Authorization", ConditionType.EQUALS, "Basic ${encoded}"))
request.setHttpHeaderProperties(headers)
return request
}
List<TestObjectProperty> removeAuthorizationHeader(List<TestObjectProperty> listTOP) {
List<TestObjectProperty> list = new ArrayList<TestObjectProperty>()
for (TestObjectProperty top : listTOP) {
if (top.getName().equals("Authorization")) {
; // ignore it
} else {
list.add(top)
}
}
return list
}
The Test Object “GET localhost” looked like this:
Please note that the Test Object has no Authorization information set in the GUI.
The test case scripts generates a HTTP header “Authorization: Basic xxxxxx” dynamically refering to the GlobalVariable.username
and GlobalVariable.password
, and inject the header into the RequestObject.
When I executed this, it showed a message in the console:
2023-02-12 15:59:26.927 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2023-02-12 15:59:26.930 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/GET localhost with Authorization
credential: mockUsername:mockPassword
encoded: bW9ja1VzZXJuYW1lOm1vY2tQYXNzd29yZA==
2023-02-12 15:59:30.076 INFO c.k.k.core.webservice.common.HarLogger - HAR: /var/folders/7m/lm7d6nx51kj0kbtnsskz6r3m0000gn/T/Katalon/Test Cases/GET localhost with Authorization/20230212_155923/requests/main/0.har
2023-02-12 15:59:30.257 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/GET localhost with Authorization
When I executed this, my HTTP server printed the request content as follows:
<<<< Request received
{
"method": "GET",
"uri": "/",
"headers": [
{"Accept-encoding": ["gzip,deflate"]}
{"Connection": ["Keep-Alive"]},
{"Host": ["localhost"]},
{"User-agent": ["Apache-HttpClient/4.5.1 (Java/1.8.0_275)"]},
{"Authorization": ["Basic bW9ja1VzZXJuYW1lOm1vY2tQYXNzd29yZA=="]},
],
"body": """"""
}
Please find the Authorization
header is sent from the Test Case script to the server.
This way the Test Case script could generate a Authorization header dynamically referring to the GlobalVariables which are chosen when you execute the Test Case.
@sedens
This is what you wanted, isn’t it?