Sha256: f57856d97313b24c1b956b49993a6ba8b72fec194008a10af0cdf1565cbfead2

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

require 'rack'
require 'uri'
require 'json'
require 'pact/mock_service/logger'
require 'pact/consumer/mock_service/cors_origin_header_middleware'
require 'pact/mock_service/request_handlers'
require 'pact/consumer/mock_service/error_handler'
require 'pact/mock_service/session'

module Pact
  module MockService

    def self.new *args
      App.new(*args)
    end

    class App

      def initialize options = {}
        logger = Logger.from_options(options)
        @name = options.fetch(:name, "MockService")
        @session = Session.new(options.merge(logger: logger))
        request_handlers = RequestHandlers.new(@name, logger, @session, options)
        @app = Rack::Builder.app do
          use Pact::Consumer::MockService::ErrorHandler, logger
          use Pact::Consumer::CorsOriginHeaderMiddleware, options[:cors_enabled]
          run request_handlers
        end
      end

      def call env
        @app.call env
      end

      def shutdown
        write_pact_if_configured
      end

      def write_pact_if_configured
        consumer_contract_writer = ConsumerContractWriter.new(@session.consumer_contract_details, StdoutLogger.new)
        if consumer_contract_writer.can_write? && !@session.pact_written?
          $stdout.puts "Writing pact before shutting down"
          consumer_contract_writer.write
        end
      end

      def to_s
        "#{@name} #{super.to_s}"
      end
    end

    # Can't write to a file in a TRAP, might deadlock
    # Not sure why we can still write to the pact file though
    class StdoutLogger
      def info message
        $stdout.puts "\n#{message}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pact-mock_service-2.3.0 lib/pact/mock_service/app.rb