Skip to content

Commit f89c8e9

Browse files
authored
πŸ”€ Merge pull request #415 from ruby/fake_server-improvements
βœ… Improvements to FakeServer (tests only)
2 parents 78d46f2 + 84e7576 commit f89c8e9

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

β€Žtest/net/imap/fake_server/command_reader.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "net/imap"
44

55
class Net::IMAP::FakeServer
6+
CommandParseError = RuntimeError
67

78
class CommandReader
89
attr_reader :last_command
@@ -23,6 +24,8 @@ def get_command
2324
end
2425
throw :eof if buf.empty?
2526
@last_command = parse(buf)
27+
rescue CommandParseError => err
28+
raise IOError, err.message if socket.eof? && !buf.end_with?("\r\n")
2629
end
2730

2831
private
@@ -32,7 +35,7 @@ def get_command
3235
# TODO: convert bad command exception to tagged BAD response, when possible
3336
def parse(buf)
3437
/\A([^ ]+) ((?:UID )?\w+)(?: (.+))?\r\n\z/min =~ buf or
35-
raise "bad request: %p" [buf]
38+
raise CommandParseError, "bad request: %p" [buf]
3639
case $2.upcase
3740
when "LOGIN", "SELECT", "EXAMINE", "ENABLE", "AUTHENTICATE"
3841
Command.new $1, $2, scan_astrings($3), buf

β€Žtest/net/imap/fake_server/command_router.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ module Routable
1010
def on(*command_names, &handler)
1111
scope = self.is_a?(Module) ? self : singleton_class
1212
command_names.each do |command_name|
13-
scope.define_method("handle_#{command_name.downcase}", &handler)
13+
method_name = :"handle_#{command_name.downcase}"
14+
scope.undef_method(method_name) if scope.method_defined?(method_name)
15+
scope.define_method(method_name, &handler)
1416
end
1517
end
1618
end

β€Žtest/net/imap/fake_server/socket.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def initialize(tcp_socket, config:)
1818
def tls?; !!@tls_socket end
1919
def closed?; @closed end
2020

21+
def eof?; socket.eof? end
2122
def gets(...) socket.gets(...) end
2223
def read(...) socket.read(...) end
2324
def print(...) socket.print(...) end

β€Žtest/net/imap/fake_server/test_helper.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
module Net::IMAP::FakeServer::TestHelper
66

7-
def run_fake_server_in_thread(ignore_io_error: false, timeout: 10, **opts)
7+
def run_fake_server_in_thread(ignore_io_error: false,
8+
report_on_exception: true,
9+
timeout: 10, **opts)
810
Timeout.timeout(timeout) do
911
server = Net::IMAP::FakeServer.new(timeout: timeout, **opts)
1012
@threads << Thread.new do
13+
Thread.current.abort_on_exception = false
14+
Thread.current.report_on_exception = report_on_exception
1115
server.run
1216
rescue IOError
1317
raise unless ignore_io_error

0 commit comments

Comments
Β (0)