Commit 42a2543f98d6cb1a859c126337630df0c3b2f40e
1 parent
4e67af18
Exists in
master
and in
1 other branch
ApplicationHelper.get_host does not break on invalid url
Showing
2 changed files
with
33 additions
and
2 deletions
Show diff stats
app/helpers/application_helper.rb
| ... | ... | @@ -23,8 +23,12 @@ module ApplicationHelper |
| 23 | 23 | end |
| 24 | 24 | |
| 25 | 25 | def get_host(url) |
| 26 | - uri = url && URI.parse(url) | |
| 27 | - uri.blank? ? "N/A" : uri.host | |
| 26 | + begin | |
| 27 | + uri = url && URI.parse(url) | |
| 28 | + uri.blank? ? "N/A" : uri.host | |
| 29 | + rescue URI::InvalidURIError | |
| 30 | + "N/A" | |
| 31 | + end | |
| 28 | 32 | end |
| 29 | 33 | |
| 30 | 34 | ... | ... |
| ... | ... | @@ -0,0 +1,27 @@ |
| 1 | +require 'spec_helper' | |
| 2 | + | |
| 3 | +# Specs in this file have access to a helper object that includes | |
| 4 | +# the AbcHelper. For example: | |
| 5 | +# | |
| 6 | +# describe AbcHelper do | |
| 7 | +# describe "string concat" do | |
| 8 | +# it "concats two strings with spaces" do | |
| 9 | +# helper.concat_strings("this","that").should == "this that" | |
| 10 | +# end | |
| 11 | +# end | |
| 12 | +# end | |
| 13 | +describe ApplicationHelper do | |
| 14 | + describe "get_host" do | |
| 15 | + it "returns host if url is valid" do | |
| 16 | + helper.get_host("http://example.com/resource/12").should == 'example.com' | |
| 17 | + end | |
| 18 | + | |
| 19 | + it "returns 'N/A' when url is not valid" do | |
| 20 | + helper.get_host("some string").should == 'N/A' | |
| 21 | + end | |
| 22 | + | |
| 23 | + it "returns 'N/A' when url is empty" do | |
| 24 | + helper.get_host({}).should == 'N/A' | |
| 25 | + end | |
| 26 | + end | |
| 27 | +end | ... | ... |