-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Making CORS work #2
Changes from 11 commits
e206c9f
0fd5a32
cff3885
8f73e79
cc7dfe6
02bbaf8
f4f5b2f
f2e79db
f677f4e
f9ee116
727faf8
3c6fdd4
6c64b2d
f3746ea
986f57b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'pact/consumer/mock_service/web_request_options' | ||
|
||
module Pact | ||
module Consumer | ||
|
||
# Allow web preflight requests to the intaractions setup by the user | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. intaractions => interactions |
||
# This is only needed in a CORS setup, where the browsers do | ||
# an OPTIONS call before a DELETE, POST (for most request), etc. in a cross domain requests | ||
class CandidateOptions < WebRequestOptions | ||
def initialize name, logger, cors_enabled | ||
super(name,logger) | ||
@cors_enabled = cors_enabled | ||
end | ||
|
||
# Will match all requests to OPTIONS when in CORS mode | ||
def request_path_match? env | ||
@cors_enabled | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if there's no check for request method == 'OPTIONS', what stops every method matching this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following is defined in the parent With its own parent having the matcher defined def match? env |
||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'pact/consumer/mock_service/web_request_options' | ||
|
||
module Pact | ||
module Consumer | ||
|
||
# Allow web preflight requests to Pact infrastructure | ||
# Browsers typically do a OPTIONS before a POST for cross domain requests | ||
class InteractionOptions < WebRequestOptions | ||
def request_path | ||
'/interactions' | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'pact/consumer/mock_service/web_request_options' | ||
|
||
module Pact | ||
module Consumer | ||
|
||
# Allow web preflight requests to Pact infrastructure | ||
# Browsers typically do a OPTIONS before a POST for cross domain requests | ||
class PactOptions < WebRequestOptions | ||
def request_path | ||
'/pact' | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require 'cgi/core' | ||
module Pact | ||
module Consumer | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'pact/consumer/mock_service/rack_request_helper' | ||
require 'pact/consumer/mock_service/mock_service_administration_endpoint' | ||
|
||
module Pact | ||
module Consumer | ||
|
||
# Administration web requests (GET, DELETE, POST, PUT, etc.) | ||
class WebRequestAdministration < MockServiceAdministrationEndpoint | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is meant by "WebRequest"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a hard time with names. I've subclassed the mock requests into:
Why web request? Because the code is handling what is sent by the browser. Happy to take any better name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking "BrowserRequest" might be more accurate then. |
||
|
||
def request_path | ||
raise NotImplementedError | ||
end | ||
|
||
def request_method | ||
raise NotImplementedError | ||
end | ||
|
||
def enable_cors? | ||
true | ||
end | ||
|
||
private | ||
def request_header_match? env | ||
headers_from(env)['X-Pact-Mock-Service'] | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'pact/consumer/mock_service/rack_request_helper' | ||
require 'pact/consumer/mock_service/mock_service_administration_endpoint' | ||
|
||
module Pact | ||
module Consumer | ||
|
||
# Web Request is OPTIONS, which is a preflight brower request made | ||
# before sending the actual POST, DELETE, etc. in CORS cases | ||
class WebRequestOptions < MockServiceAdministrationEndpoint | ||
|
||
include RackRequestHelper | ||
|
||
def request_path | ||
raise NotImplementedError | ||
end | ||
|
||
def request_method | ||
'OPTIONS' | ||
end | ||
|
||
def respond env | ||
logger.info "Preflight browser CORS check before sending data okayed (OPTIONS request)" | ||
[200, | ||
{ | ||
'Access-Control-Allow-Origin' => '*', | ||
# '*' is not allowed for 'Access-Control-Allow-Headers. We need to echo back what was provided! | ||
'Access-Control-Allow-Headers' => headers_from(env)["Access-Control-Request-Headers"], | ||
'Access-Control-Allow-Methods' => 'DELETE, POST, GET, HEAD, PUT, TRACE, CONNECT'}, | ||
["Browser, go ahead and send the actual request"] | ||
] | ||
end | ||
|
||
# Access-Control-Domain does not work on OPTIONs requests. | ||
def enable_cors? | ||
false | ||
end | ||
|
||
private | ||
# 'X-Pact-Mock-Service' header is set as a normal header in regular requests (PUT, GET, POST, etc.) | ||
# However, browsers set it within Access-Control-Request-Headers in case of OPTIONS request | ||
# (web browsers make an OPTIONS request prior to the normal request in case of CORS request) | ||
# For OPTIONS request, headers are different | ||
def request_header_match? env | ||
headers_from(env)["Access-Control-Request-Headers"].nil? ? false | ||
: headers_from(env)["Access-Control-Request-Headers"].match(/x-pact-mock-service/) | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed?