Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion test/net/imap/fake_server/command_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "net/imap"

class Net::IMAP::FakeServer
CommandParseError = RuntimeError

class CommandReader
attr_reader :last_command
Expand All @@ -22,6 +23,8 @@ def get_command
buf << socket.read(Integer($1))
end
@last_command = parse(buf)
rescue CommandParseError => err
raise IOError, err.message if socket.eof? && !buf.end_with?("\r\n")
end

private
Expand All @@ -31,7 +34,7 @@ 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: %p" [buf]
raise CommandParseError, "bad request: %p" [buf]
case $2.upcase
when "LOGIN", "SELECT", "EXAMINE", "ENABLE", "AUTHENTICATE"
Command.new $1, $2, scan_astrings($3), buf
Expand Down
4 changes: 3 additions & 1 deletion test/net/imap/fake_server/command_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module Routable
def on(*command_names, &handler)
scope = self.is_a?(Module) ? self : singleton_class
command_names.each do |command_name|
scope.define_method("handle_#{command_name.downcase}", &handler)
method_name = :"handle_#{command_name.downcase}"
scope.undef_method(method_name) if scope.method_defined?(method_name)
scope.define_method(method_name, &handler)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/net/imap/fake_server/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def initialize(tcp_socket, config:)
def tls?; !!@tls_socket end
def closed?; @closed end

def eof?; socket.eof? end
def gets(...) socket.gets(...) end
def read(...) socket.read(...) end
def print(...) socket.print(...) end
Expand Down
6 changes: 5 additions & 1 deletion test/net/imap/fake_server/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

module Net::IMAP::FakeServer::TestHelper

def run_fake_server_in_thread(ignore_io_error: false, timeout: 10, **opts)
def run_fake_server_in_thread(ignore_io_error: false,
report_on_exception: true,
timeout: 10, **opts)
Timeout.timeout(timeout) do
server = Net::IMAP::FakeServer.new(timeout: timeout, **opts)
@threads << Thread.new do
Thread.current.abort_on_exception = false
Thread.current.report_on_exception = report_on_exception
server.run
rescue IOError
raise unless ignore_io_error
Expand Down