Sha256: 0e62d9f92231c2d87465b069fa0e174039452e9cd8c5444ac433a0b5bb8272c9

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

require 'pact/consumer/mock_service/rack_request_helper'
require 'pact/consumer/mock_service/mock_service_administration_endpoint'

module Pact
  module Consumer

    class Options

      include RackRequestHelper

      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
        logger.info "Received OPTIONS request for #{env['HTTP_ACCESS_CONTROL_REQUEST_METHOD']} #{env['PATH_INFO']}. Returning CORS headers."
        [200,
         {
           'Access-Control-Allow-Origin' => '*',
           'Access-Control-Allow-Headers' => headers_from(env)["Access-Control-Request-Headers"],
           'Access-Control-Allow-Methods' => 'DELETE, POST, GET, HEAD, PUT, TRACE, CONNECT'
          },
         []
        ]
      end

      # Access-Control-Domain does not work on OPTIONs requests.
      def enable_cors?
        false
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pact-mock_service-0.2.3.pre.rc2 lib/pact/consumer/mock_service/options.rb