Skip to content

Commit

Permalink
fix: do not error when no environment is found by name
Browse files Browse the repository at this point in the history
PACT-1402
  • Loading branch information
bethesque committed Oct 13, 2023
1 parent a4b3ec5 commit d150161
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/environments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def uuid

def environments
@environments ||= if request.query["name"]
[environment_service.find_by_name(request.query["name"])]
[environment_service.find_by_name(request.query["name"])].compact
else
environment_service.find_all
end
Expand Down
12 changes: 9 additions & 3 deletions spec/features/get_environments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@
end
let(:path) { PactBroker::Api::PactBrokerUrls.environments_url }
let(:headers) { {"HTTP_ACCEPT" => "application/hal+json"} }
let(:response_body) { JSON.parse(last_response.body, symbolize_names: true)}
let(:response_body) { JSON.parse(subject.body, symbolize_names: true)}

subject { get(path, nil, headers) }

it { is_expected.to be_a_hal_json_success_response }

it "returns the environments" do
subject
expect(response_body[:_embedded][:environments].size).to be 2
end

context "by name" do
let(:path) { PactBroker::Api::PactBrokerUrls.environments_url + "?name=test" }

it "returns the environment with the matching name" do
subject
expect(response_body[:_embedded][:environments].size).to be 1
expect(response_body[:_embedded][:environments].first[:name]).to eq "test"
end

context "when no environment exists" do
let(:path) { PactBroker::Api::PactBrokerUrls.environments_url + "?name=foo" }

it "returns an empty list" do
expect(response_body[:_embedded][:environments].size).to be 0
end
end
end
end

0 comments on commit d150161

Please sign in to comment.