Permalink
Browse files

Prevent post_spec.rb from hanging the entire spec run for 30 seconds …

…while it tries to retrieve external resources.
  • Loading branch information...
1 parent 3442aeb commit f21691efb5de2b6cafa64b07b8e0fb2cb399244d @parndt parndt committed Nov 28, 2012
Showing with 21 additions and 15 deletions.
  1. +15 −11 config/initializers/url_validator.rb
  2. +6 −4 spec/models/refinery/blog/post_spec.rb
@@ -4,7 +4,7 @@ class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
url = value
-
+
# Regex code by 'Arsenic' from http://snippets.dzone.com/posts/show/3654
if url =~ /^
( (https?):\/\/ )?
@@ -21,21 +21,25 @@ def validate_each(record, attribute, value)
record.errors[attribute] << 'Not a valid URL'
end
- if options[:verify]
- begin
- url_response = RedirectFollower.new(url).resolve
- url = url_response.url if options[:verify] == [:resolve_redirects]
- rescue RedirectFollower::TooManyRedirects
- record.errors[attribute] << 'URL is redirecting too many times'
- rescue
- record.errors[attribute] << 'could not be resolved'
- end
- end
+ url = resolve_redirects_verify_url(url) if options[:verify]
if options[:update]
value.replace url
end
end
+
+ def resolve_redirects_verify_url(url)
+ begin
+ url_response = RedirectFollower.new(url).resolve
+ url = url_response.url if options[:verify] == [:resolve_redirects]
+ rescue RedirectFollower::TooManyRedirects
+ record.errors[attribute] << 'URL is redirecting too many times'
+ rescue
+ record.errors[attribute] << 'could not be resolved'
+ ensure
+ url
+ end
+ end
end
# Code below written by John Nunemaker
@@ -197,7 +197,7 @@ module Blog
end
end
end
-
+
describe "source url" do
it "should allow a source url and title" do
p = FactoryGirl.create(:blog_post, :source_url => 'google.com', :source_url_title => 'author')
@@ -206,13 +206,15 @@ module Blog
p.source_url_title.should include('author')
end
end
-
+
describe ".validate_source_url?" do
context "with Refinery::Blog.validate_source_url set to true" do
before do
Refinery::Blog.validate_source_url = true
- end
+ end
it "should have canonical url" do
+ UrlValidator.any_instance.should_receive(:resolve_redirects_verify_url).
+ and_return('http://www.google.com')
p = FactoryGirl.create(:blog_post, :source_url => 'google.com', :source_url_title => 'google')
p.source_url.should include('www')
end
@@ -227,7 +229,7 @@ module Blog
end
end
end
-
+
end
end
end

0 comments on commit f21691e

Please sign in to comment.