I'm creating requests dynamically. Once I have a token, I add it as a header. The problem arises when I try hitting the endpoint multiple times. I end up adding multiple headers with the exact same token. Is there a way I could look for available headers in a request and add the header only if it is not present?
public class PutUpdateCampaign extends PingPongRequest {
@Steps
GetToken getToken;
@Step
private String getToken() {
return getToken.usingCredentials(AccessLevel.CREATE_CAMPAIGN).jsonPath().get("token");
}
private RequestSpecification getRequestSpecification() {
QueryableRequestSpecification query = SpecificationQuerier.query(requestSpecification);
Headers reqHeaders = query.getHeaders();
if (reqHeaders.hasHeaderWithName("Authorization")) {
return requestSpecification;
}
return requestSpecification.headers("Authorization", "Token " + getToken());
}
@Override
public <UpdateCampaign> Response withParameters(UpdateCampaign details) {
return requestSpecification
.spec(getRequestSpecification())
.body(details)
.log().all()
.put( WebServiceEndPoints.CREATECAMPAIGN.getUrl() + "/" + UpdateCampaignStepDefinitions.createdCampaignId)
.then()
.log().all()
.extract().response();
}
}
PingPongRequest:
public abstract class PingPongRequest {
public RequestSpecification requestSpecification = SerenityRest.given()
.header("Accept", "application/json")
.header("Content-Type", "application/json");
@Step("with parameters")
protected abstract <T> Response withParameters(T details);
}
If I call withParameters twice in a test, I get two Authorization headers.
Headers: Accept=application/json
Authorization=Token ddffgg
Authorization=Token ddffgg
The code currently results in
groovy.lang.MissingMethodException: No signature of method: static io.restassured.internal.SpecificationMerger.merge() is applicable for argument types: (io.restassured.internal.RequestSpecificationImpl, net.serenitybdd.rest.decorators.request.RequestSpecificationDecorated$ByteBuddy$3LuBUN60) values: [io.restassured.internal.RequestSpecificationImpl@2da27453, net.serenitybdd.rest.decorators.request.RequestSpecificationDecorated$ByteBuddy$3LuBUN60@2da27453]
Any recommendations for better structuring of my approach are always welcome.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…