Sha256: 74b0fcb9d6aaeac71d17146f6485045707c0da421cf150369a79e229022675d3

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require 'find_a_port'
require 'pact/consumer/mock_service'

module Pact
  module MockService
    class Run

      def self.call options
        new(options).call
      end

      def initialize options
        @options = options
      end

      def call
        require 'pact/consumer/mock_service'

        trap(:INT) { call_shutdown_hooks  }
        trap(:TERM) { call_shutdown_hooks }

        Rack::Handler::WEBrick.run(mock_service, webbrick_opts)
      end

      private

      attr_reader :options

      def mock_service
        @mock_service ||= begin
          Pact::Consumer::MockService.new(service_options)
        end
      end

      def call_shutdown_hooks
        unless @shutting_down
          @shutting_down = true
          begin
            mock_service.shutdown
          ensure
            Rack::Handler::WEBrick.shutdown
          end
        end
      end

      def service_options
        service_options = {
          pact_dir: options[:pact_dir],
          consumer: options[:consumer],
          provider: options[:provider],
          cors_enabled: options[:cors]
        }
        service_options[:log_file] = open_log_file if options[:log]
        service_options
      end

      def open_log_file
        FileUtils.mkdir_p File.dirname(options[:log])
        log = File.open(options[:log], 'w')
        log.sync = true
        log
      end

      def webbrick_opts
        opts = {
          :Port => options[:port] || FindAPort.available_port,
          :AccessLog => []
        }
        opts.merge!(ssl_opts) if options[:ssl]
        opts
      end

      def ssl_opts
        {
          :SSLEnable => true,
          :SSLCertName => [ %w[CN localhost] ]
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pact-mock_service-0.2.4 lib/pact/mock_service/run.rb