diff --git a/tests/data/Makefile.inc b/tests/data/Makefile.inc
index 271e5348c3973ab44db8054c9e26f3e0ecca41c5..1636969622605d497b894d7051a1b7f4f70c3a4b 100644
--- a/tests/data/Makefile.inc
+++ b/tests/data/Makefile.inc
@@ -90,7 +90,7 @@ test635 test636 test637 test638 test639 test640 test641 test642 \
 test643         test645 test646 test647 test648 test649 test650 test651 \
 test652 test653 test654 test655 test656 test658 test659 test660 test661 \
 test662 test663 test664 test665 test666 test667 test668 test669 \
-test670 test671 test672 test673 test674 test675 test676         test678 \
+test670 test671 test672 test673 test674 test675 test676 test677 test678 \
 \
 test700 test701 test702 test703 test704 test705 test706 test707 test708 \
 test709 test710 test711 test712 test713 test714 test715 test716 test717 \
diff --git a/tests/data/test660 b/tests/data/test660
index 3b1964b906fdf29f3f262a59a7a4c70bae4b5973..b53d47bce83418b1703658ba3819892f5062dbf4 100644
--- a/tests/data/test660
+++ b/tests/data/test660
@@ -29,7 +29,6 @@ imap://%HOSTIP:%IMAPPORT/%TESTNUMBER
 <verify>
 <protocol>
 A001 CAPABILITY
-A002 LOGOUT
 </protocol>
 </verify>
 </testcase>
diff --git a/tests/data/test677 b/tests/data/test677
new file mode 100644
index 0000000000000000000000000000000000000000..e3d9041fdf48be147a58c784920b97a7817e71d9
--- /dev/null
+++ b/tests/data/test677
@@ -0,0 +1,43 @@
+<testcase>
+<info>
+<keywords>
+IMAP
+CONNECT_ONLY
+</keywords>
+</info>
+
+#
+# Server-side
+<reply>
+<data>
+</data>
+</reply>
+
+#
+# Client-side
+<client>
+<server>
+imap
+</server>
+ <name>
+IMAP with CONNECT_ONLY, custom command then exit
+ </name>
+<tool>
+lib%TESTNUMBER
+</tool>
+
+<command>
+imap://%HOSTIP:%IMAPPORT/677
+</command>
+
+</client>
+
+#
+# Verify data after the test has been "shot"
+<verify>
+<protocol>
+A001 CAPABILITY
+A1 IDLE
+</protocol>
+</verify>
+</testcase>
diff --git a/tests/ftpserver.pl b/tests/ftpserver.pl
index 9040478ec40986a1b232abf402b6f298ef5abd92..a3819508a90f0135309f799d67330494e196ad32 100755
--- a/tests/ftpserver.pl
+++ b/tests/ftpserver.pl
@@ -643,6 +643,7 @@ sub protocolsetup {
             'STATUS'     => \&STATUS_imap,
             'STORE'      => \&STORE_imap,
             'UID'        => \&UID_imap,
+            'IDLE'       => \&IDLE_imap,
         );
         %displaytext = (
             'welcome' => join("",
@@ -1587,6 +1588,13 @@ sub COPY_imap {
     return 0;
 }
 
+sub IDLE_imap {
+    logmsg "IDLE received\n";
+
+    sendcontrol "+ entering idle mode\r\n";
+    return 0;
+}
+
 sub UID_imap {
     my ($args) = @_;
     my ($command) = split(/ /, $args, 1);
diff --git a/tests/libtest/Makefile.inc b/tests/libtest/Makefile.inc
index 60cd65320a7ef7b9a67d2b5dc3f2255afc356b0d..c2d0a6e8e6d70b3ec9599bcd9472c1a6f0edc78f 100644
--- a/tests/libtest/Makefile.inc
+++ b/tests/libtest/Makefile.inc
@@ -48,7 +48,7 @@ noinst_PROGRAMS = chkhostname libauthretry libntlmconnect                \
  lib599 \
  lib643        lib645 lib650 lib651 lib652 lib653 lib654 lib655 lib658   \
  lib659 lib661 lib666 lib667 lib668 \
- lib670 lib671 lib672 lib673 lib674 lib676 lib678 \
+ lib670 lib671 lib672 lib673 lib674 lib676 lib677 lib678 \
  lib1156 \
  lib1500 lib1501 lib1502 lib1503 lib1504 lib1505 lib1506 lib1507 lib1508 \
  lib1509 lib1510 lib1511 lib1512 lib1513 lib1514 lib1515         lib1517 \
@@ -406,6 +406,10 @@ lib676_SOURCES = lib676.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS)
 lib676_LDADD = $(TESTUTIL_LIBS)
 lib676_CPPFLAGS = $(AM_CPPFLAGS)
 
+lib677_SOURCES = lib677.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS) $(MULTIBYTE)
+lib677_LDADD = $(TESTUTIL_LIBS)
+lib677_CPPFLAGS = $(AM_CPPFLAGS)
+
 lib678_SOURCES = lib678.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS) $(MULTIBYTE)
 lib678_LDADD = $(TESTUTIL_LIBS)
 lib678_CPPFLAGS = $(AM_CPPFLAGS)
