Skip to content

Commit

Permalink
Update RSpec and fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Morgan committed Nov 8, 2014
1 parent cd0c6d5 commit d50bbd9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
-cfs
--format documentation
--color
1 change: 1 addition & 0 deletions lib/url_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'httpi'
require 'active_support/core_ext/hash/except'
require 'active_model/validator'
require 'active_support/core_ext/array/wrap'

# Validates URLs. Uses the following I18n error message keys:
#
Expand Down
79 changes: 35 additions & 44 deletions spec/url_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,41 @@ class Record
it "should allow nil if :allow_nil is set" do
@validator = UrlValidator.new(:attributes => [ :field ], :allow_nil => true)
@validator.validate_each(@record, :field, nil)
@record.errors.should be_empty
expect(@record.errors).to be_empty
end

it "should allow \"\" if :allow_blank is set" do
@validator = UrlValidator.new(:attributes => [ :field ], :allow_blank => true)
@validator.validate_each(@record, :field, "")
@record.errors.should be_empty
expect(@record.errors).to be_empty
end
end

context "[format]" do
it "should only allow HTTP URLs if :scheme is set to 'http'" do
@validator = UrlValidator.new(:attributes => [ :field ], :scheme => 'http')
@validator.validate_each(@record, :field, "http://www.apple.com")
@record.errors.should be_empty
expect(@record.errors).to be_empty

@validator.validate_each(@record, :field, "https://www.apple.com")
@record.errors[:field].first.should include('invalid_url')
expect(@record.errors[:field].first).to include('invalid_url')
end

it "should only allow HTTP and HTTPS URLs if :scheme is set to %w( http https )" do
@validator = UrlValidator.new(:attributes => [ :field ], :scheme => %w( http https ))
@validator.validate_each(@record, :field, "http://www.apple.com")
@record.errors.should be_empty
expect(@record.errors).to be_empty
@validator.validate_each(@record, :field, "https://www.apple.com")
@record.errors.should be_empty
expect(@record.errors).to be_empty

@validator.validate_each(@record, :field, "ftp://www.apple.com")
@record.errors[:field].first.should include('invalid_url')
expect(@record.errors[:field].first).to include('invalid_url')
end

it "should try a default scheme if :default_scheme is set" do
@validator = UrlValidator.new(:attributes => [ :field ], :scheme => 'http', :default_scheme => 'http')
@validator.validate_each(@record, :field, "www.apple.com")
@record.errors.should be_empty
expect(@record.errors).to be_empty
end

context "[HTTP(S)]" do
Expand All @@ -66,7 +66,7 @@ class Record
].each do |uri|
@record.errors.clear
@validator.validate_each(@record, :field, "www.apple.com")
@record.errors[:field].first.should include('invalid_url')
expect(@record.errors[:field].first).to include('invalid_url')
end
end
end
Expand All @@ -77,163 +77,154 @@ class Record
it "should only validate if the host is accessible when :check_host is set" do
@validator = UrlValidator.new(:attributes => [ :field ])
@validator.validate_each(@record, :field, "http://www.invalid.tld")
@record.errors.should be_empty
expect(@record.errors).to be_empty

@validator = UrlValidator.new(:attributes => [ :field ], :check_host => true)
@validator.validate_each(@record, :field, "http://www.invalid.tld")
@record.errors[:field].first.should include('url_not_accessible')
expect(@record.errors[:field].first).to include('url_not_accessible')
end

it "should not perform the accessibility check if :check_host is set to 'http' and the URL scheme is not HTTP" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_host => 'http')
@validator.validate_each(@record, :field, "https://www.invalid.tld")
@record.errors.should be_empty
expect(@record.errors).to be_empty
end

it "should only validate if the host is accessible when :check_host is set to 'http' and the URL scheme is HTTP" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_host => 'http')
@validator.validate_each(@record, :field, "http://www.invalid.tld")
@record.errors[:field].first.should include('url_not_accessible')
expect(@record.errors[:field].first).to include('url_not_accessible')
end

it "should not perform the accessibility check if :check_host is set to %w( http https ) and the URL scheme is not HTTP(S)" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_host => %w( http https ), :scheme => %w( ftp http https ))
@validator.validate_each(@record, :field, "ftp://www.invalid.tld")
@record.errors.should be_empty
expect(@record.errors).to be_empty
end

it "should only validate if the host is accessible when :check_host is set to %w( http https ) and the URL scheme is HTTP(S)" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_host => %w( http https ))
@validator.validate_each(@record, :field, "http://www.invalid.tld")
@record.errors[:field].first.should include('url_not_accessible')
expect(@record.errors[:field].first).to include('url_not_accessible')

