Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Use webmock instead of WEBrick for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdunae committed Sep 9, 2011
1 parent 8ed8bfc commit ef01120
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 55 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
source :rubygems
gem 'css_parser', :git => 'git://github.com/alexdunae/css_parser.git'
gem 'webmock', :group => [:development, :test]
gemspec
2 changes: 1 addition & 1 deletion test/future_tests.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: UTF-8
require File.expand_path(File.dirname(__FILE__)) + '/helper'

class TestPremailer < Test::Unit::TestCase
class TestPremailer < Premailer::TestCase
def test_related_attributes
flunk 'Not implemented'
local_setup
Expand Down
35 changes: 34 additions & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,38 @@
$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../'))
require 'rubygems'
require 'test/unit'
require 'webrick'
require 'webmock/test_unit'
require 'premailer'

class Premailer::TestCase < Test::Unit::TestCase
BASE_URI = 'http://premailer.dev/'
BASE_PATH = File.expand_path(File.dirname(__FILE__)) + '/files'

def setup
stub_request(:any, /premailer\.dev\/*/).to_return do |request|
file_path = BASE_PATH + URI.parse(request.uri).path
if File.exists?(file_path)
{ :status => 200, :body => File.open(file_path) }
else
{ :status => 404, :body => "#{file_path} not found" }
end
end
end

def default_test; end

protected
def local_setup(f = 'base.html', opts = {})
base_file = BASE_PATH + '/' + f
premailer = Premailer.new(base_file, opts)
premailer.to_inline_css
@doc = premailer.processed_doc
end

def remote_setup(f = 'base.html', opts = {})
@premailer = Premailer.new(BASE_URI + "#{f}", opts)
@premailer.to_inline_css
@doc = @premailer.processed_doc
end

end
2 changes: 1 addition & 1 deletion test/test_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__)) + '/helper'

class TestAdapter < Test::Unit::TestCase
class TestAdapter < Premailer::TestCase

def test_default_to_best_available
require 'hpricot'
Expand Down
2 changes: 1 addition & 1 deletion test/test_html_to_plain_text.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: utf-8
require File.expand_path(File.dirname(__FILE__)) + '/helper'

class TestHtmlToPlainText < Test::Unit::TestCase
class TestHtmlToPlainText < Premailer::TestCase
include HtmlToPlainText

def test_to_plain_text_with_fragment
Expand Down
3 changes: 2 additions & 1 deletion test/test_links.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: UTF-8
require File.expand_path(File.dirname(__FILE__)) + '/helper'

class TestLinks < Test::Unit::TestCase
class TestLinks < Premailer::TestCase
def test_empty_query_string
assert_nothing_raised do
premailer = Premailer.new('<p>Test</p>', :with_html_string => true, :link_query_string => ' ')
Expand Down
2 changes: 1 addition & 1 deletion test/test_misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Random tests for specific issues.
#
# The test suite will be cleaned up at some point soon.
class TestMisc < Test::Unit::TestCase
class TestMisc < Premailer::TestCase

# in response to http://github.com/alexdunae/premailer/issues#issue/4
#
Expand Down
47 changes: 1 addition & 46 deletions test/test_premailer.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
# encoding: UTF-8
require File.expand_path(File.dirname(__FILE__)) + '/helper'

class TestPremailer < Test::Unit::TestCase
include WEBrick

def setup
# from http://nullref.se/blog/2006/5/17/testing-with-webrick
@uri_base = "http://localhost:12000"
www_root = File.expand_path(File.dirname(__FILE__)) + '/files/'

unless @server_thread
@server_thread = Thread.new do
s = WEBrick::HTTPServer.new(:Port => 12000, :DocumentRoot => www_root, :Logger => Log.new(nil, BasicLog::ERROR), :AccessLog => [])
port = s.config[:Port]
begin
s.start
ensure
s.shutdown
end
end
end
end

class TestPremailer < Premailer::TestCase
def test_special_characters_nokogiri
html = '<p>cédille c&eacute; & garçon gar&#231;on à &agrave; &nbsp; &amp; &copy;</p>'
premailer = Premailer.new(html, :with_html_string => true, :adapter => :nokogiri)
Expand Down Expand Up @@ -51,7 +31,6 @@ def test_special_characters_hpricot
premailer.to_inline_css
assert_equal 'c&eacute;dille c&eacute; &amp; gar&ccedil;on gar&ccedil;on &agrave; &agrave; &nbsp; &amp;', premailer.processed_doc.at('p').inner_html
end


def test_detecting_html
[:nokogiri, :hpricot].each do |adapter|
Expand Down Expand Up @@ -233,28 +212,4 @@ def test_premailer_related_attributes
assert_equal '20', doc.at('td')['height']
end
end


protected
def local_setup(f = 'base.html', opts = {})
base_file = File.expand_path(File.dirname(__FILE__)) + '/files/' + f
premailer = Premailer.new(base_file, opts)
premailer.to_inline_css
@doc = premailer.processed_doc
end

def remote_setup(f = 'base.html', opts = {})
# increment the port number for testing multiple adapters
@premailer = Premailer.new(@uri_base + "/#{f}", opts)
@premailer.to_inline_css
@doc = @premailer.processed_doc
end

#def teardown
# if @server_thread
# @server_thread.kill
# @server_thread.join(5)
# @server_thread = nil
# end
#end
end
4 changes: 1 addition & 3 deletions test/test_warnings.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# encoding: UTF-8
require File.expand_path(File.dirname(__FILE__)) + '/helper'

class TestWarnings < Test::Unit::TestCase
include WEBrick

class TestWarnings < Premailer::TestCase
def test_element_warnings
html = <<END_HTML
<!DOCTYPE html>
Expand Down

0 comments on commit ef01120

Please sign in to comment.