Support content type `application/x-ndjson` in DeprecationRestHandler (#36025)

org.elasticsearch.rest.RestController#hasContentType checks to see if the
RestHandler supports the `application/x-ndjson` Content-Type. DeprecationRestHandler
is a wrapper around the real RestHandler, and prior to this change
would always return `false` due to the interface's default supportsContentStream().
This prevents API's that use multi-line JSON from properly being deprecated
resulting in an HTTP 406 error.

This change ensures that the DeprecationRestHandler honors the
supportsContentStream() of the wrapped RestHandler.

Relates to #35958
This commit is contained in:
Jake Landis 2018-11-29 11:45:45 -06:00 committed by GitHub
parent 95d9cefea7
commit f8636e58f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View File

@ -62,6 +62,11 @@ public class DeprecationRestHandler implements RestHandler {
handler.handleRequest(request, channel, client);
}
@Override
public boolean supportsContentStream() {
return handler.supportsContentStream();
}
/**
* This does a very basic pass at validating that a header's value contains only expected characters according to RFC-5987, and those
* that it references.

View File

@ -24,22 +24,30 @@ import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;
import org.mockito.InOrder;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Tests {@link DeprecationRestHandler}.
*/
public class DeprecationRestHandlerTests extends ESTestCase {
private final RestHandler handler = mock(RestHandler.class);
private RestHandler handler;
/**
* Note: Headers should only use US ASCII (and this inevitably becomes one!).
*/
private final String deprecationMessage = randomAlphaOfLengthBetween(1, 30);
private final DeprecationLogger deprecationLogger = mock(DeprecationLogger.class);
private DeprecationLogger deprecationLogger;
@Before
public void setup() {
handler = mock(RestHandler.class);
deprecationLogger = mock(DeprecationLogger.class);
}
public void testNullHandler() {
expectThrows(NullPointerException.class, () -> new DeprecationRestHandler(null, deprecationMessage, deprecationLogger));
@ -114,6 +122,16 @@ public class DeprecationRestHandlerTests extends ESTestCase {
expectThrows(IllegalArgumentException.class, () -> DeprecationRestHandler.requireValidHeader(blank));
}
public void testSupportsContentStreamTrue() {
when(handler.supportsContentStream()).thenReturn(true);
assertTrue(new DeprecationRestHandler(handler, deprecationMessage, deprecationLogger).supportsContentStream());
}
public void testSupportsContentStreamFalse() {
when(handler.supportsContentStream()).thenReturn(false);
assertFalse(new DeprecationRestHandler(handler, deprecationMessage, deprecationLogger).supportsContentStream());
}
/**
* {@code ASCIIHeaderGenerator} only uses characters expected to be valid in headers (simplified US-ASCII).
*/