Skip to content

Commit

Permalink
Handle case where container field URL is empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
pilaf committed Nov 22, 2024
1 parent 0238f9a commit b6cc9be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/fmrest/v1/container_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ module ContainerFields
def fetch_container_data(container_field_url, base_connection = nil)
require "open-uri"

if container_field_url == ""
raise FmRest::ContainerFieldError, "Container field URL is empty string"
end

begin
url = URI(container_field_url)
rescue ::URI::InvalidURIError
rescue ::URI::InvalidURIError, ArgumentError
raise FmRest::ContainerFieldError, "Invalid container field URL `#{container_field_url}'"
end

# Make sure we don't try to open anything on the file:/ URI scheme
unless url.scheme.match(/\Ahttps?\Z/)
raise FmRest::ContainerFieldError, "Container URL is not HTTP (#{container_field_url})"
raise FmRest::ContainerFieldError, "Container field URL is not HTTP (#{container_field_url})"
end

ssl_options = base_connection && base_connection.ssl && base_connection.ssl.to_hash
Expand Down
10 changes: 9 additions & 1 deletion spec/core/v1/container_fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@
describe "#fetch_container_data" do
let(:container_url) { "https://foo.bar:4444/qux" }

context "when given an empty string" do
it { expect { extendee.fetch_container_data("") }.to raise_error(FmRest::ContainerFieldError, /Container field URL is empty string/) }
end

context "when given a nil URL" do
it { expect { extendee.fetch_container_data(nil) }.to raise_error(FmRest::ContainerFieldError, /Invalid container field URL/) }
end

context "when given an invalid URL" do
it { expect { extendee.fetch_container_data("boo boo") }.to raise_error(FmRest::ContainerFieldError, /Invalid container field URL/) }
end

context "when given a non-http URL" do
it { expect { extendee.fetch_container_data("file://foo/bar") }.to raise_error(FmRest::ContainerFieldError, /Container URL is not HTTP/) }
it { expect { extendee.fetch_container_data("file://foo/bar") }.to raise_error(FmRest::ContainerFieldError, /Container field URL is not HTTP/) }
end

context "when the given URL is not a redirect" do
Expand Down

0 comments on commit b6cc9be

Please sign in to comment.