Sha256: ae77fdd1bac81f2ea184aeb75070322f81c656ec9ddf9de586f6baad96dccaee

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# -*- encoding : utf-8 -*-
require 'reel'
require 'pacto'
require_relative 'server/settings'
require_relative 'server/proxy'

module Pacto
  module Server
    class HTTP < Reel::Server::HTTP
      attr_reader :settings, :logger
      include Proxy

      def initialize(host = '127.0.0.1', port = 3000, options = {})
        @logger = options[:pacto_logger] || Pacto.configuration.logger
        @settings = Settings::OptionHandler.new(port, @logger).handle(options)
        logger.info "Pacto Server starting on #{host}:#{port}"
        super(host, port, spy: options[:spy], &method(:on_connection))
      end

      def on_connection(connection)
        # Support multiple keep-alive requests per connection
        connection.each_request do |reel_request|
          begin
            pacto_request = # exclusive do
              Pacto::PactoRequest.new(
                headers: reel_request.headers, body: reel_request.read,
                method: reel_request.method, uri: Addressable::URI.heuristic_parse(reel_request.uri)
              )
            # end

            pacto_response = proxy_request(pacto_request)
            reel_response = ::Reel::Response.new(pacto_response.status, pacto_response.headers, pacto_response.body)
            reel_request.respond(reel_response)
          rescue WebMock::NetConnectNotAllowedError, Faraday::ConnectionFailed => e
            reel_request.respond 502, e.message
          rescue => e
            reel_request.respond 500, Pacto::Errors.formatted_trace(e)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
pacto-server-0.4.0.rc3 lib/pacto/server.rb
pacto-0.4.0.rc2 lib/pacto/server.rb