From 9af2cfaf1296640807819b0815060166b02e1830 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 10 May 2022 19:04:10 +1000 Subject: [PATCH] fix: return a 400 when invalid JSON is used to create a version --- .../api/resources/base_resource.rb | 4 ++++ lib/pact_broker/api/resources/version.rb | 8 +++++++ .../pact_broker/api/resources/version_spec.rb | 23 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 spec/lib/pact_broker/api/resources/version_spec.rb diff --git a/lib/pact_broker/api/resources/base_resource.rb b/lib/pact_broker/api/resources/base_resource.rb index 2bed97e41..332932c86 100644 --- a/lib/pact_broker/api/resources/base_resource.rb +++ b/lib/pact_broker/api/resources/base_resource.rb @@ -152,6 +152,10 @@ def request_body @request_body ||= request.body.to_s end + def any_request_body? + request_body && request_body.size > 0 + end + def consumer_name identifier_from_path[:consumer_name] end diff --git a/lib/pact_broker/api/resources/version.rb b/lib/pact_broker/api/resources/version.rb index cf23c8069..99e540bbf 100644 --- a/lib/pact_broker/api/resources/version.rb +++ b/lib/pact_broker/api/resources/version.rb @@ -25,6 +25,14 @@ def resource_exists? !!version end + def malformed_request? + if request.put? && any_request_body? + invalid_json? + else + false + end + end + def from_json if request.really_put? handle_request do diff --git a/spec/lib/pact_broker/api/resources/version_spec.rb b/spec/lib/pact_broker/api/resources/version_spec.rb new file mode 100644 index 000000000..930b11b0b --- /dev/null +++ b/spec/lib/pact_broker/api/resources/version_spec.rb @@ -0,0 +1,23 @@ +require "pact_broker/api/resources/version" + +module PactBroker + module Api + module Resources + describe Version do + let(:path) { "/pacticipants/Foo/versions/1" } + + context "with an empty body" do + subject { put(path, nil, "CONTENT_TYPE" => "application/json") } + + its(:status) { is_expected.to eq 201 } + end + + context "with invalid JSON" do + subject { put(path, { some: "yaml" }.to_yaml, "CONTENT_TYPE" => "application/json") } + + its(:status) { is_expected.to eq 400 } + end + end + end + end +end