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

no.unit.nva.testutils.HttpRequestUtils Maven / Gradle / Ivy

The newest version!
package no.unit.nva.testutils;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import nva.commons.core.JacocoGenerated;
import org.apache.http.HttpStatus;
import org.mockito.invocation.InvocationOnMock;

@JacocoGenerated
public class HttpRequestUtils {

    public CompletableFuture> echoRequestAsFuture(InvocationOnMock invocation) {
        return CompletableFuture.completedFuture(echoRequest(invocation));
    }

    public HttpResponse echoRequest(InvocationOnMock invocation) {
        HttpRequest request = invocation.getArgument(0);
        String body = RequestBodyReader.requestBody(request);
        HttpHeaders headers = mockHeaders(request);
        return mockResponse(body, headers);
    }

    protected HttpHeaders mockHeaders(HttpRequest request) {
        return request.headers();
    }

    @SuppressWarnings("unchecked")
    private HttpResponse mockResponse(String body, HttpHeaders headers) {
        HttpResponse response = mock(HttpResponse.class);
        when(response.statusCode()).thenReturn(HttpStatus.SC_OK);
        when(response.body()).thenReturn(body);
        when(response.headers()).thenReturn(headers);
        return response;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy