Skip to content

Commit

Permalink
[Backport 2.x] Validate content type of responses (#3719) (#3759)
Browse files Browse the repository at this point in the history
Backports c30c084 from #3719

Signed-off-by: Peter Nied <petern@amazon.com>
  • Loading branch information
peternied committed Nov 22, 2023
1 parent cf11205 commit 987c2a2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand Down Expand Up @@ -78,6 +79,7 @@
import static java.util.Objects.requireNonNull;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;

/**
Expand Down Expand Up @@ -294,6 +296,24 @@ public HttpResponse(CloseableHttpResponse inner) throws IllegalStateException, I
this.statusCode = inner.getStatusLine().getStatusCode();
this.statusReason = inner.getStatusLine().getReasonPhrase();
inner.close();

if (this.body.length() != 0) {
verifyContentType();
}
}

private void verifyContentType() {
final String contentType = this.getHeader(HttpHeaders.CONTENT_TYPE).getValue();
if (contentType.contains("application/json")) {
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
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.equalTo;
import static org.hamcrest.Matchers.not;

public class RestHelper {

protected final Logger log = LogManager.getLogger(RestHelper.class);
Expand Down Expand Up @@ -313,7 +317,29 @@ public HttpResponse(CloseableHttpResponse inner) throws IllegalStateException, I
this.header = inner.getAllHeaders();
this.statusCode = inner.getStatusLine().getStatusCode();
this.statusReason = inner.getStatusLine().getReasonPhrase();
inner.close();

if (this.body.length() != 0) {
verifyBodyContentType();
}
}

private void verifyBodyContentType() {
final String contentType = this.getHeaders()
.stream()
.filter(h -> HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(h.getName()))
.map(Header::getValue)
.findFirst()
.orElseThrow(() -> new RuntimeException("No content type found. Headers:\n" + getHeaders() + "\n\nBody:\n" + body));

if (contentType.contains("application/json")) {
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() {
Expand Down

0 comments on commit 987c2a2

Please sign in to comment.