@validator = UrlValidator.new(:attributes => [ :field ], :check_host => %w( http https ))
@validator.validate_each(@record, :field, "https://www.invalid.tld")
@record.errors[:field].first.should include('url_not_accessible')
expect(@record.errors[:field].first).to include('url_not_accessible')
end

it "should only validate the host" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_host => true)
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors.should be_empty
expect(@record.errors).to be_empty
end
end

context "[:check_path]" do
it "should not validate if the response code is equal to the Fixnum value of this option" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => 404)
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].first.should include('url_invalid_response')
expect(@record.errors[:field].first).to include('url_invalid_response')

@record.errors.clear

@validator = UrlValidator.new(:attributes => [ :field ], :check_path => 405)
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].should be_empty
expect(@record.errors[:field]).to be_empty
end

it "should not validate if the response code is equal to the Symbol value of this option" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => :not_found)
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].first.should include('url_invalid_response')
expect(@record.errors[:field].first).to include('url_invalid_response')

@record.errors.clear

@validator = UrlValidator.new(:attributes => [ :field ], :check_path => :unauthorized)
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].should be_empty
expect(@record.errors[:field]).to be_empty
end

it "should not validate if the response code is within the Range value of this option" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => 400..499)
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].first.should include('url_invalid_response')
expect(@record.errors[:field].first).to include('url_invalid_response')

@record.errors.clear

@validator = UrlValidator.new(:attributes => [ :field ], :check_path => 500..599)
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].should be_empty
expect(@record.errors[:field]).to be_empty
end

it "should not validate if the response code is equal to the Fixnum value contained in the Array value of this option" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ 404, 405 ])
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].first.should include('url_invalid_response')
expect(@record.errors[:field].first).to include('url_invalid_response')

@record.errors.clear

@validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ 405, 406 ])
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].should be_empty
expect(@record.errors[:field]).to be_empty
end

it "should not validate if the response code is equal to the Symbol value contained in the Array value of this option" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ :not_found, :unauthorized ])
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].first.should include('url_invalid_response')
expect(@record.errors[:field].first).to include('url_invalid_response')

@record.errors.clear

@validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ :unauthorized, :moved_permanently ])
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].should be_empty
expect(@record.errors[:field]).to be_empty
end

it "should not validate if the response code is equal to the Range value contained in the Array value of this option" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ 400..499, 500..599 ])
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].first.should include('url_invalid_response')
expect(@record.errors[:field].first).to include('url_invalid_response')

@record.errors.clear

@validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ 500..599, 300..399 ])
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].should be_empty
expect(@record.errors[:field]).to be_empty
end

it "should skip validation by default" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => nil)
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].should be_empty
expect(@record.errors[:field]).to be_empty
end

it "should not validate 4xx and 5xx response codes if the value is true" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => true)
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
@record.errors[:field].first.should include('url_invalid_response')
expect(@record.errors[:field].first).to include('url_invalid_response')
end

it "should skip validation for non-HTTP URLs" do
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => true, :scheme => %w( ftp http https ))
@validator.validate_each(@record, :field, "ftp://ftp.sdgasdgohaodgh.com/sdgjsdg")
@record.errors[:field].should be_empty
expect(@record.errors[:field]).to be_empty
end
end

context "[:httpi_adapter]" do
it "should use the specified HTTPI adapter" do
@validator = UrlValidator.new(:attributes => [ :field ], :httpi_adapter => :curl, :check_host => true)
HTTPI.should_receive(:get).once.with(an_instance_of(HTTPI::Request), :curl).and_return(false)
expect(HTTPI).to receive(:get).once.with(an_instance_of(HTTPI::Request), :curl).and_return(false)
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
end
end

context "[:request_callback]" do
it "should be yielded the HTTPI request" do
called = false
@validator = UrlValidator.new(:attributes => [ :field ], :check_host => true, :request_callback => lambda { |request| called = true; request.should be_kind_of(HTTPI::Request) })
@validator = UrlValidator.new(:attributes => [ :field ], :check_host => true, :request_callback => lambda { |request| called = true; expect(request).to be_kind_of(HTTPI::Request) })
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
called.should be_true
end
end

context "[:response_callback]" do
it "should be yielded the HTTPI response" do
called = false
@validator = UrlValidator.new(:attributes => [ :field ], :check_path => true, :response_callback => lambda { |response, record, attribute, value| called = true; response.should be_kind_of(HTTPI::Response) })
@validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
called.should be_true
expect(called).to eql(true)
end
end
end
Expand Down

0 comments on commit d50bbd9

Please sign in to comment.