From fb097d52032cc8dd379c9a80fecfdf90a56f3872 Mon Sep 17 00:00:00 2001 From: Peter Nied Date: Wed, 15 Nov 2023 20:34:36 +0000 Subject: [PATCH] Validate content type of responses Signed-off-by: Peter Nied --- .../framework/cluster/TestRestClient.java | 25 ++++++++++++++++ .../security/test/helper/rest/RestHelper.java | 29 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/integrationTest/java/org/opensearch/test/framework/cluster/TestRestClient.java b/src/integrationTest/java/org/opensearch/test/framework/cluster/TestRestClient.java index e38ef949cb..fa5570e9f2 100644 --- a/src/integrationTest/java/org/opensearch/test/framework/cluster/TestRestClient.java +++ b/src/integrationTest/java/org/opensearch/test/framework/cluster/TestRestClient.java @@ -65,6 +65,7 @@ import org.apache.hc.core5.http.io.entity.StringEntity; import org.apache.hc.core5.http.message.BasicHeader; import org.apache.hc.core5.net.URIBuilder; +import org.apache.http.HttpHeaders; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -76,7 +77,9 @@ import static java.lang.String.format; import static java.util.Objects.requireNonNull; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.emptyOrNullString; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; /** @@ -284,7 +287,29 @@ public HttpResponse(CloseableHttpResponse inner) throws IllegalStateException, I this.header = inner.getHeaders(); this.statusCode = inner.getCode(); this.statusReason = inner.getReasonPhrase(); + inner.close(); + + if (this.body.length() != 0) { + verifyContentType(); + } + } + + private void verifyContentType() { + final String contentType = this.getHeader(HttpHeaders.CONTENT_TYPE).getValue(); + if (contentType.equals("application/json")) { + assertThat("Response body should not have been empty", body, emptyOrNullString()); + assertThat("Response body format was not json, body: " + body, body.charAt(0), equalTo("{")); + } else { + if (body.length() != 0) { + assertThat( + "Response body format was json, whereas content-type was " + contentType + ", body: " + body, + body.charAt(0), + not(equalTo("{")) + ); + } + } + } public String getContentType() { diff --git a/src/test/java/org/opensearch/security/test/helper/rest/RestHelper.java b/src/test/java/org/opensearch/security/test/helper/rest/RestHelper.java index 43e7afc559..b9fd2b1a19 100644 --- a/src/test/java/org/opensearch/security/test/helper/rest/RestHelper.java +++ b/src/test/java/org/opensearch/security/test/helper/rest/RestHelper.java @@ -90,6 +90,11 @@ import org.opensearch.security.test.helper.cluster.ClusterInfo; import org.opensearch.security.test.helper.file.FileHelper; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.emptyOrNullString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; + public class RestHelper { protected final Logger log = LogManager.getLogger(RestHelper.class); @@ -402,6 +407,30 @@ public HttpResponse(SimpleHttpResponse inner) throws IllegalStateException, IOEx this.statusCode = inner.getCode(); this.statusReason = inner.getReasonPhrase(); this.protocolVersion = inner.getVersion(); + + if (this.body.length() != 0) { + verifyBodyContentType(); + } + } + + private void verifyBodyContentType() { + final String contentType = this.getHeaders() + .stream() + .filter(h -> h.getName() == HttpHeaders.CONTENT_TYPE) + .map(Header::getValue) + .findFirst() + .orElseThrow(() -> new RuntimeException("No content type found")); + + if (contentType.equals("application/json")) { + assertThat("Response body should not have been empty", body, emptyOrNullString()); + assertThat("Response body format was not json, body: " + body, body.charAt(0), equalTo("{")); + } else { + assertThat( + "Response body format was json, whereas content-type was " + contentType + ", body: " + body, + body.charAt(0), + not(equalTo("{")) + ); + } } public String getContentType() {