aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilip Arndt <parndt@gmail.com>2012-11-29 08:54:50 +1300
committerPhilip Arndt <parndt@gmail.com>2012-11-29 08:54:50 +1300
commitf21691efb5de2b6cafa64b07b8e0fb2cb399244d (patch)
tree7c6c606da0c421c36b0bb7fabdcd4d81d8872791
parent3442aeb85087df97d36673ad846a434412e5baea (diff)
downloadrefinerycms-blog-f21691efb5de2b6cafa64b07b8e0fb2cb399244d.tar.gz
refinerycms-blog-f21691efb5de2b6cafa64b07b8e0fb2cb399244d.tar.bz2
refinerycms-blog-f21691efb5de2b6cafa64b07b8e0fb2cb399244d.zip
Prevent post_spec.rb from hanging the entire spec run for 30 seconds while it tries to retrieve external resources.
-rw-r--r--config/initializers/url_validator.rb26
-rw-r--r--spec/models/refinery/blog/post_spec.rb10
2 files changed, 21 insertions, 15 deletions
diff --git a/config/initializers/url_validator.rb b/config/initializers/url_validator.rb
index d956d04..e9dcb89 100644
--- a/config/initializers/url_validator.rb
+++ b/config/initializers/url_validator.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 @@ $/ix
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
diff --git a/spec/models/refinery/blog/post_spec.rb b/spec/models/refinery/blog/post_spec.rb
index 7b0c9be..84e9c87 100644
--- a/spec/models/refinery/blog/post_spec.rb
+++ b/spec/models/refinery/blog/post_spec.rb
@@ -197,7 +197,7 @@ module Refinery
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 Refinery
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 Refinery
end
end
end
-
+
end
end
end