From 42a2543f98d6cb1a859c126337630df0c3b2f40e Mon Sep 17 00:00:00 2001 From: Marcin Ciunelis Date: Tue, 27 Sep 2011 09:19:57 +0200 Subject: [PATCH] ApplicationHelper.get_host does not break on invalid url --- app/helpers/application_helper.rb | 8 ++++++-- spec/helpers/application_helper_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 spec/helpers/application_helper_spec.rb 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