Class: Pact::MockService::RequestHandlers::Options

Inherits:
BaseRequestHandler show all
Defined in:
lib/pact/mock_service/request_handlers/options.rb

Constant Summary collapse

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_CREDENTIALS =
"Access-Control-Allow-Credentials".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
AUTHORIZATION =
"authorization".freeze
"cookie".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

Constants inherited from BaseRequestHandler

BaseRequestHandler::NOT_FOUND_RESPONSE

Constants included from Consumer::RackRequestHelper

Consumer::RackRequestHelper::REQUEST_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseRequestHandler

#call, #json_response, #text_response

Methods included from Consumer::RackRequestHelper

#params_hash, #request_as_hash_from

Constructor Details

#initialize(name, logger, cors_enabled) ⇒ Options

Returns a new instance of Options.



24
25
26
27
28
# File 'lib/pact/mock_service/request_handlers/options.rb', line 24

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

Instance Attribute Details

#cors_enabledObject (readonly)

Returns the value of attribute cors_enabled.



8
9
10
# File 'lib/pact/mock_service/request_handlers/options.rb', line 8

def cors_enabled
  @cors_enabled
end

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/pact/mock_service/request_handlers/options.rb', line 8

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/pact/mock_service/request_handlers/options.rb', line 8

def name
  @name
end

Instance Method Details

#is_administration_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/pact/mock_service/request_handlers/options.rb', line 53

def is_administration_request? env
  (env[HTTP_ACCESS_CONTROL_REQUEST_HEADERS] || '').match(X_PACT_MOCK_SERVICE_REGEXP)
end

#is_options_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/pact/mock_service/request_handlers/options.rb', line 49

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

#is_request_with_credentials?(env) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/pact/mock_service/request_handlers/options.rb', line 57

def is_request_with_credentials? env
  headers = (env[HTTP_ACCESS_CONTROL_REQUEST_HEADERS] || '').split(",").map { |header| header.strip.downcase }
  headers.include?(AUTHORIZATION) || headers.include?(COOKIE)
end

#match?(env) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/pact/mock_service/request_handlers/options.rb', line 30

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

#respond(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pact/mock_service/request_handlers/options.rb', line 34

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
  }

  if is_request_with_credentials?(env)
    cors_headers[ACCESS_CONTROL_ALLOW_CREDENTIALS] = "true"
  end

  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