-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature/extensions] Pass full RestResponse to user from Extension #4356
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.opensearch.extensions.rest; | ||
|
||
import org.opensearch.rest.RestStatus; | ||
import org.opensearch.common.bytes.BytesReference; | ||
import org.opensearch.common.io.stream.BytesStreamInput; | ||
import org.opensearch.common.io.stream.BytesStreamOutput; | ||
import org.opensearch.rest.BytesRestResponse; | ||
import org.opensearch.rest.RestRequest.Method; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class RestExecuteOnExtensionTests extends OpenSearchTestCase { | ||
|
||
public void testRestExecuteOnExtensionRequest() throws Exception { | ||
Method expectedMethod = Method.GET; | ||
String expectedUri = "/test/uri"; | ||
RestExecuteOnExtensionRequest request = new RestExecuteOnExtensionRequest(expectedMethod, expectedUri); | ||
|
||
assertEquals(expectedMethod, request.getMethod()); | ||
assertEquals(expectedUri, request.getUri()); | ||
|
||
try (BytesStreamOutput out = new BytesStreamOutput()) { | ||
request.writeTo(out); | ||
out.flush(); | ||
try (BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()))) { | ||
request = new RestExecuteOnExtensionRequest(in); | ||
|
||
assertEquals(expectedMethod, request.getMethod()); | ||
assertEquals(expectedUri, request.getUri()); | ||
} | ||
} | ||
} | ||
|
||
public void testRestExecuteOnExtensionResponse() throws Exception { | ||
RestStatus expectedStatus = RestStatus.OK; | ||
String expectedContentType = BytesRestResponse.TEXT_CONTENT_TYPE; | ||
String expectedResponse = "Test response"; | ||
byte[] expectedResponseBytes = expectedResponse.getBytes(StandardCharsets.UTF_8); | ||
|
||
RestExecuteOnExtensionResponse response = new RestExecuteOnExtensionResponse(expectedStatus, expectedResponse); | ||
|
||
assertEquals(expectedStatus, response.getStatus()); | ||
assertEquals(expectedContentType, response.getContentType()); | ||
assertArrayEquals(expectedResponseBytes, response.getContent()); | ||
assertEquals(0, response.getHeaders().size()); | ||
|
||
String headerKey = "foo"; | ||
List<String> headerValueList = List.of("bar", "baz"); | ||
Map<String, List<String>> expectedHeaders = Map.of(headerKey, headerValueList); | ||
|
||
response = new RestExecuteOnExtensionResponse(expectedStatus, expectedContentType, expectedResponseBytes, expectedHeaders); | ||
|
||
assertEquals(expectedStatus, response.getStatus()); | ||
assertEquals(expectedContentType, response.getContentType()); | ||
assertArrayEquals(expectedResponseBytes, response.getContent()); | ||
|
||
assertEquals(1, expectedHeaders.keySet().size()); | ||
assertTrue(expectedHeaders.containsKey(headerKey)); | ||
|
||
List<String> fooList = expectedHeaders.get(headerKey); | ||
assertEquals(2, fooList.size()); | ||
assertTrue(fooList.containsAll(headerValueList)); | ||
|
||
try (BytesStreamOutput out = new BytesStreamOutput()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is great. Now we are testing input/output byte stream as well. Can we create an issue to test the other request/response as well we have for extensibility? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a great thing that we're testing it, as when my test initially failed I discovered Yes, we should test all the other request/response code similarly. There's a small list of other fixes I'd like (compiler warnings, etc.) that I'll put in an issue later this week. |
||
response.writeTo(out); | ||
out.flush(); | ||
try (BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()))) { | ||
response = new RestExecuteOnExtensionResponse(in); | ||
|
||
assertEquals(expectedStatus, response.getStatus()); | ||
assertEquals(expectedContentType, response.getContentType()); | ||
assertArrayEquals(expectedResponseBytes, response.getContent()); | ||
|
||
assertEquals(1, expectedHeaders.keySet().size()); | ||
assertTrue(expectedHeaders.containsKey(headerKey)); | ||
|
||
fooList = expectedHeaders.get(headerKey); | ||
assertEquals(2, fooList.size()); | ||
assertTrue(fooList.containsAll(headerValueList)); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to figure out a way to remove latches. I think OpenSearch does it via Listeners.