Created
July 22, 2013 20:09
-
-
Save arr-ee/6057189 to your computer and use it in GitHub Desktop.
This file contains 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 'minitest/autorun' | |
require 'uri' | |
class UriValidator | |
ALLOWED_PROTOS = [URI::HTTP, URI::HTTPS, URI::FTP] | |
def initialize uri | |
@uri = uri | |
end | |
def validate! | |
parsed_uri = URI.parse @uri | |
ALLOWED_PROTOS.include? parsed_uri.class | |
rescue URI::InvalidURIError | |
false | |
end | |
end | |
class TestUriValidator < Minitest::Test | |
URIS = { | |
'http://test.com' => true, | |
'https://test.com' => true, | |
'ftp://good.uri.com/test' => true, | |
'shit happens' => false, | |
'alsow12414@@!!!wrong' => false, | |
'' => false, | |
nil => false | |
} | |
URIS.each_with_index do |(uri, result), idx| | |
define_method "test_uri_#{idx}" do | |
assert_equal result, UriValidator.new(uri).validate! | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment