From c5d3937f29d673a2c002a70bfc551524d3a4b2ba Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 21 Jun 2018 17:20:08 +1000 Subject: [PATCH] feat: respond correctly to OPTIONS requests --- lib/pact_broker/api/resources/all_webhooks.rb | 2 +- lib/pact_broker/api/resources/badge.rb | 2 +- .../api/resources/base_resource.rb | 4 ++++ lib/pact_broker/api/resources/dashboard.rb | 2 +- lib/pact_broker/api/resources/error_test.rb | 2 +- lib/pact_broker/api/resources/group.rb | 2 +- lib/pact_broker/api/resources/index.rb | 2 +- lib/pact_broker/api/resources/label.rb | 2 +- lib/pact_broker/api/resources/latest_pact.rb | 2 +- lib/pact_broker/api/resources/latest_pacts.rb | 2 +- ...test_verifications_for_consumer_version.rb | 2 +- lib/pact_broker/api/resources/matrix.rb | 2 +- .../matrix_for_consumer_and_provider.rb | 2 +- lib/pact_broker/api/resources/pact.rb | 2 +- .../api/resources/pact_content_diff.rb | 2 +- .../api/resources/pact_triggered_webhooks.rb | 2 +- lib/pact_broker/api/resources/pact_version.rb | 2 +- .../api/resources/pact_versions.rb | 2 +- .../api/resources/pact_webhooks.rb | 2 +- .../api/resources/pact_webhooks_status.rb | 2 +- lib/pact_broker/api/resources/pacticipant.rb | 2 +- lib/pact_broker/api/resources/pacticipants.rb | 2 +- .../api/resources/pacticipants_for_label.rb | 2 +- .../previous_distinct_pact_version.rb | 2 +- .../api/resources/provider_pacts.rb | 2 +- .../api/resources/relationships.rb | 2 +- lib/pact_broker/api/resources/tag.rb | 2 +- .../api/resources/triggered_webhook_logs.rb | 2 +- lib/pact_broker/api/resources/verification.rb | 2 +- .../verification_triggered_webhooks.rb | 2 +- .../api/resources/verifications.rb | 2 +- lib/pact_broker/api/resources/version.rb | 2 +- lib/pact_broker/api/resources/versions.rb | 2 +- lib/pact_broker/api/resources/webhook.rb | 2 +- .../api/resources/webhook_execution.rb | 2 +- lib/pact_broker/api/resources/webhooks.rb | 2 +- .../api/resources/base_resource_spec.rb | 21 +++++++++++++++++++ 37 files changed, 60 insertions(+), 35 deletions(-) diff --git a/lib/pact_broker/api/resources/all_webhooks.rb b/lib/pact_broker/api/resources/all_webhooks.rb index 46584d517..f2b665393 100644 --- a/lib/pact_broker/api/resources/all_webhooks.rb +++ b/lib/pact_broker/api/resources/all_webhooks.rb @@ -18,7 +18,7 @@ def content_types_accepted end def allowed_methods - ["GET", "POST"] + ["GET", "POST", "OPTIONS", "OPTIONS"] end def create_path diff --git a/lib/pact_broker/api/resources/badge.rb b/lib/pact_broker/api/resources/badge.rb index 43ecb7cc2..d3ddb9043 100644 --- a/lib/pact_broker/api/resources/badge.rb +++ b/lib/pact_broker/api/resources/badge.rb @@ -9,7 +9,7 @@ module Resources class Badge < BaseResource def allowed_methods - ['GET'] + ["GET", "OPTIONS", "OPTIONS"] end def content_types_provided diff --git a/lib/pact_broker/api/resources/base_resource.rb b/lib/pact_broker/api/resources/base_resource.rb index 67498807d..14afc7040 100644 --- a/lib/pact_broker/api/resources/base_resource.rb +++ b/lib/pact_broker/api/resources/base_resource.rb @@ -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 diff --git a/lib/pact_broker/api/resources/dashboard.rb b/lib/pact_broker/api/resources/dashboard.rb index 3150ccb6c..e60974ae1 100644 --- a/lib/pact_broker/api/resources/dashboard.rb +++ b/lib/pact_broker/api/resources/dashboard.rb @@ -16,7 +16,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def to_json diff --git a/lib/pact_broker/api/resources/error_test.rb b/lib/pact_broker/api/resources/error_test.rb index e2a0e1903..ec779dda6 100644 --- a/lib/pact_broker/api/resources/error_test.rb +++ b/lib/pact_broker/api/resources/error_test.rb @@ -20,7 +20,7 @@ def content_types_accepted end def allowed_methods - ["GET", "POST"] + ["GET", "POST", "OPTIONS"] end def to_json diff --git a/lib/pact_broker/api/resources/group.rb b/lib/pact_broker/api/resources/group.rb index 67736b9b6..547cb7d1d 100644 --- a/lib/pact_broker/api/resources/group.rb +++ b/lib/pact_broker/api/resources/group.rb @@ -13,7 +13,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/index.rb b/lib/pact_broker/api/resources/index.rb index 07af5707e..de0b19439 100644 --- a/lib/pact_broker/api/resources/index.rb +++ b/lib/pact_broker/api/resources/index.rb @@ -12,7 +12,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def to_json diff --git a/lib/pact_broker/api/resources/label.rb b/lib/pact_broker/api/resources/label.rb index fcdac5f67..67d39abb6 100644 --- a/lib/pact_broker/api/resources/label.rb +++ b/lib/pact_broker/api/resources/label.rb @@ -15,7 +15,7 @@ def content_types_accepted end def allowed_methods - ["GET","PUT","DELETE"] + ["GET", "PUT", "DELETE", "OPTIONS"] end def from_json diff --git a/lib/pact_broker/api/resources/latest_pact.rb b/lib/pact_broker/api/resources/latest_pact.rb index 734732811..d1a3aa5e6 100644 --- a/lib/pact_broker/api/resources/latest_pact.rb +++ b/lib/pact_broker/api/resources/latest_pact.rb @@ -14,7 +14,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/latest_pacts.rb b/lib/pact_broker/api/resources/latest_pacts.rb index 6aeab5791..648f35b3f 100644 --- a/lib/pact_broker/api/resources/latest_pacts.rb +++ b/lib/pact_broker/api/resources/latest_pacts.rb @@ -11,7 +11,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def to_json diff --git a/lib/pact_broker/api/resources/latest_verifications_for_consumer_version.rb b/lib/pact_broker/api/resources/latest_verifications_for_consumer_version.rb index 7fde1f3ce..a116e5b97 100644 --- a/lib/pact_broker/api/resources/latest_verifications_for_consumer_version.rb +++ b/lib/pact_broker/api/resources/latest_verifications_for_consumer_version.rb @@ -11,7 +11,7 @@ module Resources class LatestVerificationsForConsumerVersion < BaseResource def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def content_types_provided diff --git a/lib/pact_broker/api/resources/matrix.rb b/lib/pact_broker/api/resources/matrix.rb index 2b3ec54aa..6dd6b233c 100644 --- a/lib/pact_broker/api/resources/matrix.rb +++ b/lib/pact_broker/api/resources/matrix.rb @@ -20,7 +20,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def malformed_request? diff --git a/lib/pact_broker/api/resources/matrix_for_consumer_and_provider.rb b/lib/pact_broker/api/resources/matrix_for_consumer_and_provider.rb index 838eccad0..5e157d5c2 100644 --- a/lib/pact_broker/api/resources/matrix_for_consumer_and_provider.rb +++ b/lib/pact_broker/api/resources/matrix_for_consumer_and_provider.rb @@ -11,7 +11,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/pact.rb b/lib/pact_broker/api/resources/pact.rb index bb7f27ee4..944c420fd 100644 --- a/lib/pact_broker/api/resources/pact.rb +++ b/lib/pact_broker/api/resources/pact.rb @@ -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 diff --git a/lib/pact_broker/api/resources/pact_content_diff.rb b/lib/pact_broker/api/resources/pact_content_diff.rb index ac3d90f0d..e71b179bd 100644 --- a/lib/pact_broker/api/resources/pact_content_diff.rb +++ b/lib/pact_broker/api/resources/pact_content_diff.rb @@ -11,7 +11,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/pact_triggered_webhooks.rb b/lib/pact_broker/api/resources/pact_triggered_webhooks.rb index dd7b1e760..9636917c1 100644 --- a/lib/pact_broker/api/resources/pact_triggered_webhooks.rb +++ b/lib/pact_broker/api/resources/pact_triggered_webhooks.rb @@ -5,7 +5,7 @@ module Api module Resources class PactTriggeredWebhooks < BaseResource def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def content_types_provided diff --git a/lib/pact_broker/api/resources/pact_version.rb b/lib/pact_broker/api/resources/pact_version.rb index 216ae873e..35d47e082 100644 --- a/lib/pact_broker/api/resources/pact_version.rb +++ b/lib/pact_broker/api/resources/pact_version.rb @@ -5,7 +5,7 @@ module Api module Resources class PactVersion < Pact def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end end end diff --git a/lib/pact_broker/api/resources/pact_versions.rb b/lib/pact_broker/api/resources/pact_versions.rb index a41865478..7db58f160 100644 --- a/lib/pact_broker/api/resources/pact_versions.rb +++ b/lib/pact_broker/api/resources/pact_versions.rb @@ -13,7 +13,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/pact_webhooks.rb b/lib/pact_broker/api/resources/pact_webhooks.rb index d8f9009af..7ae823715 100644 --- a/lib/pact_broker/api/resources/pact_webhooks.rb +++ b/lib/pact_broker/api/resources/pact_webhooks.rb @@ -11,7 +11,7 @@ module Resources class PactWebhooks < BaseResource def allowed_methods - ["POST", "GET"] + ["POST", "GET", "OPTIONS"] end def content_types_provided diff --git a/lib/pact_broker/api/resources/pact_webhooks_status.rb b/lib/pact_broker/api/resources/pact_webhooks_status.rb index 490858153..ba87772a9 100644 --- a/lib/pact_broker/api/resources/pact_webhooks_status.rb +++ b/lib/pact_broker/api/resources/pact_webhooks_status.rb @@ -9,7 +9,7 @@ module Resources class PactWebhooksStatus < BaseResource def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def content_types_provided diff --git a/lib/pact_broker/api/resources/pacticipant.rb b/lib/pact_broker/api/resources/pacticipant.rb index 36293ffe2..5baa3b5bd 100644 --- a/lib/pact_broker/api/resources/pacticipant.rb +++ b/lib/pact_broker/api/resources/pacticipant.rb @@ -23,7 +23,7 @@ def content_types_accepted end def allowed_methods - ["GET", "PATCH", "DELETE"] + ["GET", "PATCH", "DELETE", "OPTIONS"] end def known_methods diff --git a/lib/pact_broker/api/resources/pacticipants.rb b/lib/pact_broker/api/resources/pacticipants.rb index f89017693..9d8ef1439 100644 --- a/lib/pact_broker/api/resources/pacticipants.rb +++ b/lib/pact_broker/api/resources/pacticipants.rb @@ -17,7 +17,7 @@ def content_types_accepted end def allowed_methods - ["GET", "POST"] + ["GET", "POST", "OPTIONS"] end def malformed_request? diff --git a/lib/pact_broker/api/resources/pacticipants_for_label.rb b/lib/pact_broker/api/resources/pacticipants_for_label.rb index d5a997722..595934705 100644 --- a/lib/pact_broker/api/resources/pacticipants_for_label.rb +++ b/lib/pact_broker/api/resources/pacticipants_for_label.rb @@ -11,7 +11,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def to_json diff --git a/lib/pact_broker/api/resources/previous_distinct_pact_version.rb b/lib/pact_broker/api/resources/previous_distinct_pact_version.rb index 4b650c6d1..b5c6ad360 100644 --- a/lib/pact_broker/api/resources/previous_distinct_pact_version.rb +++ b/lib/pact_broker/api/resources/previous_distinct_pact_version.rb @@ -14,7 +14,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/provider_pacts.rb b/lib/pact_broker/api/resources/provider_pacts.rb index d418e3f98..39735d71d 100644 --- a/lib/pact_broker/api/resources/provider_pacts.rb +++ b/lib/pact_broker/api/resources/provider_pacts.rb @@ -12,7 +12,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/relationships.rb b/lib/pact_broker/api/resources/relationships.rb index de77898a6..4198d969b 100644 --- a/lib/pact_broker/api/resources/relationships.rb +++ b/lib/pact_broker/api/resources/relationships.rb @@ -12,7 +12,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def to_csv diff --git a/lib/pact_broker/api/resources/tag.rb b/lib/pact_broker/api/resources/tag.rb index ebdfb6269..74cd5a919 100644 --- a/lib/pact_broker/api/resources/tag.rb +++ b/lib/pact_broker/api/resources/tag.rb @@ -14,7 +14,7 @@ def content_types_accepted end def allowed_methods - ["GET","PUT","DELETE"] + ["GET","PUT","DELETE", "OPTIONS"] end def from_json diff --git a/lib/pact_broker/api/resources/triggered_webhook_logs.rb b/lib/pact_broker/api/resources/triggered_webhook_logs.rb index 3fbf74154..804a19f8f 100644 --- a/lib/pact_broker/api/resources/triggered_webhook_logs.rb +++ b/lib/pact_broker/api/resources/triggered_webhook_logs.rb @@ -12,7 +12,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/verification.rb b/lib/pact_broker/api/resources/verification.rb index 2feecdf7a..784bef9a4 100644 --- a/lib/pact_broker/api/resources/verification.rb +++ b/lib/pact_broker/api/resources/verification.rb @@ -15,7 +15,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/verification_triggered_webhooks.rb b/lib/pact_broker/api/resources/verification_triggered_webhooks.rb index c0efeb0c8..01126940d 100644 --- a/lib/pact_broker/api/resources/verification_triggered_webhooks.rb +++ b/lib/pact_broker/api/resources/verification_triggered_webhooks.rb @@ -5,7 +5,7 @@ module Api module Resources class VerificationTriggeredWebhooks < BaseResource def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def content_types_provided diff --git a/lib/pact_broker/api/resources/verifications.rb b/lib/pact_broker/api/resources/verifications.rb index e9a413861..803174c06 100644 --- a/lib/pact_broker/api/resources/verifications.rb +++ b/lib/pact_broker/api/resources/verifications.rb @@ -19,7 +19,7 @@ def content_types_provided end def allowed_methods - ["POST"] + ["POST", "OPTIONS"] end def post_is_create? diff --git a/lib/pact_broker/api/resources/version.rb b/lib/pact_broker/api/resources/version.rb index ba907fa4f..c2b984278 100644 --- a/lib/pact_broker/api/resources/version.rb +++ b/lib/pact_broker/api/resources/version.rb @@ -13,7 +13,7 @@ def content_types_provided end def allowed_methods - ["GET", "DELETE"] + ["GET", "DELETE", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/versions.rb b/lib/pact_broker/api/resources/versions.rb index 4318d265b..2babe616a 100644 --- a/lib/pact_broker/api/resources/versions.rb +++ b/lib/pact_broker/api/resources/versions.rb @@ -13,7 +13,7 @@ def content_types_provided end def allowed_methods - ["GET"] + ["GET", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/webhook.rb b/lib/pact_broker/api/resources/webhook.rb index 24b4f44f9..1f0e27cf7 100644 --- a/lib/pact_broker/api/resources/webhook.rb +++ b/lib/pact_broker/api/resources/webhook.rb @@ -17,7 +17,7 @@ def content_types_provided end def allowed_methods - ["GET", "PUT", "DELETE"] + ["GET", "PUT", "DELETE", "OPTIONS"] end def resource_exists? diff --git a/lib/pact_broker/api/resources/webhook_execution.rb b/lib/pact_broker/api/resources/webhook_execution.rb index 0366e950e..8c8b4ff34 100644 --- a/lib/pact_broker/api/resources/webhook_execution.rb +++ b/lib/pact_broker/api/resources/webhook_execution.rb @@ -10,7 +10,7 @@ module Resources class WebhookExecution < BaseResource def allowed_methods - ["POST"] + ["POST", "OPTIONS"] end def process_post diff --git a/lib/pact_broker/api/resources/webhooks.rb b/lib/pact_broker/api/resources/webhooks.rb index 502d16963..dfbc330a1 100644 --- a/lib/pact_broker/api/resources/webhooks.rb +++ b/lib/pact_broker/api/resources/webhooks.rb @@ -9,7 +9,7 @@ module Resources class Webhooks < BaseResource def allowed_methods - ["POST", "GET"] + ["POST", "GET", "OPTIONS"] end def content_types_provided diff --git a/spec/lib/pact_broker/api/resources/base_resource_spec.rb b/spec/lib/pact_broker/api/resources/base_resource_spec.rb index 3f1d1065c..812abe4a0 100644 --- a/spec/lib/pact_broker/api/resources/base_resource_spec.rb +++ b/spec/lib/pact_broker/api/resources/base_resource_spec.rb @@ -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