-
-
Notifications
You must be signed in to change notification settings - Fork 399
How to pass custom security token into the request header which is new for every request?
- This is particularly useful for
- JWT token
- SAML token
- Or any OAuth token which are dynamic every time
Hi @authorjapps . I am trying to use the zerocode to automate the integration testing. I have a question: How can i get the the gwt token and pass it dynamically to set it in the header when i execute the request.. i tried hard coded token the test works, but only till the token is valid.. i have the code to get the token,... my problem is how can i pass it every time i run the test... any information resource of example will be very helpful.
@Sidharath, Understood. It's easy. Actually there are many ways you can do it. This one below should help and simpler approach. (Also there is another approach to abstract this implementation to the HttpClient level, but let's take a simpler approach for now)
{
"scenarioName": "Generate new Security Token and pass as custom header",
"steps": [
{
"name": "token_brewer",
"url": "org.jsmart.zerocode.zerocodejavaexec.utils.TokenGenerator",
"operation": "generateNew",
"request": "any_param_or_empty",
"assertions": {
"newToken" : "$NOT.NULL"
}
},
{
"name": "get_user_with_new_token",
"url": "/users/octocat",
"operation": "GET",
"request": {
"headers":{
"security_token":"${$.token_brewer.response.newToken}"
}
},
"assertions": {
"status": 200,
"body": {
"login" : "octocat"
}
}
}
]
}
- The working example is in
hello-world repo
to clone and execute locally : JUnit Test code - See the token generator code(you have to fit your logic here): TokenGenerator.java
After you execute the test, you should see that the token is new for every request; look at the
"newToken" : "2018-11-24T10-57-21-158"
in the test log below.
------ BDD: Scenario:Generate new Security Token and pass as custom header -------
--------- TEST-STEP-CORRELATION-ID: d175f3a7-055a-4e9f-8711-d4b0b286f98b ---------
*requestTimeStamp:2018-11-24T10:57:21.145
step:token_brewer
url:org.jsmart.zerocode.zerocodejavaexec.utils.TokenGenerator
method:generateNew
request:
"any_param_or_empty"
--------- TEST-STEP-CORRELATION-ID: d175f3a7-055a-4e9f-8711-d4b0b286f98b ---------
Response:
{
"newToken" : "2018-11-24T10-57-21-158"
}
*responseTimeStamp:2018-11-24T10:57:21.162
*Response delay:17.0 milli-secs
---------> Assertion: <----------
{
"newToken" : "$NOT.NULL"
}
-done-
--------- TEST-STEP-CORRELATION-ID: 73fe4b9a-f0ce-4479-a5e0-8fe4bb99828f ---------
*requestTimeStamp:2018-11-24T10:57:21.221
step:get_user_with_new_token
url:https://api.github.com:443/users/octocat
method:GET
request:
{
"headers" : {
"security_token" : "2018-11-24T10-57-21-158"
}
}
--------- TEST-STEP-CORRELATION-ID: 73fe4b9a-f0ce-4479-a5e0-8fe4bb99828f ---------
Response:
{
"status" : 200,
"headers" : {
"Date" : [ "Sat, 24 Nov 2018 10:57:22 GMT" ],
"Status" : [ "200 OK" ]
},
"body" : {
"login" : "octocat",
"id" : 583231
}
}
*responseTimeStamp:2018-11-24T10:57:22.206
*Response delay:985.0 milli-secs
---------> Assertion: <----------
{
"status" : 200,
"body" : {
"login" : "octocat"
}
}
-done-
Visit the Zerocode Documentation Site for all things.
-
User's Guide
-
Matchers
-
Zerocode Value Tokens
-
YAML DSL
-
Http Testing
-
Kafka Testing
- Introduction
- Produce, consume proto message
- Produce raw message
- Consume raw message
- Produce JSON message
- Consume JSON message
- Produce and consume XML message
- Kafka - consume the latest message or n latest messages
- Produce avro message
- Consume avro message
- KSQL in action
- Produce multiple records
- Produce from file
- Produce to a partition
- Produce and consume records with headers
- Produce n assert partition ack
- Comsume and dump to file
- commitSync vs commitAsync
- Overriding config inside a test
- Chosing String or Int or Avro Serializer
- Chosing String or Int or Avro Deserializer
- Attaching timestamp during load
- Default timestamp provided by Kafka
- Consume and assert avro schema metadata
- Error handling - produce via avro schema
- Sorting Kafka records consumed
-
DB Testing
-
Kotlin Testing
-
Performance Testing - Load and Stress
- Performance Testing - via awesome JUnit runners
- Load Vs Stress generation on target application
- Run a single test or a scenario in parallel
- Run multiple test scenarios in parallel - Production load simulation
- Dynamically change the payload for every request
- Analytics - Useful report(s) or statistics
-
Parameterized Testing
-
Docker
-
More+
-
Extensions
-
JUnit5 Jupiter Test
-
Questions And Answers(FAQ)
- What is Zerocode testing?
- SSL http https connections supported?
- How to assert array size Greater-Than Lesser-Than etc?
- How to invoke POST api?
- How to assert custom headers of the response?
- How to pass custom security token into the request header?
- When to use JUnit Suite runner and when Zerocode Package runner?
- How to execute DB SQL and assert?
- How to handle Http response other than utf-8 e.g. utf-16 or utf-32 ?
- Random Number Generator Placeholders Usages and Limits
- Automation tests for Zerocode lib itself
- Picking a leaf value from the array matching JSON Path
- Array assertions made easy, incl. size and element finder
-
Read Our Blogs
- Top 16 Open Source API Testing Tools For REST & SOAP Services - joecolantonio (Lists popular tools - Globally)
- OAuth2 Test Automation - DZone 2min Read
- Zero defect APIs - Build Pipe Line - Medium 10 min Read
- Develop ZeroDefect API's with ZeroCode! - Extreme Portal ( A must read for all developers and test engineers) 10min Read
- Performance testing using JUnit and maven - Codeproject 10 min Read
- REST API or SOAP End Point Testing - Codeproject 10min Read
- DZone- MuleSoft API Testing With Zerocode Test Framework - DZone 5min Read
- Testing need not be harder or slower, it should be easier and faster - DZone 5 min Read
- Kotlin Integration Testing simplified via Zerocode - Extreme portal 10 min Read
- and More...