Class: HttpDouble::Base

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/http_double/base.rb

Class Method Summary collapse

Class Method Details

.background(addr, port, **args, &block) ⇒ Object



35
36
37
38
39
40
# File 'lib/http_double/base.rb', line 35

def background(addr, port, **args, &block)
  thread = Thread.new{ foreground addr, port, **args, &block }
  thread.abort_on_exception = true
  sleep 0.05 until test_background addr, port
  thread
end

.foreground(addr, port, log_path: '/dev/null', logger: nil, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/http_double/base.rb', line 15

def foreground(addr, port, log_path: '/dev/null', logger: nil, &block)
  server_class = self
  app_class = @app_class || server_class

  # Even if we use /dev/null, we need a logger to suppress warnings
  # generated by Thin::Logging#trace_logger being undefined
  log_stream = File.open(log_path, 'a')
  Base.loggers[port] = logger || Logger.new(log_stream)

  initializer = @initializer

  Thin::Server.start(addr, port) do
    initializer.call if initializer
    use RequestLogger, server_class.log
    instance_exec &block if block
    run Class === app_class ? app_class.new : app_class
  end

end

.logObject



42
43
44
# File 'lib/http_double/base.rb', line 42

def log
  @log ||= []
end

.loggersObject



46
47
48
# File 'lib/http_double/base.rb', line 46

def loggers
  @loggers ||= {}
end

.use_app(klass, &block) ⇒ Object



10
11
12
13
# File 'lib/http_double/base.rb', line 10

def use_app(klass, &block)
  @app_class = klass
  @initializer = block
end