Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

java - How do I ensure that I add a header only if it is not present in Rest Assured?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...