Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate content type of responses #3719

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -77,6 +78,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 @@ -284,7 +286,26 @@ 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() {
peternied marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -90,6 +90,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 @@ -402,6 +406,29 @@ 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 -> 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
Loading