Class: Pact::MockService::RequestHandlers::Options
- Inherits:
-
BaseRequestHandler
- Object
- BaseRequestHandler
- Pact::MockService::RequestHandlers::Options
- 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 =
"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
-
#cors_enabled ⇒ Object
readonly
Returns the value of attribute cors_enabled.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, logger, cors_enabled) ⇒ Options
constructor
A new instance of Options.
- #is_administration_request?(env) ⇒ Boolean
- #is_options_request?(env) ⇒ Boolean
- #is_request_with_credentials?(env) ⇒ Boolean
- #match?(env) ⇒ Boolean
- #respond(env) ⇒ Object
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_enabled ⇒ Object (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 |
#logger ⇒ Object (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 |
#name ⇒ Object (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
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
49 50 51 |
# File 'lib/pact/mock_service/request_handlers/options.rb', line 49 def env env[REQUEST_METHOD] == OPTIONS end |
#is_request_with_credentials?(env) ⇒ 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
30 31 32 |
# File 'lib/pact/mock_service/request_handlers/options.rb', line 30 def match? env (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 |