use of org.elasticsearch.test.rest.FakeRestChannel in project elasticsearch by elastic.
the class BaseRestHandlerTests method testMultipleUnconsumedParameters.
public void testMultipleUnconsumedParameters() throws Exception {
final AtomicBoolean executed = new AtomicBoolean();
BaseRestHandler handler = new BaseRestHandler(Settings.EMPTY) {
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
request.param("consumed");
return channel -> executed.set(true);
}
};
final HashMap<String, String> params = new HashMap<>();
params.put("consumed", randomAsciiOfLength(8));
params.put("unconsumed-first", randomAsciiOfLength(8));
params.put("unconsumed-second", randomAsciiOfLength(8));
RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> handler.handleRequest(request, channel, mock(NodeClient.class)));
assertThat(e, hasToString(containsString("request [/] contains unrecognized parameters: [unconsumed-first], [unconsumed-second]")));
assertFalse(executed.get());
}
use of org.elasticsearch.test.rest.FakeRestChannel in project elasticsearch by elastic.
the class BaseRestHandlerTests method testCatResponseParameters.
public void testCatResponseParameters() throws Exception {
final AtomicBoolean executed = new AtomicBoolean();
AbstractCatAction handler = new AbstractCatAction(Settings.EMPTY) {
@Override
protected RestChannelConsumer doCatRequest(RestRequest request, NodeClient client) {
return channel -> executed.set(true);
}
@Override
protected void documentation(StringBuilder sb) {
}
@Override
protected Table getTableWithHeader(RestRequest request) {
return null;
}
};
final HashMap<String, String> params = new HashMap<>();
params.put("format", randomAsciiOfLength(8));
params.put("h", randomAsciiOfLength(8));
params.put("v", randomAsciiOfLength(8));
params.put("ts", randomAsciiOfLength(8));
params.put("pri", randomAsciiOfLength(8));
params.put("bytes", randomAsciiOfLength(8));
params.put("size", randomAsciiOfLength(8));
params.put("time", randomAsciiOfLength(8));
RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
handler.handleRequest(request, channel, mock(NodeClient.class));
assertTrue(executed.get());
}
use of org.elasticsearch.test.rest.FakeRestChannel 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.FakeRestChannel 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.FakeRestChannel 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());
}
Aggregations