Follow redirects in ClientGet
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / quelaton / DefaultFcpClientTest.java
index f7823d6..61a7d0a 100644 (file)
@@ -1,5 +1,6 @@
 package net.pterodactylus.fcp.quelaton;
 
+import static net.pterodactylus.fcp.RequestProgressMatcher.isRequestProgress;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.allOf;
 import static org.hamcrest.Matchers.contains;
@@ -16,6 +17,7 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
@@ -43,6 +45,7 @@ import net.pterodactylus.fcp.Peer;
 import net.pterodactylus.fcp.PeerNote;
 import net.pterodactylus.fcp.PluginInfo;
 import net.pterodactylus.fcp.Priority;
+import net.pterodactylus.fcp.RequestProgress;
 import net.pterodactylus.fcp.fake.FakeTcpServer;
 import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
 
@@ -215,6 +218,14 @@ public class DefaultFcpClientTest {
                assertThat(lines, requestMatcher.get());
        }
 
+       private void replyWithProtocolError() throws IOException {
+               fcpServer.writeLine(
+                       "ProtocolError",
+                       "Identifier=" + identifier,
+                       "EndMessage"
+               );
+       }
+
        public class ConnectionsAndKeyPairs {
 
                public class Connections {
@@ -438,6 +449,14 @@ public class DefaultFcpClientTest {
                                        assertThat(peer.get().get().getIdentity(), is("id1"));
                                }
 
+                               @Test
+                               public void protocolErrorEndsCommand() throws InterruptedException, ExecutionException, IOException {
+                                       Future<Optional<Peer>> peer = fcpClient.addPeer().fromFile(new File("/tmp/ref.txt")).execute();
+                                       connectAndAssert(() -> allOf(matchesAddPeer(), hasItem("File=/tmp/ref.txt")));
+                                       replyWithProtocolError();
+                                       assertThat(peer.get().isPresent(), is(false));
+                               }
+
                                private NodeRef createNodeRef() {
                                        NodeRef nodeRef = new NodeRef();
                                        nodeRef.setIdentity("id1");
@@ -974,14 +993,6 @@ public class DefaultFcpClientTest {
 
                }
 
-               private void replyWithProtocolError() throws IOException {
-                       fcpServer.writeLine(
-                               "ProtocolError",
-                               "Identifier=" + identifier,
-                               "EndMessage"
-                       );
-               }
-
                public class ReloadPlugin {
 
                        @Test
@@ -1022,6 +1033,15 @@ public class DefaultFcpClientTest {
                                verifyPluginInfo(pluginInfo);
                        }
 
+                       @Test
+                       public void protocolErrorIsRecognizedAsFailure()
+                       throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<PluginInfo>> pluginInfo = fcpClient.reloadPlugin().plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> matchReloadPluginMessage());
+                               replyWithProtocolError();
+                               assertThat(pluginInfo.get().isPresent(), is(false));
+                       }
+
                        private Matcher<List<String>> matchReloadPluginMessage() {
                                return matchesFcpMessage(
                                        "ReloadPlugin",
@@ -1279,6 +1299,30 @@ public class DefaultFcpClientTest {
                        connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "Global=true"));
                }
 
+               @Test
+               public void clientGetFollowsRedirect() throws InterruptedException, ExecutionException, IOException {
+                   Future<Optional<Data>> data = fcpClient.clientGet().uri("USK@foo/bar").execute();
+                       connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=USK@foo/bar"));
+                       replyWithRedirect("USK@foo/baz");
+                       readMessage(() -> matchesFcpMessage("ClientGet", "URI=USK@foo/baz"));
+                       replyWithAllData(identifier, "Hello", "text/plain;charset=utf-8");
+                       verifyData(data.get());
+               }
+
+               @Test
+               public void clientGetNotifiesListenersOnRedirect() throws IOException, ExecutionException, InterruptedException {
+                       List<String> redirects = new ArrayList<>();
+                       Future<Optional<Data>> data = fcpClient.clientGet().onRedirect(redirects::add).uri("USK@foo/bar").execute();
+                       connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=USK@foo/bar"));
+                       replyWithRedirect("USK@foo/baz");
+                       readMessage(() -> matchesFcpMessage("ClientGet", "URI=USK@foo/baz"));
+                       replyWithRedirect("USK@foo/quux");
+                       readMessage(() -> matchesFcpMessage("ClientGet", "URI=USK@foo/quux"));
+                       replyWithAllData(identifier, "Hello", "text/plain;charset=utf-8");
+                       verifyData(data.get());
+                       assertThat(redirects, contains("USK@foo/baz", "USK@foo/quux"));
+               }
+
                private void replyWithGetFailed(String identifier) throws IOException {
                        fcpServer.writeLine(
                                "GetFailed",
@@ -1288,6 +1332,16 @@ public class DefaultFcpClientTest {
                        );
                }
 
+               private void replyWithRedirect(String newUri) throws IOException {
+                       fcpServer.writeLine(
+                               "GetFailed",
+                               "Identifier=" + identifier,
+                               "Code=27",
+                               "RedirectURI=" + newUri,
+                               "EndMessage"
+                       );
+               }
+
                private void replyWithAllData(String identifier, String text, String contentType) throws IOException {
                        fcpServer.writeLine(
                                "AllData",
@@ -1310,197 +1364,308 @@ public class DefaultFcpClientTest {
 
        }
 
-       public class ClientPut {
-
-               @Test
-               public void sendsCorrectCommand() throws IOException, ExecutionException, InterruptedException {
-                       fcpClient.clientPut()
-                               .from(new ByteArrayInputStream("Hello\n".getBytes()))
-                               .length(6)
-                               .uri("KSK@foo.txt")
-                               .execute();
-                       connectNode();
-                       readMessage("Hello", this::matchesDirectClientPut);
-               }
-
-               @Test
-               public void succeedsOnCorrectIdentifier() throws InterruptedException, ExecutionException, IOException {
-                       Future<Optional<Key>> key = fcpClient.clientPut()
-                               .from(new ByteArrayInputStream("Hello\n".getBytes()))
-                               .length(6)
-                               .uri("KSK@foo.txt")
-                               .execute();
-                       connectNode();
-                       readMessage("Hello", this::matchesDirectClientPut);
-                       replyWithPutFailed("not-the-right-one");
-                       replyWithPutSuccessful(identifier);
-                       assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
-               }
-
-               @Test
-               public void failsOnCorrectIdentifier() throws InterruptedException, ExecutionException, IOException {
-                       Future<Optional<Key>> key = fcpClient.clientPut()
-                               .from(new ByteArrayInputStream("Hello\n".getBytes()))
-                               .length(6)
-                               .uri("KSK@foo.txt")
-                               .execute();
-                       connectNode();
-                       readMessage("Hello", this::matchesDirectClientPut);
-                       replyWithPutSuccessful("not-the-right-one");
-                       replyWithPutFailed(identifier);
-                       assertThat(key.get().isPresent(), is(false));
-               }
-
-               @Test
-               public void renameIsSentCorrectly() throws InterruptedException, ExecutionException, IOException {
-                       fcpClient.clientPut()
-                               .named("otherName.txt")
-                               .from(new ByteArrayInputStream("Hello\n".getBytes()))
-                               .length(6)
-                               .uri("KSK@foo.txt")
-                               .execute();
-                       connectNode();
-                       readMessage("Hello", () -> allOf(
-                               hasHead("ClientPut"),
-                               hasParameters(1, 2, "TargetFilename=otherName.txt", "UploadFrom=direct", "DataLength=6",
-                                       "URI=KSK@foo.txt"),
-                               hasTail("EndMessage", "Hello")
-                       ));
-               }
-
-               @Test
-               public void redirectIsSentCorrecly() throws IOException, ExecutionException, InterruptedException {
-                       fcpClient.clientPut().redirectTo("KSK@bar.txt").uri("KSK@foo.txt").execute();
-                       connectAndAssert(() ->
-                               matchesFcpMessage("ClientPut", "UploadFrom=redirect", "URI=KSK@foo.txt", "TargetURI=KSK@bar.txt"));
-               }
-
-               @Test
-               public void withFileIsSentCorrectly() throws InterruptedException, ExecutionException, IOException {
-                       fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
-                       connectAndAssert(() ->
-                               matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt", "Filename=/tmp/data.txt"));
-               }
-
-               public class DDA {
+       public class PutCommands {
 
-                       private final File ddaFile;
-                       private final File fileToUpload;
+               public class ClientPut {
 
-                       public DDA() throws IOException {
-                               ddaFile = createDdaFile();
-                               fileToUpload = new File(ddaFile.getParent(), "test.dat");
+                       @Test
+                       public void sendsCorrectCommand() throws IOException, ExecutionException, InterruptedException {
+                               fcpClient.clientPut()
+                                       .from(new ByteArrayInputStream("Hello\n".getBytes()))
+                                       .length(6)
+                                       .uri("KSK@foo.txt")
+                                       .execute();
+                               connectNode();
+                               readMessage("Hello", this::matchesDirectClientPut);
                        }
 
-                       private Matcher<List<String>> matchesFileClientPut(File file) {
-                               return matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt", "Filename=" + file);
+                       @Test
+                       public void succeedsOnCorrectIdentifier() throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<Key>> key = fcpClient.clientPut()
+                                       .from(new ByteArrayInputStream("Hello\n".getBytes()))
+                                       .length(6)
+                                       .uri("KSK@foo.txt")
+                                       .execute();
+                               connectNode();
+                               readMessage("Hello", this::matchesDirectClientPut);
+                               replyWithPutFailed("not-the-right-one");
+                               replyWithPutSuccessful(identifier);
+                               assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
                        }
 
                        @Test
-                       public void completeDda() throws IOException, ExecutionException, InterruptedException {
-                               fcpClient.clientPut().from(fileToUpload).uri("KSK@foo.txt").execute();
-                               connectAndAssert(() -> matchesFileClientPut(fileToUpload));
-                               sendDdaRequired(identifier);
-                               readMessage(() -> matchesTestDDARequest(ddaFile));
-                               sendTestDDAReply(ddaFile.getParent(), ddaFile);
-                               readMessage(() -> matchesTestDDAResponse(ddaFile));
-                               writeTestDDAComplete(ddaFile);
-                               readMessage(() -> matchesFileClientPut(fileToUpload));
+                       public void failsOnCorrectIdentifier() throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<Key>> key = fcpClient.clientPut()
+                                       .from(new ByteArrayInputStream("Hello\n".getBytes()))
+                                       .length(6)
+                                       .uri("KSK@foo.txt")
+                                       .execute();
+                               connectNode();
+                               readMessage("Hello", this::matchesDirectClientPut);
+                               replyWithPutSuccessful("not-the-right-one");
+                               replyWithPutFailed(identifier);
+                               assertThat(key.get().isPresent(), is(false));
                        }
 
                        @Test
-                       public void ignoreOtherDda() throws IOException, ExecutionException, InterruptedException {
-                               fcpClient.clientPut().from(fileToUpload).uri("KSK@foo.txt").execute();
-                               connectAndAssert(() -> matchesFileClientPut(fileToUpload));
-                               sendDdaRequired(identifier);
-                               readMessage(() -> matchesTestDDARequest(ddaFile));
-                               sendTestDDAReply("/some-other-directory", ddaFile);
-                               sendTestDDAReply(ddaFile.getParent(), ddaFile);
-                               readMessage(() -> matchesTestDDAResponse(ddaFile));
+                       public void renameIsSentCorrectly() throws InterruptedException, ExecutionException, IOException {
+                               fcpClient.clientPut()
+                                       .named("otherName.txt")
+                                       .from(new ByteArrayInputStream("Hello\n".getBytes()))
+                                       .length(6)
+                                       .uri("KSK@foo.txt")
+                                       .execute();
+                               connectNode();
+                               readMessage("Hello", () -> allOf(
+                                       hasHead("ClientPut"),
+                                       hasParameters(1, 2, "TargetFilename=otherName.txt", "UploadFrom=direct", "DataLength=6",
+                                               "URI=KSK@foo.txt"),
+                                       hasTail("EndMessage", "Hello")
+                               ));
                        }
 
                        @Test
-                       public void sendResponseIfFileUnreadable() throws IOException, ExecutionException, InterruptedException {
-                               fcpClient.clientPut().from(fileToUpload).uri("KSK@foo.txt").execute();
-                               connectAndAssert(() -> matchesFileClientPut(fileToUpload));
-                               sendDdaRequired(identifier);
-                               readMessage(() -> matchesTestDDARequest(ddaFile));
-                               sendTestDDAReply(ddaFile.getParent(), new File(ddaFile + ".foo"));
-                               readMessage(this::matchesFailedToReadResponse);
+                       public void redirectIsSentCorrecly() throws IOException, ExecutionException, InterruptedException {
+                               fcpClient.clientPut().redirectTo("KSK@bar.txt").uri("KSK@foo.txt").execute();
+                               connectAndAssert(() ->
+                                       matchesFcpMessage("ClientPut", "UploadFrom=redirect", "URI=KSK@foo.txt", "TargetURI=KSK@bar.txt"));
                        }
 
                        @Test
-                       public void clientPutDoesNotResendOriginalClientPutOnTestDDACompleteWithWrongDirectory()
-                       throws IOException, ExecutionException, InterruptedException {
-                               fcpClient.clientPut().from(fileToUpload).uri("KSK@foo.txt").execute();
-                               connectNode();
-                               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
-                               String identifier = extractIdentifier(lines);
-                               fcpServer.writeLine(
-                                       "TestDDAComplete",
-                                       "Directory=/some-other-directory",
-                                       "EndMessage"
-                               );
-                               sendDdaRequired(identifier);
-                               lines = fcpServer.collectUntil(is("EndMessage"));
-                               assertThat(lines, matchesFcpMessage(
-                                       "TestDDARequest",
-                                       "Directory=" + ddaFile.getParent(),
-                                       "WantReadDirectory=true",
-                                       "WantWriteDirectory=false"
-                               ));
+                       public void withFileIsSentCorrectly() throws InterruptedException, ExecutionException, IOException {
+                               fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
+                               connectAndAssert(() ->
+                                       matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt", "Filename=/tmp/data.txt"));
                        }
 
-                       private Matcher<List<String>> matchesFailedToReadResponse() {
-                               return matchesFcpMessage(
-                                       "TestDDAResponse",
-                                       "Directory=" + ddaFile.getParent(),
-                                       "ReadContent=failed-to-read"
-                               );
+                       public class DDA {
+
+                               private final File ddaFile;
+                               private final File fileToUpload;
+
+                               public DDA() throws IOException {
+                                       ddaFile = createDdaFile();
+                                       fileToUpload = new File(ddaFile.getParent(), "test.dat");
+                               }
+
+                               private Matcher<List<String>> matchesFileClientPut(File file) {
+                                       return matchesFcpMessage("ClientPut", "UploadFrom=disk", "URI=KSK@foo.txt", "Filename=" + file);
+                               }
+
+                               @Test
+                               public void completeDda() throws IOException, ExecutionException, InterruptedException {
+                                       fcpClient.clientPut().from(fileToUpload).uri("KSK@foo.txt").execute();
+                                       connectAndAssert(() -> matchesFileClientPut(fileToUpload));
+                                       sendDdaRequired(identifier);
+                                       readMessage(() -> matchesTestDDARequest(ddaFile));
+                                       sendTestDDAReply(ddaFile.getParent(), ddaFile);
+                                       readMessage(() -> matchesTestDDAResponse(ddaFile));
+                                       writeTestDDAComplete(ddaFile);
+                                       readMessage(() -> matchesFileClientPut(fileToUpload));
+                               }
+
+                               @Test
+                               public void ignoreOtherDda() throws IOException, ExecutionException, InterruptedException {
+                                       fcpClient.clientPut().from(fileToUpload).uri("KSK@foo.txt").execute();
+                                       connectAndAssert(() -> matchesFileClientPut(fileToUpload));
+                                       sendDdaRequired(identifier);
+                                       readMessage(() -> matchesTestDDARequest(ddaFile));
+                                       sendTestDDAReply("/some-other-directory", ddaFile);
+                                       sendTestDDAReply(ddaFile.getParent(), ddaFile);
+                                       readMessage(() -> matchesTestDDAResponse(ddaFile));
+                               }
+
+                               @Test
+                               public void sendResponseIfFileUnreadable()
+                               throws IOException, ExecutionException, InterruptedException {
+                                       fcpClient.clientPut().from(fileToUpload).uri("KSK@foo.txt").execute();
+                                       connectAndAssert(() -> matchesFileClientPut(fileToUpload));
+                                       sendDdaRequired(identifier);
+                                       readMessage(() -> matchesTestDDARequest(ddaFile));
+                                       sendTestDDAReply(ddaFile.getParent(), new File(ddaFile + ".foo"));
+                                       readMessage(this::matchesFailedToReadResponse);
+                               }
+
+                               @Test
+                               public void clientPutDoesNotResendOriginalClientPutOnTestDDACompleteWithWrongDirectory()
+                               throws IOException, ExecutionException, InterruptedException {
+                                       fcpClient.clientPut().from(fileToUpload).uri("KSK@foo.txt").execute();
+                                       connectNode();
+                                       List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+                                       String identifier = extractIdentifier(lines);
+                                       fcpServer.writeLine(
+                                               "TestDDAComplete",
+                                               "Directory=/some-other-directory",
+                                               "EndMessage"
+                                       );
+                                       sendDdaRequired(identifier);
+                                       lines = fcpServer.collectUntil(is("EndMessage"));
+                                       assertThat(lines, matchesFcpMessage(
+                                               "TestDDARequest",
+                                               "Directory=" + ddaFile.getParent(),
+                                               "WantReadDirectory=true",
+                                               "WantWriteDirectory=false"
+                                       ));
+                               }
+
+                               private Matcher<List<String>> matchesFailedToReadResponse() {
+                                       return matchesFcpMessage(
+                                               "TestDDAResponse",
+                                               "Directory=" + ddaFile.getParent(),
+                                               "ReadContent=failed-to-read"
+                                       );
+                               }
+
+                               private void writeTestDDAComplete(File tempFile) throws IOException {
+                                       fcpServer.writeLine(
+                                               "TestDDAComplete",
+                                               "Directory=" + tempFile.getParent(),
+                                               "ReadDirectoryAllowed=true",
+                                               "EndMessage"
+                                       );
+                               }
+
+                               private Matcher<List<String>> matchesTestDDAResponse(File tempFile) {
+                                       return matchesFcpMessage(
+                                               "TestDDAResponse",
+                                               "Directory=" + tempFile.getParent(),
+                                               "ReadContent=test-content"
+                                       );
+                               }
+
+                               private void sendTestDDAReply(String directory, File tempFile) throws IOException {
+                                       fcpServer.writeLine(
+                                               "TestDDAReply",
+                                               "Directory=" + directory,
+                                               "ReadFilename=" + tempFile,
+                                               "EndMessage"
+                                       );
+                               }
+
+                               private Matcher<List<String>> matchesTestDDARequest(File tempFile) {
+                                       return matchesFcpMessage(
+                                               "TestDDARequest",
+                                               "Directory=" + tempFile.getParent(),
+                                               "WantReadDirectory=true",
+                                               "WantWriteDirectory=false"
+                                       );
+                               }
+
+                               private void sendDdaRequired(String identifier) throws IOException {
+                                       fcpServer.writeLine(
+                                               "ProtocolError",
+                                               "Identifier=" + identifier,
+                                               "Code=25",
+                                               "EndMessage"
+                                       );
+                               }
+
                        }
 
-                       private void writeTestDDAComplete(File tempFile) throws IOException {
+                       private void replyWithPutFailed(String identifier) throws IOException {
                                fcpServer.writeLine(
-                                       "TestDDAComplete",
-                                       "Directory=" + tempFile.getParent(),
-                                       "ReadDirectoryAllowed=true",
+                                       "PutFailed",
+                                       "Identifier=" + identifier,
                                        "EndMessage"
                                );
                        }
 
-                       private Matcher<List<String>> matchesTestDDAResponse(File tempFile) {
-                               return matchesFcpMessage(
-                                       "TestDDAResponse",
-                                       "Directory=" + tempFile.getParent(),
-                                       "ReadContent=test-content"
+                       private Matcher<List<String>> matchesDirectClientPut(String... additionalLines) {
+                               List<String> lines =
+                                       new ArrayList<>(Arrays.asList("UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt"));
+                               Arrays.asList(additionalLines).forEach(lines::add);
+                               return allOf(
+                                       hasHead("ClientPut"),
+                                       hasParameters(1, 2, lines.toArray(new String[lines.size()])),
+                                       hasTail("EndMessage", "Hello")
                                );
                        }
 
-                       private void sendTestDDAReply(String directory, File tempFile) throws IOException {
+                       private File createDdaFile() throws IOException {
+                               File tempFile = File.createTempFile("test-dda-", ".dat");
+                               tempFile.deleteOnExit();
+                               Files.write("test-content", tempFile, StandardCharsets.UTF_8);
+                               return tempFile;
+                       }
+
+                       @Test
+                       public void clientPutDoesNotReactToProtocolErrorForDifferentIdentifier()
+                       throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<Key>> key =
+                                       fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
+                               connectNode();
+                               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+                               String identifier = extractIdentifier(lines);
                                fcpServer.writeLine(
-                                       "TestDDAReply",
-                                       "Directory=" + directory,
-                                       "ReadFilename=" + tempFile,
+                                       "ProtocolError",
+                                       "Identifier=not-the-right-one",
+                                       "Code=25",
                                        "EndMessage"
                                );
-                       }
-
-                       private Matcher<List<String>> matchesTestDDARequest(File tempFile) {
-                               return matchesFcpMessage(
-                                       "TestDDARequest",
-                                       "Directory=" + tempFile.getParent(),
-                                       "WantReadDirectory=true",
-                                       "WantWriteDirectory=false"
+                               fcpServer.writeLine(
+                                       "PutSuccessful",
+                                       "Identifier=" + identifier,
+                                       "URI=KSK@foo.txt",
+                                       "EndMessage"
                                );
+                               assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
                        }
 
-                       private void sendDdaRequired(String identifier) throws IOException {
+                       @Test
+                       public void clientPutAbortsOnProtocolErrorOtherThan25()
+                       throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<Key>> key =
+                                       fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
+                               connectNode();
+                               List<String> lines = fcpServer.collectUntil(is("EndMessage"));
+                               String identifier = extractIdentifier(lines);
                                fcpServer.writeLine(
                                        "ProtocolError",
                                        "Identifier=" + identifier,
-                                       "Code=25",
+                                       "Code=1",
                                        "EndMessage"
                                );
+                               assertThat(key.get().isPresent(), is(false));
+                       }
+
+                       @Test
+                       public void clientPutSendsNotificationsForGeneratedKeys()
+                       throws InterruptedException, ExecutionException, IOException {
+                               List<String> generatedKeys = new CopyOnWriteArrayList<>();
+                               Future<Optional<Key>> key = fcpClient.clientPut()
+                                       .onKeyGenerated(generatedKeys::add)
+                                       .from(new ByteArrayInputStream("Hello\n".getBytes()))
+                                       .length(6)
+                                       .uri("KSK@foo.txt")
+                                       .execute();
+                               connectNode();
+                               readMessage("Hello", this::matchesDirectClientPut);
+                               replyWithGeneratedUri();
+                               replyWithPutSuccessful(identifier);
+                               assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
+                               assertThat(generatedKeys, contains("KSK@foo.txt"));
+                       }
+
+                       @Test
+                       public void clientPutSendsNotificationOnProgress()
+                       throws InterruptedException, ExecutionException, IOException {
+                               List<RequestProgress> requestProgress = new ArrayList<>();
+                               Future<Optional<Key>> key = fcpClient.clientPut()
+                                       .onProgress(requestProgress::add)
+                                       .from(new ByteArrayInputStream("Hello\n".getBytes()))
+                                       .length(6)
+                                       .uri("KSK@foo.txt")
+                                       .execute();
+                               connectNode();
+                               readMessage("Hello", () -> matchesDirectClientPut("Verbosity=1"));
+                               replyWithSimpleProgress(1, 2, 3, 4, 5, 6, true, 8);
+                               replyWithSimpleProgress(11, 12, 13, 14, 15, 16, false, 18);
+                               replyWithPutSuccessful(identifier);
+                               assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
+                               assertThat(requestProgress, contains(
+                                       isRequestProgress(1, 2, 3, 4, 5, 6, true, 8),
+                                       isRequestProgress(11, 12, 13, 14, 15, 16, false, 18)
+                               ));
                        }
 
                }
@@ -1514,89 +1679,85 @@ public class DefaultFcpClientTest {
                        );
                }
 
-               private void replyWithPutFailed(String identifier) throws IOException {
+               private void replyWithGeneratedUri() throws IOException {
                        fcpServer.writeLine(
-                               "PutFailed",
-                               "Identifier=" + identifier,
-                               "EndMessage"
-                       );
-               }
-
-               private Matcher<List<String>> matchesDirectClientPut() {
-                       return allOf(
-                               hasHead("ClientPut"),
-                               hasParameters(1, 2, "UploadFrom=direct", "DataLength=6", "URI=KSK@foo.txt"),
-                               hasTail("EndMessage", "Hello")
-                       );
-               }
-
-               private File createDdaFile() throws IOException {
-                       File tempFile = File.createTempFile("test-dda-", ".dat");
-                       tempFile.deleteOnExit();
-                       Files.write("test-content", tempFile, StandardCharsets.UTF_8);
-                       return tempFile;
-               }
-
-               @Test
-               public void clientPutDoesNotReactToProtocolErrorForDifferentIdentifier()
-               throws InterruptedException, ExecutionException, IOException {
-                       Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
-                       connectNode();
-                       List<String> lines = fcpServer.collectUntil(is("EndMessage"));
-                       String identifier = extractIdentifier(lines);
-                       fcpServer.writeLine(
-                               "ProtocolError",
-                               "Identifier=not-the-right-one",
-                               "Code=25",
-                               "EndMessage"
-                       );
-                       fcpServer.writeLine(
-                               "PutSuccessful",
+                               "URIGenerated",
                                "Identifier=" + identifier,
                                "URI=KSK@foo.txt",
                                "EndMessage"
                        );
-                       assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
                }
 
-               @Test
-               public void clientPutAbortsOnProtocolErrorOtherThan25()
-               throws InterruptedException, ExecutionException, IOException {
-                       Future<Optional<Key>> key = fcpClient.clientPut().from(new File("/tmp/data.txt")).uri("KSK@foo.txt").execute();
-                       connectNode();
-                       List<String> lines = fcpServer.collectUntil(is("EndMessage"));
-                       String identifier = extractIdentifier(lines);
+               private void replyWithSimpleProgress(
+                       int total, int required, int failed, int fatallyFailed, int succeeded, int lastProgress,
+                       boolean finalizedTotal, int minSuccessFetchBlocks) throws IOException {
                        fcpServer.writeLine(
-                               "ProtocolError",
+                               "SimpleProgress",
                                "Identifier=" + identifier,
-                               "Code=1",
+                               "Total=" + total,
+                               "Required=" + required,
+                               "Failed=" + failed,
+                               "FatallyFailed=" + fatallyFailed,
+                               "Succeeded=" + succeeded,
+                               "LastProgress=" + lastProgress,
+                               "FinalizedTotal=" + finalizedTotal,
+                               "MinSuccessFetchBlocks=" + minSuccessFetchBlocks,
                                "EndMessage"
                        );
-                       assertThat(key.get().isPresent(), is(false));
                }
 
-               @Test
-               public void clientPutSendsNotificationsForGeneratedKeys()
-               throws InterruptedException, ExecutionException, IOException {
-                       List<String> generatedKeys = new CopyOnWriteArrayList<>();
-                       Future<Optional<Key>> key = fcpClient.clientPut()
-                               .onKeyGenerated(generatedKeys::add)
-                               .from(new ByteArrayInputStream("Hello\n".getBytes()))
-                               .length(6)
-                               .uri("KSK@foo.txt")
-                               .execute();
-                       connectNode();
-                       List<String> lines = fcpServer.collectUntil(is("Hello"));
-                       String identifier = extractIdentifier(lines);
-                       fcpServer.writeLine(
-                               "URIGenerated",
-                               "Identifier=" + identifier,
-                               "URI=KSK@foo.txt",
-                               "EndMessage"
-                       );
-                       replyWithPutSuccessful(identifier);
-                       assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
-                       assertThat(generatedKeys, contains("KSK@foo.txt"));
+               public class ClientPutDiskDir {
+
+                       @Test
+                       public void commandIsSentCorrectly() throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<Key>> key = fcpClient.clientPutDiskDir().fromDirectory(new File("")).uri("CHK@").execute();
+                               connectAndAssert(this::matchesClientPutDiskDir);
+                               fcpServer.writeLine("PutSuccessful", "Identifier=" + identifier, "URI=CHK@abc", "EndMessage");
+                               assertThat(key.get().get().getKey(), is("CHK@abc"));
+                       }
+
+                       @Test
+                       public void protocolErrorAbortsCommand() throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<Key>> key = fcpClient.clientPutDiskDir().fromDirectory(new File("")).uri("CHK@").execute();
+                               connectAndAssert(this::matchesClientPutDiskDir);
+                               replyWithProtocolError();
+                               assertThat(key.get().isPresent(), is(false));
+                       }
+
+                       @Test
+                       public void progressIsSentToConsumerCorrectly() throws InterruptedException, ExecutionException, IOException {
+                               List<RequestProgress> requestProgress = new ArrayList<>();
+                               Future<Optional<Key>> key = fcpClient.clientPutDiskDir().onProgress(requestProgress::add)
+                                       .fromDirectory(new File("")).uri("CHK@").execute();
+                               connectAndAssert(() -> matchesClientPutDiskDir("Verbosity=1"));
+                               replyWithSimpleProgress(1, 2, 3, 4, 5, 6, true, 8);
+                               replyWithSimpleProgress(11, 12, 13, 14, 15, 16, false, 18);
+                               replyWithPutSuccessful(identifier);
+                               assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
+                               assertThat(requestProgress, contains(
+                                       isRequestProgress(1, 2, 3, 4, 5, 6, true, 8),
+                                       isRequestProgress(11, 12, 13, 14, 15, 16, false, 18)
+                               ));
+                       }
+
+                       @Test
+                       public void generatedUriIsSentToConsumerCorrectly() throws InterruptedException, ExecutionException, IOException {
+                               List<String> generatedKeys = new ArrayList<>();
+                               Future<Optional<Key>> key = fcpClient.clientPutDiskDir().onKeyGenerated(generatedKeys::add)
+                                       .fromDirectory(new File("")).uri("CHK@").execute();
+                               connectAndAssert(this::matchesClientPutDiskDir);
+                               replyWithGeneratedUri();
+                               replyWithPutSuccessful(identifier);
+                               assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
+                               assertThat(generatedKeys, contains("KSK@foo.txt"));
+                       }
+
+                       private Matcher<List<String>> matchesClientPutDiskDir(String... additionalLines) {
+                               List<String> lines = new ArrayList<>(Arrays.asList("Identifier=" + identifier, "URI=CHK@", "Filename=" + new File("").getPath()));
+                               Arrays.asList(additionalLines).forEach(lines::add);
+                               return matchesFcpMessage("ClientPutDiskDir", lines.toArray(new String[lines.size()]));
+                       }
+
                }
 
        }