Skip to content

Commit

Permalink
feat: respond correctly to OPTIONS requests
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 21, 2018
1 parent d6260c1 commit c5d3937
Show file tree
Hide file tree
Showing 37 changed files with 60 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/all_webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def content_types_accepted
end

def allowed_methods
["GET", "POST"]
["GET", "POST", "OPTIONS", "OPTIONS"]
end

def create_path
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Resources
class Badge < BaseResource

def allowed_methods
['GET']
["GET", "OPTIONS", "OPTIONS"]
end

def content_types_provided
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/api/resources/base_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def initialize
PactBroker.configuration.before_resource.call(self)
end

def options
{ 'Access-Control-Allow-Methods' => allowed_methods.join(", ")}
end

def update_matrix_after_request?
false
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def to_json
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def content_types_accepted
end

def allowed_methods
["GET", "POST"]
["GET", "POST", "OPTIONS"]
end

def to_json
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def resource_exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def to_json
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def content_types_accepted
end

def allowed_methods
["GET","PUT","DELETE"]
["GET", "PUT", "DELETE", "OPTIONS"]
end

def from_json
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/latest_pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def resource_exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/latest_pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def to_json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Resources
class LatestVerificationsForConsumerVersion < BaseResource

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def content_types_provided
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def malformed_request?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def resource_exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def content_types_accepted
end

def allowed_methods
["GET", "PUT", "DELETE", "PATCH"]
["GET", "PUT", "DELETE", "PATCH", "OPTIONS"]
end

def known_methods
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pact_content_diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def resource_exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pact_triggered_webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Api
module Resources
class PactTriggeredWebhooks < BaseResource
def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def content_types_provided
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pact_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Api
module Resources
class PactVersion < Pact
def allowed_methods
["GET"]
["GET", "OPTIONS"]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pact_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def resource_exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pact_webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Resources
class PactWebhooks < BaseResource

def allowed_methods
["POST", "GET"]
["POST", "GET", "OPTIONS"]
end

def content_types_provided
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pact_webhooks_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Resources
class PactWebhooksStatus < BaseResource

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def content_types_provided
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pacticipant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def content_types_accepted
end

def allowed_methods
["GET", "PATCH", "DELETE"]
["GET", "PATCH", "DELETE", "OPTIONS"]
end

def known_methods
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pacticipants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def content_types_accepted
end

def allowed_methods
["GET", "POST"]
["GET", "POST", "OPTIONS"]
end

def malformed_request?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pacticipants_for_label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def to_json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def resource_exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/provider_pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def resource_exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def to_csv
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def content_types_accepted
end

def allowed_methods
["GET","PUT","DELETE"]
["GET","PUT","DELETE", "OPTIONS"]
end

def from_json
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/triggered_webhook_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def resource_exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def resource_exists?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Api
module Resources
class VerificationTriggeredWebhooks < BaseResource
def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def content_types_provided
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/verifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def content_types_provided
end

def allowed_methods
["POST"]
["POST", "OPTIONS"]
end

def post_is_create?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def content_types_provided
end

def allowed_methods
["GET", "DELETE"]
["GET", "DELETE", "OPTIONS"]
end

def resource_exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def content_types_provided
end

def allowed_methods
["GET"]
["GET", "OPTIONS"]
end

def resource_exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def content_types_provided
end

def allowed_methods
["GET", "PUT", "DELETE"]
["GET", "PUT", "DELETE", "OPTIONS"]
end

def resource_exists?
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/webhook_execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Resources
class WebhookExecution < BaseResource

def allowed_methods
["POST"]
["POST", "OPTIONS"]
end

def process_post
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Resources
class Webhooks < BaseResource

def allowed_methods
["POST", "GET"]
["POST", "GET", "OPTIONS"]
end

def content_types_provided
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/pact_broker/api/resources/base_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ module Resources
subject { BaseResource.new(request, response) }

its(:resource_url) { is_expected.to eq 'http://example.org/path' }

describe "options" do
subject { options "/"; last_response }

it "returns a list of allowed methods" do
expect(subject.headers['Access-Control-Allow-Methods']).to eq "GET, OPTIONS"
end
end
end

ALL_RESOURCES = ObjectSpace.each_object(::Class).select {|klass| klass < BaseResource }

ALL_RESOURCES.each do | resource |
describe resource do
let(:request) { double('request', uri: URI("http://example.org")) }
let(:response) { double('response') }

it "includes OPTIONS in the list of allowed_methods" do
expect(resource.new(request, response).allowed_methods).to include "OPTIONS"
end
end
end
end
end
Expand Down

0 comments on commit c5d3937

Please sign in to comment.