From 4b54a0755f587db3e01323e2f9465b6296786f15 Mon Sep 17 00:00:00 2001 From: nick evans Date: Fri, 22 Sep 2023 18:09:17 -0400 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=85=20Add=20more=20helpful=20error=20?= =?UTF-8?q?message=20for=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/net/imap/fake_server/command_reader.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/net/imap/fake_server/command_reader.rb b/test/net/imap/fake_server/command_reader.rb index 2fd15ef2..ecfef530 100644 --- a/test/net/imap/fake_server/command_reader.rb +++ b/test/net/imap/fake_server/command_reader.rb @@ -30,7 +30,8 @@ def get_command # TODO: convert bad command exception to tagged BAD response, when possible def parse(buf) - /\A([^ ]+) ((?:UID )?\w+)(?: (.+))?\r\n\z/min =~ buf or raise "bad request" + /\A([^ ]+) ((?:UID )?\w+)(?: (.+))?\r\n\z/min =~ buf or + raise "bad request: %p" [buf] case $2.upcase when "LOGIN", "SELECT", "ENABLE", "AUTHENTICATE" Command.new $1, $2, scan_astrings($3), buf From fa36434f5c4e21972cfd6e50b47a6026cdcf62e0 Mon Sep 17 00:00:00 2001 From: nick evans Date: Fri, 22 Sep 2023 16:35:25 -0400 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8=20Add=20`#logout!`=20to=20combine?= =?UTF-8?q?=20logout=20and=20disconnect?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A logout command that also disconnects, even if logout raises an exception, and doesn't raise exceptions when already disconnected would be very useful in situations where the connection MUST be gracefully closed, e.g. for security or tests. Although the change is relatively insignificant, changing #logout to also ignore exceptions and immediately call #disconnect would be backwards incompatible. So a new method was created. --- lib/net/imap.rb | 27 ++++++++++++++++++++++-- test/net/imap/fake_server/test_helper.rb | 3 +-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/net/imap.rb b/lib/net/imap.rb index a3ebbb86..2c5d8172 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -868,7 +868,7 @@ def client_thread # :nodoc: # Disconnects from the server. # - # Related: #logout + # Related: #logout, #logout! def disconnect return if disconnected? begin @@ -1067,11 +1067,34 @@ def noop # to inform the command to inform the server that the client is done with # the connection. # - # Related: #disconnect + # Related: #disconnect, #logout! def logout send_command("LOGOUT") end + # Calls #logout then, after receiving the TaggedResponse for the +LOGOUT+, + # calls #disconnect. Returns the TaggedResponse from +LOGOUT+. Returns + # +nil+ when the client is already disconnected, in contrast to #logout + # which raises an exception. + # + # If #logout raises a StandardError, a warning will be printed but the + # exception will not be re-raised. + # + # This is useful in situations where the connection must be dropped, for + # example for security or after tests. If logout errors need to be handled, + # use #logout and #disconnect instead. + # + # Related: #logout, #disconnect + def logout! + logout unless disconnected? + rescue => ex + warn "%s during logout!: %s" % [ + ex.class, host, port, ex + ] + ensure + disconnect + end + # Sends a {STARTTLS command [IMAP4rev1 ยง6.2.1]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.2.1] # to start a TLS session. # diff --git a/test/net/imap/fake_server/test_helper.rb b/test/net/imap/fake_server/test_helper.rb index c35b1d6a..eee6a153 100644 --- a/test/net/imap/fake_server/test_helper.rb +++ b/test/net/imap/fake_server/test_helper.rb @@ -23,8 +23,7 @@ def with_client(*args, **kwargs) yield client ensure if client && !client.disconnected? - client.logout rescue pp $! - client.disconnect unless client.disconnected? + client.logout! end end