diff options
Diffstat (limited to 'core/src/test/java/org/elasticsearch/rest/RestRequestTests.java')
-rw-r--r-- | core/src/test/java/org/elasticsearch/rest/RestRequestTests.java | 80 |
1 files changed, 62 insertions, 18 deletions
diff --git a/core/src/test/java/org/elasticsearch/rest/RestRequestTests.java b/core/src/test/java/org/elasticsearch/rest/RestRequestTests.java index 7f0184e20c..bddcd39160 100644 --- a/core/src/test/java/org/elasticsearch/rest/RestRequestTests.java +++ b/core/src/test/java/org/elasticsearch/rest/RestRequestTests.java @@ -24,11 +24,15 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicReference; import static java.util.Collections.emptyMap; @@ -54,10 +58,13 @@ public class RestRequestTests extends ESTestCase { } public void testContentOrSourceParam() throws IOException { - assertEquals(BytesArray.EMPTY, new ContentRestRequest("", emptyMap()).contentOrSourceParam()); - assertEquals(new BytesArray("stuff"), new ContentRestRequest("stuff", emptyMap()).contentOrSourceParam()); - assertEquals(new BytesArray("stuff"), new ContentRestRequest("stuff", singletonMap("source", "stuff2")).contentOrSourceParam()); - assertEquals(new BytesArray("stuff"), new ContentRestRequest("", singletonMap("source", "stuff")).contentOrSourceParam()); + assertEquals(BytesArray.EMPTY, new ContentRestRequest("", emptyMap()).contentOrSourceParam().v2()); + assertEquals(new BytesArray("stuff"), new ContentRestRequest("stuff", emptyMap()).contentOrSourceParam().v2()); + assertEquals(new BytesArray("stuff"), + new ContentRestRequest("stuff", singletonMap("source", "stuff2")).contentOrSourceParam().v2()); + assertEquals(new BytesArray("{\"foo\": \"stuff\"}"), + new ContentRestRequest("", singletonMap("source", "{\"foo\": \"stuff\"}")).contentOrSourceParam().v2()); + assertWarnings("Deprecated use of the [source] parameter without the [source_content_type] parameter."); } public void testHasContentOrSourceParam() throws IOException { @@ -74,6 +81,7 @@ public class RestRequestTests extends ESTestCase { assertEquals(emptyMap(), new ContentRestRequest("{}", emptyMap()).contentOrSourceParamParser().map()); assertEquals(emptyMap(), new ContentRestRequest("{}", singletonMap("source", "stuff2")).contentOrSourceParamParser().map()); assertEquals(emptyMap(), new ContentRestRequest("", singletonMap("source", "{}")).contentOrSourceParamParser().map()); + assertWarnings("Deprecated use of the [source] parameter without the [source_content_type] parameter."); } public void testWithContentOrSourceParamParserOrNull() throws IOException { @@ -83,12 +91,58 @@ public class RestRequestTests extends ESTestCase { assertEquals(emptyMap(), parser.map())); new ContentRestRequest("", singletonMap("source", "{}")).withContentOrSourceParamParserOrNull(parser -> assertEquals(emptyMap(), parser.map())); + assertWarnings("Deprecated use of the [source] parameter without the [source_content_type] parameter."); + } + + public void testContentTypeParsing() { + for (XContentType xContentType : XContentType.values()) { + Map<String, List<String>> map = new HashMap<>(); + map.put("Content-Type", Collections.singletonList(xContentType.mediaType())); + ContentRestRequest restRequest = new ContentRestRequest("", Collections.emptyMap(), map); + assertEquals(xContentType, restRequest.getXContentType()); + + map = new HashMap<>(); + map.put("Content-Type", Collections.singletonList(xContentType.mediaTypeWithoutParameters())); + restRequest = new ContentRestRequest("", Collections.emptyMap(), map); + assertEquals(xContentType, restRequest.getXContentType()); + } + } + + public void testPlainTextSupport() { + ContentRestRequest restRequest = new ContentRestRequest(randomAsciiOfLengthBetween(1, 30), Collections.emptyMap(), + Collections.singletonMap("Content-Type", + Collections.singletonList(randomFrom("text/plain", "text/plain; charset=utf-8", "text/plain;charset=utf-8")))); + assertNull(restRequest.getXContentType()); + } + + public void testMalformedContentTypeHeader() { + final String type = randomFrom("text", "text/:ain; charset=utf-8", "text/plain\";charset=utf-8", ":", "/", "t:/plain"); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new ContentRestRequest("", Collections.emptyMap(), + Collections.singletonMap("Content-Type", Collections.singletonList(type)))); + assertEquals("invalid Content-Type header [" + type + "]", e.getMessage()); + } + + public void testNoContentTypeHeader() { + ContentRestRequest contentRestRequest = new ContentRestRequest("", Collections.emptyMap(), Collections.emptyMap()); + assertNull(contentRestRequest.getXContentType()); + } + + public void testMultipleContentTypeHeaders() { + List<String> headers = new ArrayList<>(randomUnique(() -> randomAsciiOfLengthBetween(1, 16), randomIntBetween(2, 10))); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new ContentRestRequest("", Collections.emptyMap(), + Collections.singletonMap("Content-Type", headers))); + assertEquals("only one Content-Type header should be provided", e.getMessage()); } private static final class ContentRestRequest extends RestRequest { private final BytesArray content; - public ContentRestRequest(String content, Map<String, String> params) { - super(NamedXContentRegistry.EMPTY, params, "not used by this test"); + + ContentRestRequest(String content, Map<String, String> params) { + this(content, params, Collections.singletonMap("Content-Type", Collections.singletonList("application/json"))); + } + + ContentRestRequest(String content, Map<String, String> params, Map<String, List<String>> headers) { + super(NamedXContentRegistry.EMPTY, params, "not used by this test", headers); this.content = new BytesArray(content); } @@ -96,7 +150,7 @@ public class RestRequestTests extends ESTestCase { public boolean hasContent() { return Strings.hasLength(content); } - + @Override public BytesReference content() { return content; @@ -111,15 +165,5 @@ public class RestRequestTests extends ESTestCase { public Method method() { throw new UnsupportedOperationException("Not used by this test"); } - - @Override - public Iterable<Entry<String, String>> headers() { - throw new UnsupportedOperationException("Not used by this test"); - } - - @Override - public String header(String name) { - throw new UnsupportedOperationException("Not used by this test"); - } } } |