Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static io.grpc.netty.shaded.io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
import static io.grpc.netty.shaded.io.netty.handler.codec.http.HttpHeaderValues.CLOSE;

import com.google.api.client.http.UriTemplate;
import com.google.api.gax.retrying.RetrySettings;
import com.google.cloud.NoCredentials;
import com.google.cloud.storage.it.runner.registry.Registry;
Expand Down Expand Up @@ -48,6 +49,7 @@
import java.net.InetSocketAddress;
import java.net.URI;
import java.time.Duration;
import java.util.Map;

final class FakeHttpServer implements AutoCloseable {

Expand All @@ -64,12 +66,24 @@ private FakeHttpServer(
this.httpStorageOptions = httpStorageOptions;
}

public HttpStorageOptions getHttpStorageOptions() {
return httpStorageOptions;
/**
* overload which calls {@link #createUri(String, Map, boolean)} with {@code createUri(template,
* params, false)}
*/
public URI createUri(String template, Map<String, String> params) {
return createUri(template, params, false);
}

public URI getEndpoint() {
return endpoint;
/** Decorator for {@link UriTemplate#expand(String, String, Object, boolean)} */
public URI createUri(
String template, Map<String, String> params, boolean addUnusedParamsAsQueryParams) {
String expand =
UriTemplate.expand(endpoint.toString(), template, params, addUnusedParamsAsQueryParams);
return URI.create(expand);
}

public HttpStorageOptions getHttpStorageOptions() {
return httpStorageOptions;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand Down Expand Up @@ -95,14 +94,14 @@ public void emptyObjectHappyPath() throws Exception {
};

try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.empty(),
HttpContentRange.of(ByteRangeSpec.explicitClosed(0L, 0L), 0));

Expand Down Expand Up @@ -142,16 +141,16 @@ public void scenario7() throws Exception {
};

try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

AtomicLong confirmedBytes = new AtomicLong(-1L);

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.empty(),
HttpContentRange.of(ByteRangeSpec.explicitClosed(0L, 10L)));

Expand Down Expand Up @@ -224,16 +223,16 @@ public void scenario1() throws Exception {
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler);
TmpFile tmpFile =
DataGenerator.base64Characters().tempFile(temp.newFolder().toPath(), _256KiBL)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

AtomicLong confirmedBytes = new AtomicLong(-1L);

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.of(tmpFile.getPath()),
HttpContentRange.of(ByteRangeSpec.explicit(0L, _256KiBL)));

Expand Down Expand Up @@ -294,16 +293,16 @@ public void scenario2() throws Exception {
};

try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

AtomicLong confirmedBytes = new AtomicLong(-1L);

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.empty(),
HttpContentRange.of(_256KiBL));

Expand Down Expand Up @@ -364,16 +363,16 @@ public void scenario3() throws Exception {
};

try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

AtomicLong confirmedBytes = new AtomicLong(-1L);

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.empty(),
HttpContentRange.of(_512KiBL));

Expand Down Expand Up @@ -445,22 +444,21 @@ public void scenario4() throws Exception {
};

try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.empty(),
HttpContentRange.of(_256KiBL));

ResumableOperationResult<@Nullable StorageObject> operationResult = task.call();
StorageObject call = operationResult.getObject();
assertThat(call).isNotNull();
assertThat(call.getMetadata())
.containsEntry("upload_id", uploadUrl.substring(endpoint.toString().length()));
assertThat(call.getMetadata()).containsEntry("upload_id", uri.getPath());
assertThat(operationResult.getPersistedSize()).isEqualTo(_256KiBL);
}
}
Expand Down Expand Up @@ -526,16 +524,16 @@ public void scenario4_1() throws Exception {
};

try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

AtomicLong confirmedBytes = new AtomicLong(-1L);

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.empty(),
HttpContentRange.of(_512KiBL));

Expand Down Expand Up @@ -607,16 +605,16 @@ public void scenario4_2() throws Exception {
};

try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

AtomicLong confirmedBytes = new AtomicLong(-1L);

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.empty(),
HttpContentRange.of(_128KiBL));

Expand Down Expand Up @@ -686,16 +684,16 @@ public void scenario5() throws Exception {
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler);
TmpFile tmpFile =
DataGenerator.base64Characters().tempFile(temp.newFolder().toPath(), _256KiBL)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

AtomicLong confirmedBytes = new AtomicLong(-1L);

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.of(tmpFile.getPath()),
HttpContentRange.of(ByteRangeSpec.explicit(_512KiBL, _768KiBL)));

Expand All @@ -719,16 +717,16 @@ public void _503_emptyBody() throws Exception {
try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler);
TmpFile tmpFile =
DataGenerator.base64Characters().tempFile(temp.newFolder().toPath(), _256KiBL)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

AtomicLong confirmedBytes = new AtomicLong(-1L);

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.of(tmpFile.getPath()),
HttpContentRange.of(ByteRangeSpec.explicit(_512KiBL, _768KiBL)));

Expand Down Expand Up @@ -761,16 +759,16 @@ public void jsonParseFailure() throws Exception {
};

try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

AtomicLong confirmedBytes = new AtomicLong(-1L);

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.empty(),
HttpContentRange.of(0));

Expand Down Expand Up @@ -802,14 +800,14 @@ public void jsonDeserializationOnlyAttemptedWhenContentPresent() throws Exceptio
};

try (FakeHttpServer fakeHttpServer = FakeHttpServer.of(handler)) {
URI endpoint = fakeHttpServer.getEndpoint();
String uploadUrl =
String.format(Locale.US, "%s/upload/%s", endpoint.toString(), UUID.randomUUID());
URI uri =
fakeHttpServer.createUri(
"/upload/{uploadId}", ImmutableMap.of("uploadId", UUID.randomUUID().toString()));

JsonResumableSessionPutTask task =
new JsonResumableSessionPutTask(
httpClientContext,
jsonResumableWrite(uploadUrl),
jsonResumableWrite(uri),
RewindableContent.empty(),
HttpContentRange.of(0));

Expand Down Expand Up @@ -878,7 +876,7 @@ public void repeatedRewindsToTheSameLocationWork() {
assertThat(buf2.position()).isEqualTo(13);
}

static @NonNull JsonResumableWrite jsonResumableWrite(String uploadUrl) {
return JsonResumableWrite.of(new StorageObject(), ImmutableMap.of(), uploadUrl, 0);
static @NonNull JsonResumableWrite jsonResumableWrite(URI uploadUrl) {
return JsonResumableWrite.of(new StorageObject(), ImmutableMap.of(), uploadUrl.toString(), 0);
}
}
Loading