diff --git a/tests/libtest/lib677.c b/tests/libtest/lib677.c
new file mode 100644
index 0000000000000000000000000000000000000000..48d5919fec65d4aa40ede30d57b809f44e5a0358
--- /dev/null
+++ b/tests/libtest/lib677.c
@@ -0,0 +1,117 @@
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at https://curl.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ***************************************************************************/
+#include "test.h"
+
+#include "testutil.h"
+#include "warnless.h"
+#include "memdebug.h"
+
+static const char cmd[] = "A1 IDLE\r\n";
+static char buf[1024];
+
+int test(char *URL)
+{
+  CURLM *mcurl;
+  CURL *curl;
+  int mrun;
+  curl_socket_t sock = CURL_SOCKET_BAD;
+  time_t start = time(NULL);
+  int state = 0;
+  ssize_t pos = 0;
+
+  curl_global_init(CURL_GLOBAL_DEFAULT);
+  mcurl = curl_multi_init();
+  if(!mcurl)
+    return 1;
+  curl = curl_easy_init();
+  if(!curl)
+    goto fail;
+
+  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
+  curl_easy_setopt(curl, CURLOPT_URL, URL);
+  curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
+  curl_multi_add_handle(mcurl, curl);
+
+  while(time(NULL) - start < 5) {
+    struct curl_waitfd waitfd;
+
+    curl_multi_perform(mcurl, &mrun);
+    for(;;) {
+      int i;
+      struct CURLMsg *m = curl_multi_info_read(mcurl, &i);
+
+      if(!m)
+        break;
+      if(m->msg == CURLMSG_DONE && m->easy_handle == curl) {
+        curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sock);
+        if(sock == CURL_SOCKET_BAD)
+          return 3;
+        printf("Connected fine, extracted socket. Moving on\n");
+      }
+    }
+
+    if(sock != CURL_SOCKET_BAD) {
+      waitfd.events = state ? CURL_WAIT_POLLIN : CURL_WAIT_POLLOUT;
+      waitfd.revents = 0;
+      curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sock);
+      waitfd.fd = sock;
+    }
+    curl_multi_wait(mcurl, &waitfd, sock == CURL_SOCKET_BAD ? 0 : 1, 500,
+                    &mrun);
+    if((sock != CURL_SOCKET_BAD) && (waitfd.revents & waitfd.events)) {
+      size_t len = 0;
+
+      if(!state) {
+        curl_easy_send(curl, cmd + pos, sizeof(cmd) - 1 - pos, &len);
+        if(len > 0)
+          pos += len;
+        else
+          pos = 0;
+        if(pos == sizeof(cmd) - 1) {
+          state++;
+          pos = 0;
+        }
+      }
+      else if(pos < (ssize_t)sizeof(buf)) {
+        curl_easy_recv(curl, buf + pos, sizeof(buf) - pos, &len);
+        if(len > 0)
+          pos += len;
+      }
+      if(len <= 0)
+        sock = CURL_SOCKET_BAD;
+    }
+  }
+
+  if(state) {
+    fwrite(buf, pos, 1, stdout);
+    putchar('\n');
+  }
+
+  curl_multi_remove_handle(mcurl, curl);
+  fail:
+  curl_easy_cleanup(curl);
+  curl_multi_cleanup(mcurl);
+
+  curl_global_cleanup();
+  return 0;
+}
+