Skip to content

Commit

Permalink
fix: return a 400 when invalid JSON is used to create a version
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 10, 2022
1 parent 2ccbb90 commit 9af2cfa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
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 @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/pact_broker/api/resources/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions spec/lib/pact_broker/api/resources/version_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9af2cfa

Please sign in to comment.