✨ Add chunked uploads
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
package dev.dinauer.r9t.core.multipart;
|
||||
|
||||
import dev.dinauer.r9t.core.auth.SigV4TestSigner;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
public class MultipartUploadTest
|
||||
{
|
||||
private static final String BUCKET = "multipart-test-bucket";
|
||||
private static final String KEY = "multipart-test.txt";
|
||||
private static final String ACCESS_KEY = "testkey";
|
||||
private static final String SECRET_KEY = "testsecret";
|
||||
|
||||
@TestHTTPResource
|
||||
URI baseUri;
|
||||
|
||||
private final HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();
|
||||
|
||||
@Test
|
||||
public void completeUploadAssemblesObject() throws Exception
|
||||
{
|
||||
createBucket();
|
||||
|
||||
HttpResponse<String> initiate = sendForString("POST", baseUri.resolve(BUCKET + "/" + KEY + "?uploads="),
|
||||
new byte[0], "text/plain");
|
||||
assertEquals(200, initiate.statusCode());
|
||||
String uploadId = extract(initiate.body(), "UploadId");
|
||||
|
||||
String etag1 = uploadPart(uploadId, 1, "hello ");
|
||||
String etag2 = uploadPart(uploadId, 2, "world");
|
||||
|
||||
String completeBody = "<CompleteMultipartUpload>"
|
||||
+ "<Part><PartNumber>1</PartNumber><ETag>\"" + etag1 + "\"</ETag></Part>"
|
||||
+ "<Part><PartNumber>2</PartNumber><ETag>\"" + etag2 + "\"</ETag></Part>"
|
||||
+ "</CompleteMultipartUpload>";
|
||||
HttpResponse<String> complete = sendForString("POST", baseUri.resolve(BUCKET + "/" + KEY + "?uploadId=" + uploadId),
|
||||
completeBody.getBytes(StandardCharsets.UTF_8), null);
|
||||
assertEquals(200, complete.statusCode());
|
||||
String finalEtag = extract(complete.body(), "ETag");
|
||||
assertTrue(Pattern.matches("[0-9a-f]{32}-2", finalEtag), "unexpected ETag: " + finalEtag);
|
||||
|
||||
HttpResponse<byte[]> get = sendForBytes("GET", baseUri.resolve(BUCKET + "/" + KEY));
|
||||
assertEquals(200, get.statusCode());
|
||||
assertEquals("text/plain", get.headers().firstValue("Content-Type").orElse(""));
|
||||
assertEquals("hello world", new String(get.body(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void abortDiscardsStagedParts() throws Exception
|
||||
{
|
||||
createBucket();
|
||||
|
||||
HttpResponse<String> initiate = sendForString("POST", baseUri.resolve(BUCKET + "/" + KEY + "?uploads="),
|
||||
new byte[0], "text/plain");
|
||||
String uploadId = extract(initiate.body(), "UploadId");
|
||||
|
||||
uploadPart(uploadId, 1, "hello ");
|
||||
|
||||
HttpResponse<Void> abort = send("DELETE", baseUri.resolve(BUCKET + "/" + KEY + "?uploadId=" + uploadId),
|
||||
new byte[0], null);
|
||||
assertEquals(204, abort.statusCode());
|
||||
|
||||
HttpResponse<Void> uploadAfterAbort = send("PUT",
|
||||
baseUri.resolve(BUCKET + "/" + KEY + "?partNumber=2&uploadId=" + uploadId), "world".getBytes(StandardCharsets.UTF_8), null);
|
||||
assertEquals(404, uploadAfterAbort.statusCode());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void cleanup() throws Exception
|
||||
{
|
||||
send("DELETE", baseUri.resolve(BUCKET + "/" + KEY), new byte[0], null);
|
||||
send("DELETE", baseUri.resolve(BUCKET), new byte[0], null);
|
||||
}
|
||||
|
||||
private void createBucket() throws Exception
|
||||
{
|
||||
HttpResponse<Void> createBucket = send("PUT", baseUri.resolve(BUCKET), new byte[0], null);
|
||||
assertEquals(200, createBucket.statusCode());
|
||||
}
|
||||
|
||||
private String uploadPart(String uploadId, int partNumber, String content) throws Exception
|
||||
{
|
||||
byte[] body = content.getBytes(StandardCharsets.UTF_8);
|
||||
HttpResponse<Void> put = send("PUT",
|
||||
baseUri.resolve(BUCKET + "/" + KEY + "?partNumber=" + partNumber + "&uploadId=" + uploadId), body, null);
|
||||
assertEquals(200, put.statusCode());
|
||||
return put.headers().firstValue("ETag").orElseThrow().replace("\"", "");
|
||||
}
|
||||
|
||||
private String extract(String xml, String tag)
|
||||
{
|
||||
Matcher matcher = Pattern.compile("<" + tag + ">(.*?)</" + tag + ">").matcher(xml);
|
||||
assertTrue(matcher.find(), "missing <" + tag + "> in: " + xml);
|
||||
return matcher.group(1).replace(""", "").replace("\"", "");
|
||||
}
|
||||
|
||||
private HttpResponse<Void> send(String method, URI uri, byte[] body, String contentType) throws Exception
|
||||
{
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder(uri)
|
||||
.method(method, HttpRequest.BodyPublishers.ofByteArray(body));
|
||||
if (contentType != null)
|
||||
{
|
||||
builder.header("Content-Type", contentType);
|
||||
}
|
||||
SigV4TestSigner.sign(builder, method, uri, ACCESS_KEY, SECRET_KEY, body);
|
||||
return client.send(builder.build(), HttpResponse.BodyHandlers.discarding());
|
||||
}
|
||||
|
||||
private HttpResponse<String> sendForString(String method, URI uri, byte[] body, String contentType) throws Exception
|
||||
{
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder(uri)
|
||||
.method(method, HttpRequest.BodyPublishers.ofByteArray(body));
|
||||
if (contentType != null)
|
||||
{
|
||||
builder.header("Content-Type", contentType);
|
||||
}
|
||||
SigV4TestSigner.sign(builder, method, uri, ACCESS_KEY, SECRET_KEY, body);
|
||||
return client.send(builder.build(), HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
|
||||
private HttpResponse<byte[]> sendForBytes(String method, URI uri) throws Exception
|
||||
{
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder(uri)
|
||||
.method(method, HttpRequest.BodyPublishers.ofByteArray(new byte[0]));
|
||||
SigV4TestSigner.sign(builder, method, uri, ACCESS_KEY, SECRET_KEY, new byte[0]);
|
||||
return client.send(builder.build(), HttpResponse.BodyHandlers.ofByteArray());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user