Sha256: e311529d5de273a73c359f8da74f660c3139d144371050a02be26d906405ee50

Contents?: true

Size: 1.39 KB

Versions: 12

Compression:

Stored size: 1.39 KB

Contents

require "net/http"
require "uri"
require "waitutil"
require_relative "silently"

module RSpec
  module Support
    module App
      private

      class RunningApp
        attr_reader :port

        def initialize(port)
          @port = port
        end

        def get(path)
          Net::HTTP.get(uri(path))
        end

        private

        def uri(path)
          URI("http://0.0.0.0:#{port}#{path}")
        end
      end

      def run_app(host: "0.0.0.0", port: "30333", timeout: 5)
        cmd = "bundle exec rackup -o 0.0.0.0 -p #{port} config.ru"
        out = Tempfile.new("dry-web-roda-out")

        pid = fork {
          Bundler.with_clean_env do
            exec cmd, out: out.path, err: out.path
          end
        }

        WaitUtil.wait_for_condition "app available on #{host}:#{port}", timeout_sec: timeout do
          begin
            result = WaitUtil.send(:is_tcp_port_open, host, port, WaitUtil::DEFAULT_DELAY_SEC)

            result || begin
              out.rewind
              [false, "#{cmd} failed:\n#{out.read}"]
            end
          rescue SocketError
            out.rewind
            [false, "#{cmd} failed:\n#{out.read}"]
          end
        end

        yield RunningApp.new(port)
      ensure
        Process.kill "TERM", pid
        Process.wait pid
      end
    end
  end
end

RSpec.configure do |config|
  config.include RSpec::Support::App, type: :cli
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
dry-web-roda-0.9.1 spec/support/app.rb
dry-web-roda-0.9.0 spec/support/app.rb
dry-web-roda-0.8.0 spec/support/app.rb
dry-web-roda-0.7.5 spec/support/app.rb
dry-web-roda-0.7.4 spec/support/app.rb
dry-web-roda-0.7.3 spec/support/app.rb
dry-web-roda-0.7.2 spec/support/app.rb
dry-web-roda-0.7.1 spec/support/app.rb
dry-web-roda-0.7.0 spec/support/app.rb
dry-web-roda-0.6.3 spec/support/app.rb
dry-web-roda-0.6.2 spec/support/app.rb
dry-web-roda-0.6.1 spec/support/app.rb