All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lv.ctco.cukes.graphql.facade.GQLResponseFacade Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package lv.ctco.cukes.graphql.facade;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import lv.ctco.cukes.core.CukesOptions;
import lv.ctco.cukes.core.internal.context.GlobalWorldFacade;
import lv.ctco.cukes.core.internal.context.InflateContext;
import lv.ctco.cukes.http.HttpMethod;
import lv.ctco.cukes.http.extension.CukesHttpPlugin;

import java.util.Set;
import java.util.concurrent.Callable;

@Singleton
@InflateContext
public class GQLResponseFacade {

    @Inject
    GQLRequestFacade requestFacade;
    @Inject
    GlobalWorldFacade world;
    @Inject
    Set pluginSet;

    private Response response;

    public void doRequest() throws Exception {
        requestFacade.body(requestFacade.getGraphQLRequest());
        doRequest(HttpMethod.POST).call();

        requestFacade.initNewSpecification();
    }

    private Callable doRequest(final HttpMethod method) {
        return () -> {
            final RequestSpecification requestSpec = requestFacade.value();

            for (CukesHttpPlugin plugin : pluginSet) {
                plugin.beforeRequest(requestSpec);
            }

            response = method.doRequest(requestSpec);

            for (CukesHttpPlugin plugin : pluginSet) {
                plugin.afterRequest(response);
            }
            cacheHeaders(response);
            return response;
        };
    }

    private void clearOldHeaders() {
        Set keys = world.getKeysStartingWith(CukesOptions.HEADER_PREFIX);
        for (String key : keys) {
            world.remove(key);
        }
    }

    private void cacheHeaders(Response response) {
        clearOldHeaders();
        Headers headers = response.getHeaders();
        for (Header header : headers) {
            String headerName = CukesOptions.HEADER_PREFIX + header.getName();
            world.put(headerName, header.getValue());
        }
    }

    public Response response() {
        return response;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy