Skip to content

Commit

Permalink
Respect accept header on no handler (#30383)
Browse files Browse the repository at this point in the history
Today when processing a request for a URL path for which we can not find
a handler we send back a plain-text response. Yet, we have the accept
header in our hand and can respect the accepted media type of the
request. This commit addresses this.
  • Loading branch information
jasontedor committed May 5, 2018
1 parent 660c050 commit 175ae5e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ Engine::

Ingest::
* Don't allow referencing the pattern bank name in the pattern bank {pull}29295[#29295] (issue: {issue}29257[#29257])
Respect accept header on requests with no handler ({pull}30383[#30383])
Rollup::
* Validate timezone in range queries to ensure they match the selected job when
searching ({pull}30338[#30338])

Java High Level REST Client::
* Bulk processor#awaitClose to close scheduler {pull}29263[#29263]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.http;

import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;

import java.io.IOException;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

public class NoHandlerIT extends HttpSmokeTestCase {

public void testNoHandlerRespectsAcceptHeader() throws IOException {
runTestNoHandlerRespectsAcceptHeader(
"application/json",
"application/json; charset=UTF-8",
"\"error\":\"no handler found for uri [/foo/bar/baz/qux/quux] and method [GET]\"");
runTestNoHandlerRespectsAcceptHeader(
"application/yaml",
"application/yaml",
"error: \"no handler found for uri [/foo/bar/baz/qux/quux] and method [GET]\"");
}

private void runTestNoHandlerRespectsAcceptHeader(
final String accept, final String contentType, final String expect) throws IOException {
final ResponseException e =
expectThrows(
ResponseException.class,
() -> getRestClient().performRequest("GET", "/foo/bar/baz/qux/quux", new BasicHeader("Accept", accept)));

final Response response = e.getResponse();
assertThat(response.getHeader("Content-Type"), equalTo(contentType));
assertThat(EntityUtils.toString(e.getResponse().getEntity()), containsString(expect));
assertThat(response.getStatusLine().getStatusCode(), is(400));
}

}
12 changes: 9 additions & 3 deletions server/src/main/java/org/elasticsearch/rest/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,15 @@ private void handleOptionsRequest(RestRequest request, RestChannel channel, Set<
* Handle a requests with no candidate handlers (return a 400 Bad Request
* error).
*/
private void handleBadRequest(RestRequest request, RestChannel channel) {
channel.sendResponse(new BytesRestResponse(BAD_REQUEST,
"No handler found for uri [" + request.uri() + "] and method [" + request.method() + "]"));
private void handleBadRequest(RestRequest request, RestChannel channel) throws IOException {
try (XContentBuilder builder = channel.newErrorBuilder()) {
builder.startObject();
{
builder.field("error", "no handler found for uri [" + request.uri() + "] and method [" + request.method() + "]");
}
builder.endObject();
channel.sendResponse(new BytesRestResponse(BAD_REQUEST, builder));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public void testActionsFail() throws Exception {
ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest("put",
MachineLearning.BASE_PATH + "anomaly_detectors/foo", Collections.emptyMap(),
new StringEntity(Strings.toString(xContentBuilder), ContentType.APPLICATION_JSON)));
assertThat(exception.getMessage(), containsString("No handler found for uri [/_xpack/ml/anomaly_detectors/foo] and method [PUT]"));
assertThat(exception.getMessage(), containsString("no handler found for uri [/_xpack/ml/anomaly_detectors/foo] and method [PUT]"));
}
}

0 comments on commit 175ae5e

Please sign in to comment.