Skip to content

Commit

Permalink
Update ExtensionRestResponse to handle RestRequest object (opensearch…
Browse files Browse the repository at this point in the history
…-project#6874)

Signed-off-by: Daniel Widdis <widdis@gmail.com>
Signed-off-by: Valentin Mitrofanov <mitrofmep@gmail.com>
  • Loading branch information
dbwiddis authored and mitrofmep committed Apr 5, 2023
1 parent cea6000 commit b993a6a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestStatus;

import java.util.List;
Expand All @@ -32,7 +33,7 @@ public class ExtensionRestResponse extends BytesRestResponse {
* @param status The REST status.
* @param builder The builder for the response.
*/
public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, XContentBuilder builder) {
public ExtensionRestResponse(RestRequest request, RestStatus status, XContentBuilder builder) {
super(status, builder);
this.consumedParams = request.consumedParams();
this.contentConsumed = request.isContentConsumed();
Expand All @@ -45,7 +46,7 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, XC
* @param status The REST status.
* @param content A plain text response string.
*/
public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String content) {
public ExtensionRestResponse(RestRequest request, RestStatus status, String content) {
super(status, content);
this.consumedParams = request.consumedParams();
this.contentConsumed = request.isContentConsumed();
Expand All @@ -59,7 +60,7 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, St
* @param contentType The content type of the response string.
* @param content A response string.
*/
public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String contentType, String content) {
public ExtensionRestResponse(RestRequest request, RestStatus status, String contentType, String content) {
super(status, contentType, content);
this.consumedParams = request.consumedParams();
this.contentConsumed = request.isContentConsumed();
Expand All @@ -73,7 +74,7 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, St
* @param contentType The content type of the response bytes.
* @param content Response bytes.
*/
public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String contentType, byte[] content) {
public ExtensionRestResponse(RestRequest request, RestStatus status, String contentType, byte[] content) {
super(status, contentType, content);
this.consumedParams = request.consumedParams();
this.contentConsumed = request.isContentConsumed();
Expand All @@ -87,7 +88,7 @@ public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, St
* @param contentType The content type of the response bytes.
* @param content Response bytes.
*/
public ExtensionRestResponse(ExtensionRestRequest request, RestStatus status, String contentType, BytesReference content) {
public ExtensionRestResponse(RestRequest request, RestStatus status, String contentType, BytesReference content) {
super(status, contentType, content);
this.consumedParams = request.consumedParams();
this.contentConsumed = request.isContentConsumed();
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/org/opensearch/rest/RestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public Map<String, String> params() {
*
* @return the list of currently consumed parameters.
*/
List<String> consumedParams() {
public List<String> consumedParams() {
return new ArrayList<>(consumedParams);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.http.HttpRequest;
import org.opensearch.http.HttpResponse;
import org.opensearch.http.HttpRequest.HttpVersion;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestStatus;
import org.opensearch.rest.RestRequest.Method;
import org.opensearch.test.OpenSearchTestCase;

Expand All @@ -38,15 +45,63 @@ public void setUp() throws Exception {
testBytes = new byte[] { 1, 2 };
}

private ExtensionRestRequest generateTestRequest() {
ExtensionRestRequest request = new ExtensionRestRequest(
Method.GET,
"/foo",
Collections.emptyMap(),
null,
new BytesArray("Text Content"),
null
);
private RestRequest generateTestRequest() {
RestRequest request = RestRequest.request(null, new HttpRequest() {

@Override
public Method method() {
return Method.GET;
}

@Override
public String uri() {
return "/foo";
}

@Override
public BytesReference content() {
return new BytesArray("Text Content");
}

@Override
public Map<String, List<String>> getHeaders() {
return Collections.emptyMap();
}

@Override
public List<String> strictCookies() {
return Collections.emptyList();
}

@Override
public HttpVersion protocolVersion() {
return null;
}

@Override
public HttpRequest removeHeader(String header) {
// we don't use
return null;
}

@Override
public HttpResponse createResponse(RestStatus status, BytesReference content) {
return null;
}

@Override
public Exception getInboundException() {
return null;
}

@Override
public void release() {}

@Override
public HttpRequest releaseAndCopy() {
return null;
}
}, null);
// consume params "foo" and "bar"
request.param("foo");
request.param("bar");
Expand All @@ -60,7 +115,7 @@ public void testConstructorWithBuilder() throws IOException {
builder.startObject();
builder.field("status", ACCEPTED);
builder.endObject();
ExtensionRestRequest request = generateTestRequest();
RestRequest request = generateTestRequest();
ExtensionRestResponse response = new ExtensionRestResponse(request, OK, builder);

assertEquals(OK, response.status());
Expand All @@ -73,7 +128,7 @@ public void testConstructorWithBuilder() throws IOException {
}

public void testConstructorWithPlainText() {
ExtensionRestRequest request = generateTestRequest();
RestRequest request = generateTestRequest();
ExtensionRestResponse response = new ExtensionRestResponse(request, OK, testText);

assertEquals(OK, response.status());
Expand All @@ -86,7 +141,7 @@ public void testConstructorWithPlainText() {
}

public void testConstructorWithText() {
ExtensionRestRequest request = generateTestRequest();
RestRequest request = generateTestRequest();
ExtensionRestResponse response = new ExtensionRestResponse(request, OK, TEXT_CONTENT_TYPE, testText);

assertEquals(OK, response.status());
Expand All @@ -100,7 +155,7 @@ public void testConstructorWithText() {
}

public void testConstructorWithByteArray() {
ExtensionRestRequest request = generateTestRequest();
RestRequest request = generateTestRequest();
ExtensionRestResponse response = new ExtensionRestResponse(request, OK, OCTET_CONTENT_TYPE, testBytes);

assertEquals(OK, response.status());
Expand All @@ -113,7 +168,7 @@ public void testConstructorWithByteArray() {
}

public void testConstructorWithBytesReference() {
ExtensionRestRequest request = generateTestRequest();
RestRequest request = generateTestRequest();
ExtensionRestResponse response = new ExtensionRestResponse(
request,
OK,
Expand Down

0 comments on commit b993a6a

Please sign in to comment.