简体   繁体   English

保证放心的cookie或授权标头保持最新

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

we using rest-assured in two situations where i can't find an automatic solution. 我们在无法找到自动解决方案的两种情况下使用了保证放心的方法。 i have a feeling i am missing something. 我有一种想念的感觉。

1#: 1#:

till now Rest-Assured was automatically updating the session value from the server. 到目前为止,Rest-Assured一直在自动从服务器更新会话值。 we recently moved to a new architecture that uses an load balancer. 我们最近转移到了使用负载平衡器的新架构。 so the server is returning additionally to the old one, a new cookie representing the load balancer. 因此服务器将另外返回到旧的cookie,即代表负载均衡器的新cookie。 i find my self getting the new cookie programmatically and updating the next requests. 我发现自己以编程方式获取新的Cookie并更新了下一个请求。 can rest assured do the automatically for me ? 可以放心为我自动吗?

2#: 2#:

other servers require the header "Authorization: Bearer yada.yada.yada". 其他服务器需要标头“授权:承载yada.yada.yada”。 to be renewed after each request. 在每次请求后进行续订。 Here also how can i tell rest-Assured to do that automatically for me ? 在这里我又如何告诉安心的人为我自动做到这一点?

thank you shay 谢谢你谢伊

I would recommend you to use AuthFilter to automatically provide auth header to your requests. 我建议您使用AuthFilter自动为您的请求提供auth标头。 But you still need to extract data - do it with an additional filter: 但是您仍然需要提取数据-使用其他过滤器可以做到这一点:

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;
        }
    }

}

So... what's happing here? 所以...这是什么?

First request will be made with header Testauth=default the second with Testauth=default_updated , and if you will add else one iteration it would be Testauth=default_updated_updated 第一个请求将使用标头Testauth=default ,第二个请求将使用Testauth=default_updated ,如果您要添加其他迭代,则将是Testauth=default_updated_updated

Actually GetAuthFilter should be a little bit difficult to respect body expectations, see implementation of io.restassured.filter.log.StatusCodeBasedLoggingFilter which extract response data and prints it. 实际上, GetAuthFilter应该有点难以遵守主体的期望,请参阅io.restassured.filter.log.StatusCodeBasedLoggingFilter实现,该实现提取响应数据并进行打印。

Also you may use FilterContext values storage to pass values between filters, external static variable is just to simplify example. 也可以使用FilterContext值存储在过滤器之间传递值,外部静态变量只是为了简化示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM