use of org.elasticsearch.test.rest.FakeRestRequest in project elasticsearch by elastic.
the class RestBuilderListenerTests method testXContentBuilderClosedInBuildResponse.
public void testXContentBuilderClosedInBuildResponse() throws Exception {
AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>();
RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {
@Override
public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
builderAtomicReference.set(builder);
builder.close();
return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
}
};
builderListener.buildResponse(Empty.INSTANCE);
assertNotNull(builderAtomicReference.get());
assertTrue(builderAtomicReference.get().generator().isClosed());
}
use of org.elasticsearch.test.rest.FakeRestRequest in project elasticsearch by elastic.
the class RestBuilderListenerTests method testXContentBuilderNotClosedInBuildResponseAssertionsEnabled.
public void testXContentBuilderNotClosedInBuildResponseAssertionsEnabled() throws Exception {
assumeTrue("tests are not being run with assertions", RestBuilderListener.class.desiredAssertionStatus());
RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {
@Override
public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
}
};
AssertionError error = expectThrows(AssertionError.class, () -> builderListener.buildResponse(Empty.INSTANCE));
assertEquals("callers should ensure the XContentBuilder is closed themselves", error.getMessage());
}
use of org.elasticsearch.test.rest.FakeRestRequest in project elasticsearch by elastic.
the class SettingsFilterTests method testFiltering.
private void testFiltering(Settings source, Settings filtered, String... patterns) throws IOException {
SettingsFilter settingsFilter = new SettingsFilter(Settings.EMPTY, Arrays.asList(patterns));
// Test using direct filtering
Settings filteredSettings = settingsFilter.filter(source);
assertThat(filteredSettings.getAsMap().entrySet(), equalTo(filtered.getAsMap().entrySet()));
// Test using toXContent filtering
RestRequest request = new FakeRestRequest();
settingsFilter.addFilterSettingParams(request);
XContentBuilder xContentBuilder = XContentBuilder.builder(JsonXContent.jsonXContent);
xContentBuilder.startObject();
source.toXContent(xContentBuilder, request);
xContentBuilder.endObject();
String filteredSettingsString = xContentBuilder.string();
filteredSettings = Settings.builder().loadFromSource(filteredSettingsString, xContentBuilder.contentType()).build();
assertThat(filteredSettings.getAsMap().entrySet(), equalTo(filtered.getAsMap().entrySet()));
}
use of org.elasticsearch.test.rest.FakeRestRequest in project elasticsearch by elastic.
the class RestBuilderListenerTests method testXContentBuilderNotClosedInBuildResponseAssertionsDisabled.
public void testXContentBuilderNotClosedInBuildResponseAssertionsDisabled() throws Exception {
AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>();
RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {
@Override
public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
builderAtomicReference.set(builder);
return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
}
@Override
boolean assertBuilderClosed(XContentBuilder xContentBuilder) {
// don't check the actual builder being closed so we can test auto close
return true;
}
};
builderListener.buildResponse(Empty.INSTANCE);
assertNotNull(builderAtomicReference.get());
assertTrue(builderAtomicReference.get().generator().isClosed());
}
use of org.elasticsearch.test.rest.FakeRestRequest in project elasticsearch by elastic.
the class RestMainActionTests method testHeadResponse.
public void testHeadResponse() throws Exception {
final String nodeName = "node1";
final ClusterName clusterName = new ClusterName("cluster1");
final String clusterUUID = randomAsciiOfLengthBetween(10, 20);
final boolean available = randomBoolean();
final RestStatus expectedStatus = available ? RestStatus.OK : RestStatus.SERVICE_UNAVAILABLE;
final Version version = Version.CURRENT;
final Build build = Build.CURRENT;
final MainResponse mainResponse = new MainResponse(nodeName, version, clusterName, clusterUUID, build, available);
XContentBuilder builder = JsonXContent.contentBuilder();
RestRequest restRequest = new FakeRestRequest() {
@Override
public Method method() {
return Method.HEAD;
}
};
BytesRestResponse response = RestMainAction.convertMainResponse(mainResponse, restRequest, builder);
assertNotNull(response);
assertEquals(expectedStatus, response.status());
// the empty responses are handled in the HTTP layer so we do
// not assert on them here
}
Aggregations