-
-
Save sporsh/1779116 to your computer and use it in GitHub Desktop.
Vital Ruby Advanced Lab 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
class UrlFetcher | |
def fetch(url) | |
begin | |
open(url) { |stream| stream.read } | |
rescue SocketError, OpenURI::HTTPError, URI::InvalidURIError, Errno::ENOENT | |
nil | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec/given' | |
require './url_fetcher' | |
describe UrlFetcher do | |
Given(:fetcher) { UrlFetcher.new } | |
context "with not an string" do | |
Then { lambda { fetcher.fetch(1337)}.should raise_error } | |
end | |
context "with good url" do | |
Then { fetcher.fetch("http://onestepback.org").should =~ /<html/i } | |
end | |
context "with bad url path" do | |
Then { fetcher.fetch("http://onestepback.org/this_path_does_not_exist").should be_nil } | |
end | |
context "with bad url host" do | |
Then { fetcher.fetch("http://this_host_does_not_exist.com/").should be_nil } | |
end | |
context "with an ill-formed url" do | |
Then { fetcher.fetch("http://no_dot_com").should be_nil } | |
end | |
context "with an ill-formed protocol" do | |
Then { fetcher.fetch("bad_protocol://onestepback.org").should be_nil } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment