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

com.github.ngeor.yak4j.ResponseEntityAssert Maven / Gradle / Ivy

package com.github.ngeor.yak4j;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;

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

/**
 * Assertion DSL for {@link ResponseEntity}.
 *
 * @param  The body of the response.
 */
@SuppressWarnings({"unused", "WeakerAccess", "UnusedReturnValue"})
public class ResponseEntityAssert extends AbstractAssert, ResponseEntity> {
    ResponseEntityAssert(ResponseEntity responseEntity) {
        super(responseEntity, ResponseEntityAssert.class);
    }

    /**
     * Verifies that the response has the given HTTP status code.
     *
     * @param httpStatus The expected HTTP status code.
     * @return This instance.
     */
    public ResponseEntityAssert hasStatus(HttpStatus httpStatus) {
        isNotNull();
        HttpStatus actualStatusCode = actual.getStatusCode();
        Assertions.assertThat(actualStatusCode)
            .withFailMessage("Expecting response status code to be %s but was %s", httpStatus, actualStatusCode)
            .isEqualTo(httpStatus);
        return this;
    }

    public ResponseEntityAssert isOk() {
        return hasStatus(HttpStatus.OK);
    }

    public ResponseEntityAssert isBadRequest() {
        return hasStatus(HttpStatus.BAD_REQUEST);
    }

    public ResponseEntityAssert isForbidden() {
        return hasStatus(HttpStatus.FORBIDDEN);
    }

    public ResponseEntityAssert isInternalServerError() {
        return hasStatus(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * Verifies that the response contains the given object as body.
     *
     * @param body The expected body.
     * @return This instance.
     */
    public ResponseEntityAssert hasBody(T body) {
        isNotNull();
        T actualBody = actual.getBody();
        Assertions.assertThat(actualBody)
            .withFailMessage("Expecting response entity body to be %s but was %s", body, actualBody)
            .isNotNull()
            .isEqualTo(body);
        return this;
    }

    /**
     * Verify that the response entity always has the correct CORS headers.
     * @return This instance.
     */
    @SuppressWarnings("checkstyle:LineLength")
    public ResponseEntityAssert hasCorsHeaders() {
        HttpHeaders headers = actual.getHeaders();
        assertThat(headers)
            .withFailMessage("Expecting non-null headers")
            .isNotNull();

        String origin = headers.getAccessControlAllowOrigin();
        assertThat(origin)
            .withFailMessage("Expecting Access-Control-Allow-Origin to be *, was %s", origin)
            .isEqualTo("*");

        List allowHeaders = headers.getAccessControlAllowHeaders();
        assertThat(allowHeaders)
            .withFailMessage("Expecting Access-Control-Allow-Headers to be Authorization, Content-Type but was %s", allowHeaders)
            .containsExactly("Authorization", "Content-Type");

        List methods = headers.getAccessControlAllowMethods();
        assertThat(methods)
            .withFailMessage("Expecting Access-Control-Allow-Methods to be GET, POST, DELETE, PUT but was %s", methods)
            .containsExactly(HttpMethod.GET, HttpMethod.POST, HttpMethod.DELETE, HttpMethod.PUT);
        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy