Skip to content

Commit 56d9ae5

Browse files
committed
Return 400 error for pacts with invalid JSON
1 parent ba88b8d commit 56d9ae5

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/pact_broker/api/resources/pact.rb

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ def allowed_methods
2020
["GET", "PUT"]
2121
end
2222

23+
def malformed_request?
24+
begin
25+
JSON.load(pact_content)
26+
false
27+
rescue
28+
response.headers['Content-Type'] = 'application/json'
29+
response.body = {error: 'Invalid JSON'}.to_json
30+
true
31+
end
32+
end
33+
2334
def resource_exists?
2435
@pact = pact_service.find_pact(identifier_from_path)
2536
@pact != nil

spec/lib/pact_broker/api/resources/pact_spec.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ module Resources
1717
context "with invalid JSON" do
1818

1919
before do
20-
put ""
20+
put "/pacts/provider/Provider/consumer/Consumer/version/1.2", '{', {'CONTENT_TYPE' => "application/json"}
2121
end
2222

23-
it "returns a 400 response"
23+
it "returns a 400 response" do
24+
expect(last_response.status).to eq 400
25+
end
2426

25-
it "returns a JSON body"
27+
it "returns a JSON content type" do
28+
expect(last_response.headers['Content-Type']).to eq "application/json"
29+
end
2630

27-
it "returns an error message"
31+
it "returns an error message" do
32+
expect(JSON.parse(last_response.body)).to eq "error" => "Invalid JSON"
33+
end
2834
end
2935
end
3036

0 commit comments

Comments
 (0)