簡體   English   中英

保證放心的cookie或授權標頭保持最新

[英]Rest-Assured keep cookie OR Authorization header up to date

我們在無法找到自動解決方案的兩種情況下使用了保證放心的方法。 我有一種想念的感覺。

1#:

到目前為止,Rest-Assured一直在自動從服務器更新會話值。 我們最近轉移到了使用負載平衡器的新架構。 因此服務器將另外返回到舊的cookie,即代表負載均衡器的新cookie。 我發現自己以編程方式獲取新的Cookie並更新了下一個請求。 可以放心為我自動嗎?

2#:

其他服務器需要標頭“授權:承載yada.yada.yada”。 在每次請求后進行續訂。 在這里我又如何告訴安心的人為我自動做到這一點?

謝謝你謝伊

我建議您使用AuthFilter自動為您的請求提供auth標頭。 但是您仍然需要提取數據-使用其他過濾器可以做到這一點:

import io.restassured.RestAssured;
import io.restassured.builder.ResponseBuilder;
import io.restassured.filter.FilterContext;
import io.restassured.filter.OrderedFilter;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
import org.testng.annotations.Test;

import static io.restassured.RestAssured.given;

public class ReuseRestAssuredResponse {

    private static String authVal = "default";

    @Test
    public void sampleTest() {
        RestAssured.filters(new SetAuthFilter(), new GetAuthFilter());
        given()
                .log().all()
                .when()
                .get("https://httpbin.org/get")
                .then()
                .log().all();

        given()
                .log().all()
                .when()
                .get("https://httpbin.org/get")
                .then()
                .log().all();
    }

    class SetAuthFilter implements OrderedFilter {

        @Override
        public Response filter(FilterableRequestSpecification filterableRequestSpecification, FilterableResponseSpecification filterableResponseSpecification, FilterContext filterContext) {
            filterableRequestSpecification.header("Testauth", authVal);
            return filterContext.next(filterableRequestSpecification, filterableResponseSpecification);
        }

        @Override
        public int getOrder() {
            return DEFAULT_PRECEDENCE - 1;
        }
    }

    class GetAuthFilter implements OrderedFilter {

        @Override
        public Response filter(FilterableRequestSpecification filterableRequestSpecification, FilterableResponseSpecification filterableResponseSpecification, FilterContext filterContext) {
            Response response = filterContext.next(filterableRequestSpecification, filterableResponseSpecification);
            authVal = response.body().path("headers.Testauth") + "_updated";
            return response;
        }

        @Override
        public int getOrder() {
            return DEFAULT_PRECEDENCE;
        }
    }

}

所以...這是什么?

第一個請求將使用標頭Testauth=default ,第二個請求將使用Testauth=default_updated ,如果您要添加其他迭代,則將是Testauth=default_updated_updated

實際上, GetAuthFilter應該有點難以遵守主體的期望,請參閱io.restassured.filter.log.StatusCodeBasedLoggingFilter實現,該實現提取響應數據並進行打印。

也可以使用FilterContext值存儲在過濾器之間傳遞值,外部靜態變量只是為了簡化示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM