Follow redirects in ClientGet
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / quelaton / DefaultFcpClientTest.java
index c8a834a..61a7d0a 100644 (file)
@@ -1299,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",
@@ -1308,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",
@@ -1605,8 +1639,7 @@ public class DefaultFcpClientTest {
                                        .uri("KSK@foo.txt")
                                        .execute();
                                connectNode();
-                               List<String> lines = fcpServer.collectUntil(is("Hello"));
-                               String identifier = extractIdentifier(lines);
+                               readMessage("Hello", this::matchesDirectClientPut);
                                replyWithGeneratedUri();
                                replyWithPutSuccessful(identifier);
                                assertThat(key.get().get().getKey(), is("KSK@foo.txt"));
@@ -1699,14 +1732,26 @@ public class DefaultFcpClientTest {
                                connectAndAssert(() -> matchesClientPutDiskDir("Verbosity=1"));
                                replyWithSimpleProgress(1, 2, 3, 4, 5, 6, true, 8);
                                replyWithSimpleProgress(11, 12, 13, 14, 15, 16, false, 18);
-                               fcpServer.writeLine("PutSuccessful", "Identifier=" + identifier, "URI=CHK@abc", "EndMessage");
-                               assertThat(key.get().get().getKey(), is("CHK@abc"));
+                               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);