Sha256: 8da3de651d8c8748907df99967f8ead1cbfd9068c16e31df8f430c0476ea1730

Contents?: true

Size: 1.31 KB

Versions: 17

Compression:

Stored size: 1.31 KB

Contents

require 'pact/mock_service/request_handlers/base_request_handler'

module Pact
  module MockService
    module RequestHandlers

      class Options < BaseRequestHandler

        attr_reader :name, :logger, :cors_enabled

        def initialize name, logger, cors_enabled
          @name = name
          @logger = logger
          @cors_enabled = cors_enabled
        end

        def match? env
           is_options_request?(env) && (cors_enabled || is_administration_request?(env))
        end

        def respond env
          cors_headers = {
           'Access-Control-Allow-Origin' => env.fetch('HTTP_ORIGIN','*'),
           'Access-Control-Allow-Headers' => headers_from(env)["Access-Control-Request-Headers"],
           'Access-Control-Allow-Methods' => 'DELETE, POST, GET, HEAD, PUT, TRACE, CONNECT, PATCH'
          }
          logger.info "Received OPTIONS request for mock service administration endpoint #{env['HTTP_ACCESS_CONTROL_REQUEST_METHOD']} #{env['PATH_INFO']}. Returning CORS headers: #{cors_headers.to_json}."
          [200, cors_headers, []]
        end

        def is_options_request? env
          env['REQUEST_METHOD'] == 'OPTIONS'
        end

        def is_administration_request? env
          env["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"].match(/x-pact-mock-service/i)
        end

      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
pact-mock_service-2.2.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-2.1.1.pre.alpha.2 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-2.1.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-2.0.1 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-2.0.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-1.2.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-1.1.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-1.0.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-0.12.1 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-0.12.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-0.11.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-0.10.2 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-0.10.1 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-0.10.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-0.9.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-0.8.2 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-0.8.1 lib/pact/mock_service/request_handlers/options.rb