Sha256: e8023fe0ce116be093adc2feb3c594745aee1f582a9798c4788bcc1272efefa5

Contents?: true

Size: 1.93 KB

Versions: 7

Compression:

Stored size: 1.93 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

        HTTP_ACCESS_CONTROL_REQUEST_METHOD = "HTTP_ACCESS_CONTROL_REQUEST_METHOD".freeze
        HTTP_ACCESS_CONTROL_REQUEST_HEADERS = "HTTP_ACCESS_CONTROL_REQUEST_HEADERS".freeze
        ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin".freeze
        ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods".freeze
        ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers".freeze
        HTTP_ORIGIN = "HTTP_ORIGIN".freeze
        ALL_METHODS = "DELETE, POST, GET, HEAD, PUT, TRACE, CONNECT, PATCH".freeze
        REQUEST_METHOD = "REQUEST_METHOD".freeze
        OPTIONS = "OPTIONS".freeze
        X_PACT_MOCK_SERVICE_REGEXP = /x-pact-mock-service/i

        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 => env.fetch(HTTP_ACCESS_CONTROL_REQUEST_HEADERS, '*'),
            ACCESS_CONTROL_ALLOW_METHODS => ALL_METHODS
          }

          logger.info "Received OPTIONS request for mock service administration endpoint #{env[HTTP_ACCESS_CONTROL_REQUEST_METHOD]} #{env['PATH_INFO']}. Returning CORS headers: #{cors_headers}."
          [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_REGEXP)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pact-mock_service-2.11.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-2.10.1 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-2.10.0 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-2.9.8 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-2.9.3 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-2.9.2 lib/pact/mock_service/request_handlers/options.rb
pact-mock_service-2.9.1 lib/pact/mock_service/request_handlers/options.rb