spec/sunscraper_spec.rb in sunscraper-1.0.0 vs spec/sunscraper_spec.rb in sunscraper-1.1.0.beta1
- old
+ new
@@ -19,27 +19,73 @@
</html>
HTML
PORT = 45555
-describe Sunscraper do
- it "can scrape an HTML provided as a string" do
- Sunscraper.scrape_html(HTML).should include('It works!')
+def with_webserver
+ server = WEBrick::HTTPServer.new :Port => PORT, :Logger => WEBrick::Log.new('/dev/null'), :AccessLog => []
+ server.mount_proc '/' do |req, res|
+ res.body = HTML
end
+ Thread.new { server.start }
- it "can scrape an URL" do
- server = WEBrick::HTTPServer.new :Port => PORT, :Logger => WEBrick::Log.new('/dev/null'), :AccessLog => []
- server.mount_proc '/' do |req, res|
- res.body = HTML
+ yield PORT
+ensure
+ server.shutdown if server
+end
+
+class String
+ def to_v
+ split(".").map(&:to_i).extend Comparable
+ end
+end
+
+unless Sunscraper.os_x?
+ # This part currently crashes on OS X (and will forever).
+ describe "Sunscraper::Library" do
+ before do
+ Sunscraper.worker = :embed
end
- Thread.new { server.start }
- Sunscraper.scrape_url("http://localhost:#{PORT}/").should include('It works!')
+ it "can scrape an HTML provided as a string" do
+ Sunscraper.scrape_html(HTML).should include('It works!')
+ end
- server.stop
+ it "can scrape an URL" do
+ with_webserver do |port|
+ Sunscraper.scrape_url("http://127.0.0.1:#{port}/").should include('It works!')
+ end
+ end
+
+ it "should time out if callback is not called" do
+ lambda { Sunscraper.scrape_html("<!-- nothing. at least no callbacks -->", 1000) }.
+ should raise_exception(Sunscraper::ScrapeTimeout)
+ end
end
+end
- it "should time out if callback is not called" do
- lambda { Sunscraper.scrape_html("<!-- nothing. at least no callbacks -->", 1000) }.
- should raise_exception(Sunscraper::ScrapeTimeout)
+if !(RUBY_ENGINE =~ /rbx/ || RUBY_ENGINE =~ /jruby/) ||
+ ENV['EXPERIMENTAL'] == 'true'
+ # This part currently crashes Rubinius (as of Mar 09, 2012),
+ # and crashes jruby < 1.7.0, and uses Unix sockets which don't
+ # work even on jruby master (as of Mar 09, 2012).
+ describe "Sunscraper::Standalone" do
+ before do
+ Sunscraper.worker = :standalone
+ end
+
+ it "can scrape an HTML provided as a string" do
+ Sunscraper.scrape_html(HTML).should include('It works!')
+ end
+
+ it "can scrape an URL" do
+ with_webserver do |port|
+ Sunscraper.scrape_url("http://127.0.0.1:#{port}/").should include('It works!')
+ end
+ end
+
+ it "should time out if callback is not called" do
+ lambda { Sunscraper.scrape_html("<!-- nothing. at least no callbacks -->", 1000) }.
+ should raise_exception(Sunscraper::ScrapeTimeout)
+ end
end
end
\ No newline at end of file