Sha256: 4317925c496923f7640fdb58d3f9201f8b06a88b0fd74d4ab36cb0ea89319f77

Contents?: true

Size: 1.51 KB

Versions: 8

Compression:

Stored size: 1.51 KB

Contents

require 'hyperion_test/test_framework_hooks'
require 'hyperion/aux/hash_ext'
require 'hyperion_test/fake_server/dispatcher'
require 'hyperion_test/fake_server/types'
require 'hyperion_test/fake_server/config'

class Hyperion
  class FakeServer
    # 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.

    attr_accessor :port, :rules

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

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

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

    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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hyperion_http-0.1.9 lib/hyperion_test/fake_server.rb
hyperion_http-0.1.8 lib/hyperion_test/fake_server.rb
hyperion_http-0.1.7 lib/hyperion_test/fake_server.rb
hyperion_http-0.1.6 lib/hyperion_test/fake_server.rb
hyperion_http-0.1.5 lib/hyperion_test/fake_server.rb
hyperion_http-0.1.4 lib/hyperion_test/fake_server.rb
hyperion_http-0.1.3 lib/hyperion_test/fake_server.rb
hyperion_http-0.1.2 lib/hyperion_test/fake_server.rb