class FakeSMTPd::HTTPServer

Attributes

port[R]
server[R]
smtpd[R]

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/fakesmtpd/server.rb, line 40
def initialize(options = {})
  @port = options.fetch(:port)
  @smtpd = options.fetch(:smtpd)
  super(
    @port,
    options[:host] || '0.0.0.0',
    options[:max_connections] || 4,
    options[:logfile],
    options[:audit] || !!ENV['FAKESMTPD_AUDIT'] || false,
    options[:debug] || !!ENV['FAKESMTPD_DEBUG'] || false
  )
end

Public Instance Methods

serve(io) click to toggle source
# File lib/fakesmtpd/server.rb, line 64
def serve(io)
  io.set_encoding('UTF-8')
  request_line = io.gets
  path = request_line.split[1]
  handle_client(request_line, path, io)
rescue => e
  handle_500(path, io, e)
end
start(*args) click to toggle source
Calls superclass method
# File lib/fakesmtpd/server.rb, line 53
def start(*args)
  super(*args)
  log "FakeSMTPd HTTP server serving on #{port}"
  log "PID=#{$$} Thread=#{Thread.current.inspect}"
end
stop(*args) click to toggle source
Calls superclass method
# File lib/fakesmtpd/server.rb, line 59
def stop(*args)
  log "FakeSMTPd HTTP server stopping"
  super(*args)
end

Private Instance Methods

handle_404(path, client) click to toggle source
# File lib/fakesmtpd/server.rb, line 161
def handle_404(path, client)
  client.puts 'HTTP/1.1 404 Not Found'
  client.puts 'Content-type: application/json;charset=utf-8'
  client.puts
  client.puts JSON.pretty_generate(
    _links: {
      self: {href: path}
    },
    error: 'Nothing is here'
  )
end
handle_500(path, client, e) click to toggle source
# File lib/fakesmtpd/server.rb, line 173
def handle_500(path, client, e)
  client.puts 'HTTP/1.1 500 Internal Server Error'
  client.puts 'Content-type: application/json;charset=utf-8'
  client.puts
  client.puts JSON.pretty_generate(
    _links: {
      self: {href: path}
    },
    error: "#{e.class.name} #{e.message}",
    backtrace: e.backtrace
  )
end
handle_clear_messages(path, client) click to toggle source
# File lib/fakesmtpd/server.rb, line 155
def handle_clear_messages(path, client)
  smtpd.messages.clear
  client.puts 'HTTP/1.1 204 No Content'
  client.puts
end
handle_client(request_line, path, client) click to toggle source
# File lib/fakesmtpd/server.rb, line 75
def handle_client(request_line, path, client)
  log request_line.chomp
  case request_line
  when /^GET \/ /
    handle_get_root(path, client)
  when /^GET \/messages /
    handle_get_messages(path, client)
  when /^GET \/messages\/([[:digit:]]+) /
    handle_get_message(path, client, $1)
  when /^DELETE \/messages /
    handle_clear_messages(path, client)
  else
    handle_404(path, client)
  end
end
handle_get_message(path, client, message_id) click to toggle source
# File lib/fakesmtpd/server.rb, line 125
def handle_get_message(path, client, message_id)
  message_file = smtpd.messages[message_id]
  if message_file
    client.puts 'HTTP/1.1 200 OK'
    client.puts 'Content-type: application/json;charset=utf-8'
    client.puts
    message = File.open(message_file, 'r:UTF-8') do |f|
      JSON.parse(f.read)
    end
    client.puts JSON.pretty_generate(
      message.merge(
        _links: {
          self: {href: path}
        },
        filename: message_file
      )
    )
  else
    client.puts 'HTTP/1.1 404 Not Found'
    client.puts 'Content-type: application/json;charset=utf-8'
    client.puts
    client.puts JSON.pretty_generate(
      _links: {
        self: {href: path}
      },
      error: "Message #{message_id.inspect} not found"
    )
  end
end
handle_get_messages(path, client) click to toggle source
# File lib/fakesmtpd/server.rb, line 103
def handle_get_messages(path, client)
  client.puts 'HTTP/1.1 200 OK'
  client.puts 'Content-type: application/json;charset=utf-8'
  client.puts
  client.puts JSON.pretty_generate(
    _links: {
      self: {href: path}
    },
    _embedded: {
      messages: smtpd.messages.to_hash.map do |message_id, filename|
        {
          _links: {
            self: {href: "/messages/#{message_id}"}
          },
          message_id: message_id,
          filename: filename
        }
      end
    }
  )
end
handle_get_root(path, client) click to toggle source
# File lib/fakesmtpd/server.rb, line 91
def handle_get_root(path, client)
  client.puts 'HTTP/1.1 200 OK'
  client.puts 'Content-type: application/json;charset=utf-8'
  client.puts
  client.puts JSON.pretty_generate(
    _links: {
      self: {href: path},
      messages: {href: '/messages'},
    }
  )
end