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

lv.ctco.cukesrest.internal.matchers.StatusCodeMatcher Maven / Gradle / Ivy

The newest version!
package lv.ctco.cukesrest.internal.matchers;

import io.restassured.response.Response;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import static java.lang.String.format;

public class StatusCodeMatcher extends TypeSafeDiagnosingMatcher {

    private final Integer expectedStatusCode;
    private final Response response;
    private final boolean appendBody;
    private final Integer maxSize;

    public StatusCodeMatcher(Integer expectedStatusCode, Response response, boolean appendBody, Integer maxSize) {
        this.expectedStatusCode = expectedStatusCode;
        this.response = response;
        this.appendBody = appendBody;
        this.maxSize = maxSize;
    }

    @Override
    protected boolean matchesSafely(Integer statusCode, Description description) {
        description.appendText(format("was \"%d\"", statusCode));

        if (appendBody) {
            final String body = response.body().asString();
            final int size = body.length();

            if (response.getContentType().equals("application/octet-stream")) {
                description.appendText(" with body ");
            } else if (maxSize != null && size > maxSize) {
                description.appendText(" with body ");
            } else {
                description.appendText(format(" with body:\n\"\"\"\n%s\n\"\"\"", body));
            }
        }

        return expectedStatusCode.equals(statusCode);
    }

    @Override
    public void describeTo(Description description) {
        description.appendText(format("\"%d\"", expectedStatusCode));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy