summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/action/ingest
diff options
context:
space:
mode:
authorJay Modi <jaymode@users.noreply.github.com>2017-02-02 14:07:13 -0500
committerGitHub <noreply@github.com>2017-02-02 14:07:13 -0500
commit7520a107bee67099338813728147d2aee25ed240 (patch)
tree22828e74c5aa601c185c36c7463665fbfeaa4c51 /core/src/test/java/org/elasticsearch/action/ingest
parentb41d5747f0bd67dad05c8168312ba456bcdaebda (diff)
Optionally require a valid content type for all rest requests with content (#22691)
This change adds a strict mode for xcontent parsing on the rest layer. The strict mode will be off by default for 5.x and in a separate commit will be enabled by default for 6.0. The strict mode, which can be enabled by setting `http.content_type.required: true` in 5.x, will require that all incoming rest requests have a valid and supported content type header before the request is dispatched. In the non-strict mode, the Content-Type header will be inspected and if it is not present or not valid, we will continue with auto detection of content like we have done previously. The content type header is parsed to the matching XContentType value with the only exception being for plain text requests. This value is then passed on with the content bytes so that we can reduce the number of places where we need to auto-detect the content type. As part of this, many transport requests and builders were updated to provide methods that accepted the XContentType along with the bytes and the methods that would rely on auto-detection have been deprecated. In the non-strict mode, deprecation warnings are issued whenever a request with body doesn't provide the Content-Type header. See #19388
Diffstat (limited to 'core/src/test/java/org/elasticsearch/action/ingest')
-rw-r--r--core/src/test/java/org/elasticsearch/action/ingest/PutPipelineRequestTests.java67
-rw-r--r--core/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestTests.java40
-rw-r--r--core/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java2
3 files changed, 107 insertions, 2 deletions
diff --git a/core/src/test/java/org/elasticsearch/action/ingest/PutPipelineRequestTests.java b/core/src/test/java/org/elasticsearch/action/ingest/PutPipelineRequestTests.java
new file mode 100644
index 0000000000..01aed87947
--- /dev/null
+++ b/core/src/test/java/org/elasticsearch/action/ingest/PutPipelineRequestTests.java
@@ -0,0 +1,67 @@
+/*
+ * 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.action.ingest;
+
+import org.elasticsearch.Version;
+import org.elasticsearch.common.bytes.BytesArray;
+import org.elasticsearch.common.io.stream.BytesStreamOutput;
+import org.elasticsearch.common.io.stream.StreamInput;
+import org.elasticsearch.common.xcontent.XContentType;
+import org.elasticsearch.test.ESTestCase;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+
+public class PutPipelineRequestTests extends ESTestCase {
+
+ public void testSerializationWithXContent() throws IOException {
+ PutPipelineRequest request = new PutPipelineRequest("1", new BytesArray("{}".getBytes(StandardCharsets.UTF_8)), XContentType.JSON);
+ assertEquals(XContentType.JSON, request.getXContentType());
+
+ BytesStreamOutput output = new BytesStreamOutput();
+ request.writeTo(output);
+ StreamInput in = StreamInput.wrap(output.bytes().toBytesRef().bytes);
+
+ PutPipelineRequest serialized = new PutPipelineRequest();
+ serialized.readFrom(in);
+ assertEquals(XContentType.JSON, serialized.getXContentType());
+ assertEquals("{}", serialized.getSource().utf8ToString());
+ }
+
+ public void testSerializationBwc() throws IOException {
+ final byte[] data = Base64.getDecoder().decode("ADwDATECe30=");
+ final Version version = randomFrom(Version.V_5_0_0, Version.V_5_0_1, Version.V_5_0_2,
+ Version.V_5_0_3_UNRELEASED, Version.V_5_1_1_UNRELEASED, Version.V_5_1_2_UNRELEASED, Version.V_5_2_0_UNRELEASED);
+ try (StreamInput in = StreamInput.wrap(data)) {
+ in.setVersion(version);
+ PutPipelineRequest request = new PutPipelineRequest();
+ request.readFrom(in);
+ assertEquals(XContentType.JSON, request.getXContentType());
+ assertEquals("{}", request.getSource().utf8ToString());
+
+ try (BytesStreamOutput out = new BytesStreamOutput()) {
+ out.setVersion(version);
+ request.writeTo(out);
+ assertArrayEquals(data, out.bytes().toBytesRef().bytes);
+ }
+ }
+ }
+}
diff --git a/core/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestTests.java b/core/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestTests.java
index 0966010a8f..86dc56cdd0 100644
--- a/core/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestTests.java
+++ b/core/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestTests.java
@@ -19,19 +19,23 @@
package org.elasticsearch.action.ingest;
+import org.elasticsearch.Version;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
+import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
import static org.hamcrest.CoreMatchers.equalTo;
public class SimulatePipelineRequestTests extends ESTestCase {
public void testSerialization() throws IOException {
- SimulatePipelineRequest request = new SimulatePipelineRequest(new BytesArray(""));
+ SimulatePipelineRequest request = new SimulatePipelineRequest(new BytesArray(""), XContentType.JSON);
// Sometimes we set an id
if (randomBoolean()) {
request.setId(randomAsciiOfLengthBetween(1, 10));
@@ -51,4 +55,38 @@ public class SimulatePipelineRequestTests extends ESTestCase {
assertThat(otherRequest.getId(), equalTo(request.getId()));
assertThat(otherRequest.isVerbose(), equalTo(request.isVerbose()));
}
+
+ public void testSerializationWithXContent() throws IOException {
+ SimulatePipelineRequest request =
+ new SimulatePipelineRequest(new BytesArray("{}".getBytes(StandardCharsets.UTF_8)), XContentType.JSON);
+ assertEquals(XContentType.JSON, request.getXContentType());
+
+ BytesStreamOutput output = new BytesStreamOutput();
+ request.writeTo(output);
+ StreamInput in = StreamInput.wrap(output.bytes().toBytesRef().bytes);
+
+ SimulatePipelineRequest serialized = new SimulatePipelineRequest();
+ serialized.readFrom(in);
+ assertEquals(XContentType.JSON, serialized.getXContentType());
+ assertEquals("{}", serialized.getSource().utf8ToString());
+ }
+
+ public void testSerializationWithXContentBwc() throws IOException {
+ final byte[] data = Base64.getDecoder().decode("AAAAAnt9AAA=");
+ final Version version = randomFrom(Version.V_5_0_0, Version.V_5_0_1, Version.V_5_0_2,
+ Version.V_5_0_3_UNRELEASED, Version.V_5_1_1_UNRELEASED, Version.V_5_1_2_UNRELEASED, Version.V_5_2_0_UNRELEASED);
+ try (StreamInput in = StreamInput.wrap(data)) {
+ in.setVersion(version);
+ SimulatePipelineRequest request = new SimulatePipelineRequest();
+ request.readFrom(in);
+ assertEquals(XContentType.JSON, request.getXContentType());
+ assertEquals("{}", request.getSource().utf8ToString());
+
+ try (BytesStreamOutput out = new BytesStreamOutput()) {
+ out.setVersion(version);
+ request.writeTo(out);
+ assertArrayEquals(data, out.bytes().toBytesRef().bytes);
+ }
+ }
+ }
}
diff --git a/core/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java b/core/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java
index bc72094558..d5417526c0 100644
--- a/core/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java
+++ b/core/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java
@@ -127,7 +127,7 @@ public class WriteableIngestDocumentTests extends ESTestCase {
builder.startObject();
writeableIngestDocument.toXContent(builder, EMPTY_PARAMS);
builder.endObject();
- Map<String, Object> toXContentMap = XContentHelper.convertToMap(builder.bytes(), false).v2();
+ Map<String, Object> toXContentMap = XContentHelper.convertToMap(builder.bytes(), false, builder.contentType()).v2();
Map<String, Object> toXContentDoc = (Map<String, Object>) toXContentMap.get("doc");
Map<String, Object> toXContentSource = (Map<String, Object>) toXContentDoc.get("_source");