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

clean tests #10

Merged
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 @@ -20,6 +20,7 @@

import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestResponse;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -45,8 +46,8 @@ public void testSecurityResponseHasSingleContentType() {
@Test
public void testSecurityResponseMultipleContentTypesUsesPassed() {
final SecurityResponse response = new SecurityResponse(HttpStatus.SC_OK, null, "foo bar", XContentType.JSON.mediaType());
response.addHeader(HttpHeaders.CONTENT_TYPE, "plain/text");
assertThat(response.getHeaders().get("Content-Type"), equalTo(List.of("plain/text")));
response.addHeader(HttpHeaders.CONTENT_TYPE, BytesRestResponse.TEXT_CONTENT_TYPE);
assertThat(response.getHeaders().get("Content-Type"), equalTo(List.of(BytesRestResponse.TEXT_CONTENT_TYPE)));
final RestResponse restResponse = response.asRestResponse();
assertThat(restResponse.contentType(), equalTo(XContentType.JSON.mediaType()));
assertThat(restResponse.status(), equalTo(RestStatus.OK));
Expand All @@ -59,8 +60,8 @@ public void testSecurityResponseMultipleContentTypesUsesPassed() {
public void testSecurityResponseDefaultContentTypeIsJson() {
final SecurityResponse response = new SecurityResponse(HttpStatus.SC_OK, null, "foo bar");
final RestResponse restResponse = response.asRestResponse();
assertThat(restResponse.contentType(), equalTo(XContentType.JSON.mediaType()));
assertThat(restResponse.status(), equalTo(RestStatus.OK)); // This fails because it is a text/plain but we should pick one default type and stick to it IMO
assertThat(restResponse.contentType(), equalTo(BytesRestResponse.TEXT_CONTENT_TYPE));
assertThat(restResponse.status(), equalTo(RestStatus.OK));
}

/**
Expand All @@ -71,7 +72,7 @@ public void testSecurityResponseSetHeaderContentTypeDoesNothing() {
final SecurityResponse response = new SecurityResponse(HttpStatus.SC_OK, null, "foo bar");
response.addHeader(HttpHeaders.CONTENT_TYPE, XContentType.JSON.mediaType());
final RestResponse restResponse = response.asRestResponse();
assertThat(restResponse.contentType(), equalTo("text/plain; charset=UTF-8"));
assertThat(restResponse.contentType(), equalTo(BytesRestResponse.TEXT_CONTENT_TYPE));
assertThat(restResponse.status(), equalTo(RestStatus.OK));
}

Expand All @@ -81,10 +82,10 @@ public void testSecurityResponseSetHeaderContentTypeDoesNothing() {
@Test
public void testSecurityResponseAddMultipleContentTypeHeaders() {
final SecurityResponse response = new SecurityResponse(HttpStatus.SC_OK, null, "foo bar", XContentType.JSON.mediaType());
response.addHeader(HttpHeaders.CONTENT_TYPE, "plain/text");
assertThat(response.getHeaders().get("Content-Type"), equalTo(List.of("plain/text")));
response.addHeader(HttpHeaders.CONTENT_TYPE, BytesRestResponse.TEXT_CONTENT_TYPE);
assertThat(response.getHeaders().get("Content-Type"), equalTo(List.of(BytesRestResponse.TEXT_CONTENT_TYPE)));
response.addHeader(HttpHeaders.CONTENT_TYPE, "newContentType");
assertThat(response.getHeaders().get("Content-Type"), equalTo(List.of("plain/text", "newContentType")));
assertThat(response.getHeaders().get("Content-Type"), equalTo(List.of(BytesRestResponse.TEXT_CONTENT_TYPE, "newContentType")));
final RestResponse restResponse = response.asRestResponse();
assertThat(restResponse.status(), equalTo(RestStatus.OK));
}
Expand All @@ -108,7 +109,7 @@ public void testSecurityResponseContentTypeInConstructorHeader() {
final SecurityResponse response = new SecurityResponse(HttpStatus.SC_OK, Map.of("Content-Type", "testType"), "foo bar");
assertThat(response.getHeaders().get("Content-Type"), equalTo(List.of("testType")));
final RestResponse restResponse = response.asRestResponse();
assertThat(restResponse.contentType(), equalTo(XContentType.JSON.mediaType()));
assertThat(restResponse.contentType(), equalTo(BytesRestResponse.TEXT_CONTENT_TYPE));
assertThat(restResponse.status(), equalTo(RestStatus.OK));
}

Expand All @@ -132,7 +133,7 @@ public void testSecurityResponseUnauthorizedRequestWithPlainTextContentType(){
final SecurityResponse response = new SecurityResponse(HttpStatus.SC_UNAUTHORIZED, null, "foo bar");
response.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
final RestResponse restResponse = response.asRestResponse();
assertThat(restResponse.contentType(), equalTo("text/plain; charset=UTF-8"));
assertThat(restResponse.contentType(), equalTo(BytesRestResponse.TEXT_CONTENT_TYPE));
assertThat(restResponse.status(), equalTo(RestStatus.UNAUTHORIZED));
}

Expand All @@ -144,7 +145,7 @@ public void testSecurityResponseForbiddenRequestWithPlainTextContentType(){
final SecurityResponse response = new SecurityResponse(HttpStatus.SC_FORBIDDEN, null, "foo bar");
response.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
final RestResponse restResponse = response.asRestResponse();
assertThat(restResponse.contentType(), equalTo("text/plain; charset=UTF-8"));
assertThat(restResponse.contentType(), equalTo(BytesRestResponse.TEXT_CONTENT_TYPE));
assertThat(restResponse.status(), equalTo(RestStatus.FORBIDDEN));
}
}
Loading