dev.galasa.extensions.mocks.BaseHttpInteraction Maven / Gradle / Ivy
/*
* 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;
}
}