简体   繁体   中英

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#:

till now Rest-Assured was automatically updating the session value from the server. 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. i find my self getting the new cookie programmatically and updating the next requests. can rest assured do the automatically for me ?

2#:

other servers require the header "Authorization: Bearer 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. 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

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.

Also you may use FilterContext values storage to pass values between filters, external static variable is just to simplify example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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