Skip to content

Instantly share code, notes, and snippets.

@arr-ee
Created July 22, 2013 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arr-ee/6057189 to your computer and use it in GitHub Desktop.
Save arr-ee/6057189 to your computer and use it in GitHub Desktop.
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