com.vtence.molecule.testing.http.HttpResponseThat Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of molecule Show documentation
Show all versions of molecule Show documentation
A web micro-framework for Java
The newest version!
package com.vtence.molecule.testing.http;
import com.vtence.molecule.http.ContentType;
import com.vtence.molecule.testing.CharsetDetector;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
import java.net.http.HttpResponse;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import static org.hamcrest.Matchers.equalTo;
public class HttpResponseThat {
public static Matcher> contentEncodedAs(Charset charset) {
return contentEncodedWithCharset(equalTo(charset));
}
public static Matcher> contentEncodedWithCharset(Matcher matching) {
return new FeatureMatcher<>(matching, "content encoded as", "encoding") {
@Override
protected Charset featureValueOf(HttpResponse actual) {
return Charset.forName(CharsetDetector.detectCharsetOf(actual.body().getBytes(encodingOf(actual))));
}
private Charset encodingOf(HttpResponse> response) {
return contentType(response).map(ContentType::parse)
.map(ContentType::charset)
.orElse(StandardCharsets.ISO_8859_1);
}
private Optional contentType(HttpResponse> response) {
return response.headers().firstValue("Content-Type");
}
};
}
}