forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRestRequestTests.java
366 lines (326 loc) · 16.7 KB
/
RestRequestTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* 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.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.rest;
import org.opensearch.OpenSearchParseException;
import org.opensearch.common.CheckedConsumer;
import org.opensearch.common.Strings;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.collect.MapBuilder;
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.http.HttpChannel;
import org.opensearch.http.HttpRequest;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.rest.FakeRestRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class RestRequestTests extends OpenSearchTestCase {
public void testContentConsumesContent() {
runConsumesContentTest(RestRequest::content, true);
}
public void testRequiredContentConsumesContent() {
runConsumesContentTest(RestRequest::requiredContent, true);
}
public void testContentParserConsumesContent() {
runConsumesContentTest(RestRequest::contentParser, true);
}
public void testContentOrSourceParamConsumesContent() {
runConsumesContentTest(RestRequest::contentOrSourceParam, true);
}
public void testContentOrSourceParamsParserConsumesContent() {
runConsumesContentTest(RestRequest::contentOrSourceParamParser, true);
}
public void testWithContentOrSourceParamParserOrNullConsumesContent() {
@SuppressWarnings("unchecked")
CheckedConsumer<XContentParser, IOException> consumer = mock(CheckedConsumer.class);
runConsumesContentTest(request -> request.withContentOrSourceParamParserOrNull(consumer), true);
}
public void testApplyContentParserConsumesContent() {
@SuppressWarnings("unchecked")
CheckedConsumer<XContentParser, IOException> consumer = mock(CheckedConsumer.class);
runConsumesContentTest(request -> request.applyContentParser(consumer), true);
}
public void testHasContentDoesNotConsumesContent() {
runConsumesContentTest(RestRequest::hasContent, false);
}
private <T extends Exception> void runConsumesContentTest(final CheckedConsumer<RestRequest, T> consumer, final boolean expected) {
final HttpRequest httpRequest = mock(HttpRequest.class);
when(httpRequest.uri()).thenReturn("");
when(httpRequest.content()).thenReturn(new BytesArray(new byte[1]));
when(httpRequest.getHeaders()).thenReturn(
Collections.singletonMap("Content-Type", Collections.singletonList(randomFrom("application/json", "application/x-ndjson")))
);
final RestRequest request = RestRequest.request(mock(NamedXContentRegistry.class), httpRequest, mock(HttpChannel.class));
assertFalse(request.isContentConsumed());
try {
consumer.accept(request);
} catch (final Exception e) {
throw new RuntimeException(e);
}
assertThat(request.isContentConsumed(), equalTo(expected));
}
public void testContentParser() throws IOException {
Exception e = expectThrows(OpenSearchParseException.class, () -> contentRestRequest("", emptyMap()).contentParser());
assertEquals("request body is required", e.getMessage());
e = expectThrows(OpenSearchParseException.class, () -> contentRestRequest("", singletonMap("source", "{}")).contentParser());
assertEquals("request body is required", e.getMessage());
assertEquals(emptyMap(), contentRestRequest("{}", emptyMap()).contentParser().map());
e = expectThrows(OpenSearchParseException.class, () -> contentRestRequest("", emptyMap(), emptyMap()).contentParser());
assertEquals("request body is required", e.getMessage());
}
public void testApplyContentParser() throws IOException {
contentRestRequest("", emptyMap()).applyContentParser(p -> fail("Shouldn't have been called"));
contentRestRequest("", singletonMap("source", "{}")).applyContentParser(p -> fail("Shouldn't have been called"));
AtomicReference<Object> source = new AtomicReference<>();
contentRestRequest("{}", emptyMap()).applyContentParser(p -> source.set(p.map()));
assertEquals(emptyMap(), source.get());
}
public void testContentOrSourceParam() throws IOException {
Exception e = expectThrows(OpenSearchParseException.class, () -> contentRestRequest("", emptyMap()).contentOrSourceParam());
assertEquals("request body or source parameter is required", e.getMessage());
assertEquals(new BytesArray("stuff"), contentRestRequest("stuff", emptyMap()).contentOrSourceParam().v2());
assertEquals(
new BytesArray("stuff"),
contentRestRequest(
"stuff",
MapBuilder.<String, String>newMapBuilder()
.put("source", "stuff2")
.put("source_content_type", "application/json")
.immutableMap()
).contentOrSourceParam().v2()
);
assertEquals(
new BytesArray("{\"foo\": \"stuff\"}"),
contentRestRequest(
"",
MapBuilder.<String, String>newMapBuilder()
.put("source", "{\"foo\": \"stuff\"}")
.put("source_content_type", "application/json")
.immutableMap()
).contentOrSourceParam().v2()
);
e = expectThrows(
IllegalStateException.class,
() -> contentRestRequest("", MapBuilder.<String, String>newMapBuilder().put("source", "stuff2").immutableMap())
.contentOrSourceParam()
);
assertEquals("source and source_content_type parameters are required", e.getMessage());
}
public void testHasContentOrSourceParam() throws IOException {
assertEquals(false, contentRestRequest("", emptyMap()).hasContentOrSourceParam());
assertEquals(true, contentRestRequest("stuff", emptyMap()).hasContentOrSourceParam());
assertEquals(true, contentRestRequest("stuff", singletonMap("source", "stuff2")).hasContentOrSourceParam());
assertEquals(true, contentRestRequest("", singletonMap("source", "stuff")).hasContentOrSourceParam());
}
public void testContentOrSourceParamParser() throws IOException {
Exception e = expectThrows(OpenSearchParseException.class, () -> contentRestRequest("", emptyMap()).contentOrSourceParamParser());
assertEquals("request body or source parameter is required", e.getMessage());
assertEquals(emptyMap(), contentRestRequest("{}", emptyMap()).contentOrSourceParamParser().map());
assertEquals(emptyMap(), contentRestRequest("{}", singletonMap("source", "stuff2")).contentOrSourceParamParser().map());
assertEquals(
emptyMap(),
contentRestRequest(
"",
MapBuilder.<String, String>newMapBuilder().put("source", "{}").put("source_content_type", "application/json").immutableMap()
).contentOrSourceParamParser().map()
);
}
public void testWithContentOrSourceParamParserOrNull() throws IOException {
contentRestRequest("", emptyMap()).withContentOrSourceParamParserOrNull(parser -> assertNull(parser));
contentRestRequest("{}", emptyMap()).withContentOrSourceParamParserOrNull(parser -> assertEquals(emptyMap(), parser.map()));
contentRestRequest("{}", singletonMap("source", "stuff2")).withContentOrSourceParamParserOrNull(
parser -> assertEquals(emptyMap(), parser.map())
);
contentRestRequest(
"",
MapBuilder.<String, String>newMapBuilder().put("source_content_type", "application/json").put("source", "{}").immutableMap()
).withContentOrSourceParamParserOrNull(parser -> assertEquals(emptyMap(), parser.map()));
}
public void testContentTypeParsing() {
for (XContentType xContentType : XContentType.values()) {
Map<String, List<String>> map = new HashMap<>();
map.put("Content-Type", Collections.singletonList(xContentType.mediaType()));
RestRequest restRequest = contentRestRequest("", Collections.emptyMap(), map);
assertEquals(xContentType, restRequest.getXContentType());
map = new HashMap<>();
map.put("Content-Type", Collections.singletonList(xContentType.mediaTypeWithoutParameters()));
restRequest = contentRestRequest("", Collections.emptyMap(), map);
assertEquals(xContentType, restRequest.getXContentType());
}
}
public void testPlainTextSupport() {
RestRequest restRequest = contentRestRequest(
randomAlphaOfLengthBetween(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");
final RestRequest.ContentTypeHeaderException e = expectThrows(RestRequest.ContentTypeHeaderException.class, () -> {
final Map<String, List<String>> headers = Collections.singletonMap("Content-Type", Collections.singletonList(type));
contentRestRequest("", Collections.emptyMap(), headers);
});
assertNotNull(e.getCause());
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
assertThat(e.getMessage(), equalTo("java.lang.IllegalArgumentException: invalid Content-Type header [" + type + "]"));
}
public void testNoContentTypeHeader() {
RestRequest contentRestRequest = contentRestRequest("", Collections.emptyMap(), Collections.emptyMap());
assertNull(contentRestRequest.getXContentType());
}
public void testMultipleContentTypeHeaders() {
List<String> headers = new ArrayList<>(randomUnique(() -> randomAlphaOfLengthBetween(1, 16), randomIntBetween(2, 10)));
final RestRequest.ContentTypeHeaderException e = expectThrows(
RestRequest.ContentTypeHeaderException.class,
() -> contentRestRequest("", Collections.emptyMap(), Collections.singletonMap("Content-Type", headers))
);
assertNotNull(e.getCause());
assertThat(e.getCause(), instanceOf((IllegalArgumentException.class)));
assertThat(e.getMessage(), equalTo("java.lang.IllegalArgumentException: only one Content-Type header should be provided"));
}
public void testRequiredContent() {
Exception e = expectThrows(OpenSearchParseException.class, () -> contentRestRequest("", emptyMap()).requiredContent());
assertEquals("request body is required", e.getMessage());
assertEquals(new BytesArray("stuff"), contentRestRequest("stuff", emptyMap()).requiredContent());
assertEquals(
new BytesArray("stuff"),
contentRestRequest(
"stuff",
MapBuilder.<String, String>newMapBuilder()
.put("source", "stuff2")
.put("source_content_type", "application/json")
.immutableMap()
).requiredContent()
);
e = expectThrows(
OpenSearchParseException.class,
() -> contentRestRequest(
"",
MapBuilder.<String, String>newMapBuilder()
.put("source", "{\"foo\": \"stuff\"}")
.put("source_content_type", "application/json")
.immutableMap()
).requiredContent()
);
assertEquals("request body is required", e.getMessage());
e = expectThrows(IllegalStateException.class, () -> contentRestRequest("test", null, Collections.emptyMap()).requiredContent());
assertEquals("unknown content type", e.getMessage());
}
/*
* The test is added in 2.0 when the request parameter "cluster_manager_timeout" is introduced.
* Remove the test along with the removal of the non-inclusive terminology "master_timeout".
*/
public void testValidateParamValuesAreEqualWhenTheyAreEqual() {
FakeRestRequest request = new FakeRestRequest();
String valueForKey1 = randomFrom("value1", "", null);
String valueForKey2 = "value1";
request.params().put("key1", valueForKey1);
request.params().put("key2", valueForKey2);
request.validateParamValuesAreEqual("key1", "key2");
assertTrue(
String.format(
Locale.ROOT,
"The 2 values should be equal, or having 1 null/empty value. Value of key1: %s. Value of key2: %s",
valueForKey1,
valueForKey2
),
Strings.isNullOrEmpty(valueForKey1) || valueForKey1.equals(valueForKey2)
);
}
/*
* The test is added in 2.0 when the request parameter "cluster_manager_timeout" is introduced.
* Remove the test along with the removal of the non-inclusive terminology "master_timeout".
*/
public void testValidateParamValuesAreEqualWhenTheyAreNotEqual() {
FakeRestRequest request = new FakeRestRequest();
request.params().put("key1", "value1");
request.params().put("key2", "value2");
Exception e = assertThrows(OpenSearchParseException.class, () -> request.validateParamValuesAreEqual("key1", "key2"));
assertThat(e.getMessage(), containsString("The values of the request parameters: [key1, key2] are required to be equal"));
}
private static RestRequest contentRestRequest(String content, Map<String, String> params) {
Map<String, List<String>> headers = new HashMap<>();
headers.put("Content-Type", Collections.singletonList("application/json"));
return contentRestRequest(content, params, headers);
}
private static RestRequest contentRestRequest(String content, Map<String, String> params, Map<String, List<String>> headers) {
FakeRestRequest.Builder builder = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY);
builder.withHeaders(headers);
builder.withContent(new BytesArray(content), null);
builder.withParams(params);
return new ContentRestRequest(builder.build());
}
private static final class ContentRestRequest extends RestRequest {
private final RestRequest restRequest;
private ContentRestRequest(RestRequest restRequest) {
super(
restRequest.getXContentRegistry(),
restRequest.params(),
restRequest.path(),
restRequest.getHeaders(),
restRequest.getHttpRequest(),
restRequest.getHttpChannel()
);
this.restRequest = restRequest;
}
@Override
public Method method() {
return restRequest.method();
}
@Override
public String uri() {
return restRequest.uri();
}
@Override
public BytesReference content() {
return restRequest.content();
}
}
}