Skip to content

Commit

Permalink
Add test for GCS fixture (elastic#118737)
Browse files Browse the repository at this point in the history
Relates: ES-5679
  • Loading branch information
nicktindall authored and rjernst committed Dec 18, 2024
1 parent c7fd15e commit 9f928de
Show file tree
Hide file tree
Showing 4 changed files with 520 additions and 6 deletions.
8 changes: 8 additions & 0 deletions server/src/main/java/org/elasticsearch/rest/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;

import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand All @@ -35,6 +36,13 @@ public class RestUtils {

public static final UnaryOperator<String> REST_DECODER = RestUtils::decodeComponent;

public static void decodeQueryString(URI uri, Map<String, String> params) {
final var rawQuery = uri.getRawQuery();
if (Strings.hasLength(rawQuery)) {
decodeQueryString(rawQuery, 0, params);
}
}

public static void decodeQueryString(String s, int fromIndex, Map<String, String> params) {
if (fromIndex < 0) {
return;
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/gcs-fixture/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
apply plugin: 'elasticsearch.java'

description = 'Fixture for Google Cloud Storage service'
tasks.named("test").configure { enabled = false }

dependencies {
api project(':server')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void handle(final HttpExchange exchange) throws IOException {
} else if (Regex.simpleMatch("GET /storage/v1/b/" + bucket + "/o*", request)) {
// List Objects https://cloud.google.com/storage/docs/json_api/v1/objects/list
final Map<String, String> params = new HashMap<>();
RestUtils.decodeQueryString(exchange.getRequestURI().getQuery(), 0, params);
RestUtils.decodeQueryString(exchange.getRequestURI(), params);
final String prefix = params.getOrDefault("prefix", "");
final String delimiter = params.get("delimiter");

Expand Down Expand Up @@ -212,7 +212,7 @@ public void handle(final HttpExchange exchange) throws IOException {
} else if (Regex.simpleMatch("POST /upload/storage/v1/b/" + bucket + "/*uploadType=resumable*", request)) {
// Resumable upload initialization https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload
final Map<String, String> params = new HashMap<>();
RestUtils.decodeQueryString(exchange.getRequestURI().getQuery(), 0, params);
RestUtils.decodeQueryString(exchange.getRequestURI(), params);
final String blobName = params.get("name");
blobs.put(blobName, BytesArray.EMPTY);

Expand All @@ -237,7 +237,7 @@ public void handle(final HttpExchange exchange) throws IOException {
} else if (Regex.simpleMatch("PUT /upload/storage/v1/b/" + bucket + "/o?*uploadType=resumable*", request)) {
// Resumable upload https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload
final Map<String, String> params = new HashMap<>();
RestUtils.decodeQueryString(exchange.getRequestURI().getQuery(), 0, params);
RestUtils.decodeQueryString(exchange.getRequestURI(), params);

final String blobName = params.get("test_blob_name");
if (blobs.containsKey(blobName) == false) {
Expand Down Expand Up @@ -269,8 +269,6 @@ public void handle(final HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1);
}
} finally {
int read = exchange.getRequestBody().read();
assert read == -1 : "Request body should have been fully read here but saw [" + read + "]";
exchange.close();
}
}
Expand Down
Loading

0 comments on commit 9f928de

Please sign in to comment.