Class: Hyperion::FakeServer

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperion_test/fake_server.rb,
lib/hyperion_test/fake_server/types.rb,
lib/hyperion_test/fake_server/config.rb,
lib/hyperion_test/fake_server/dispatcher.rb

Defined Under Namespace

Classes: Config, Dispatcher

Constant Summary collapse

MimicRoute =
ImmutableStruct.new(:method, :path)
Rule =
ImmutableStruct.new(:mimic_route, :headers, :handler, :rest_route)
Request =
ImmutableStruct.new(:body)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ FakeServer

Returns a new instance of FakeServer.



16
17
18
19
# File 'lib/hyperion_test/fake_server.rb', line 16

def initialize(port)
  @port = port
  @rules = []
end

Instance Attribute Details

#portObject

Runs a Mimic server configured per the specified routing rules. Restarts the server when the rules change. The server must be restarted because it runs in a forked process and it is easier to kill it than try to communicate with it.



14
15
16
# File 'lib/hyperion_test/fake_server.rb', line 14

def port
  @port
end

#rulesObject

Runs a Mimic server configured per the specified routing rules. Restarts the server when the rules change. The server must be restarted because it runs in a forked process and it is easier to kill it than try to communicate with it.



14
15
16
# File 'lib/hyperion_test/fake_server.rb', line 14

def rules
  @rules
end

Instance Method Details

#configure(&configure_routes) ⇒ Object



21
22
23
24
25
26
# File 'lib/hyperion_test/fake_server.rb', line 21

def configure(&configure_routes)
  config = Config.new
  configure_routes.call(config)
  rules.concat(config.rules)
  restart_server
end

#restart_serverObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hyperion_test/fake_server.rb', line 34

def restart_server
  server = self
  dispatcher = Dispatcher.new(rules)
  if @mimic_running
    Mimic.cleanup!
    Mimic::Server.instance.instance_variable_get(:@thread).join
  end
  Mimic.mimic(port: @port) do
    # this block executes in a strange context, which is why we
    # have to close over server and dispatcher
    server.rules.map(&:mimic_route).uniq.each do |mimic_route|
      # register the route handlers. this is basically Sinatra.
      send(mimic_route.method, mimic_route.path) do
        dispatcher.dispatch(mimic_route, request)
      end
    end
  end
  @mimic_running = true
end

#teardownObject



28
29
30
31
32
# File 'lib/hyperion_test/fake_server.rb', line 28

def teardown
  rules.clear
  @mimic_running = true
  Mimic.cleanup!
end