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

dev.galasa.extensions.mocks.BaseHttpInteraction Maven / Gradle / Ivy

There is a newer version: 0.37.0
Show newest version
/*
 * Copyright contributors to the Galasa project
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package dev.galasa.extensions.mocks;

import static org.assertj.core.api.Assertions.*;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpStatus;

import dev.galasa.framework.spi.utils.GalasaGson;

public abstract class BaseHttpInteraction implements HttpInteraction {

    private GalasaGson gson = new GalasaGson();

    private String expectedBaseUri ;
    private String returnedDocument;

    private String responsePayload = "";
    private int responseStatusCode = HttpStatus.SC_OK;

    public BaseHttpInteraction(String expectedBaseUri, String returnedDocument ) {
        this.expectedBaseUri = expectedBaseUri;
        this.returnedDocument = returnedDocument;
    }

    public BaseHttpInteraction(String expectedBaseUri, int responseStatusCode) {
        this.expectedBaseUri = expectedBaseUri;
        this.responseStatusCode = responseStatusCode;
    }

    public String getExpectedBaseUri() {
        return this.expectedBaseUri;
    }

    public String getReturnedDocument() {
        return this.returnedDocument;
    }

    public String getExpectedHttpContentType() {
        return "application/json";
    }

    public void setResponsePayload(Object responsePayload) {
        this.responsePayload = gson.toJson(responsePayload);
    }

    @Override
    public void validateRequest(HttpHost host, HttpRequest request) throws RuntimeException {

        String uri = request.getRequestLine().getUri();
        assertThat(uri).isEqualTo(this.expectedBaseUri);

        validateRequestContentType(request);
    }

    public void validateRequestContentType(HttpRequest request) {
        assertThat(request.containsHeader("Content-Type")).as("Missing Content-Type header!").isTrue();
        assertThat(request.getHeaders("Content-Type")[0].getValue()).isEqualTo(getExpectedHttpContentType());
    }

    @Override
    public MockCloseableHttpResponse getResponse() {
        HttpEntity entity = new MockHttpEntity(responsePayload);

        MockCloseableHttpResponse response = new MockCloseableHttpResponse();

        MockStatusLine statusLine = new MockStatusLine();
        statusLine.setStatusCode(responseStatusCode);
        response.setStatusLine(statusLine);
        response.setEntity(entity);

        return response;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy