diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 39d6a6d..9282420 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -23,8 +23,12 @@ module ApplicationHelper end def get_host(url) - uri = url && URI.parse(url) - uri.blank? ? "N/A" : uri.host + begin + uri = url && URI.parse(url) + uri.blank? ? "N/A" : uri.host + rescue URI::InvalidURIError + "N/A" + end end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb new file mode 100644 index 0000000..46d88ae --- /dev/null +++ b/spec/helpers/application_helper_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the AbcHelper. For example: +# +# describe AbcHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe ApplicationHelper do + describe "get_host" do + it "returns host if url is valid" do + helper.get_host("http://example.com/resource/12").should == 'example.com' + end + + it "returns 'N/A' when url is not valid" do + helper.get_host("some string").should == 'N/A' + end + + it "returns 'N/A' when url is empty" do + helper.get_host({}).should == 'N/A' + end + end +end -- libgit2 0.21.2