Permalink
Browse files

Refactored tests

- Adjusted new-style request URLs
- mocks HTTP requests using vcr
- split test helper from _test.rb
- renames Http module to SimpleHttp to prevent name clash with vcr/webmock
  • Loading branch information...
1 parent d260a71 commit 84649ef7b3fff00a77807534be4af6ac11f93d4a @radiospiel committed Apr 19, 2012
Showing with 658 additions and 27 deletions.
  1. +6 −0 Gemfile
  2. +27 −3 Gemfile.lock
  3. +4 −4 lib/http.rb
  4. +2 −1 lib/robot/url_getter.rb
  5. +571 −0 tests/fixtures-vcr/imgio.yml
  6. +29 −19 tests/resizing_test.rb
  7. +19 −0 tests/test_helper.rb
View
@@ -8,4 +8,10 @@ gem "sinatra-contrib", :group => :development
group :test do
gem "rack-test", :require => 'rack/test'
gem "image_size"
+ gem "ruby-debug19"
+
+ # vcr stuff: this needs psych or else will crash
+ gem "vcr"
+ gem "webmock"
+ gem "psych"
end
View
@@ -1,8 +1,11 @@
GEM
remote: http://rubygems.org/
specs:
- addressable (2.2.6)
- backports (2.3.0)
+ addressable (2.2.7)
+ archive-tar-minitar (0.5.2)
+ backports (2.5.1)
+ columnize (0.3.6)
+ crack (0.3.1)
daemons (1.1.8)
em-http-request (1.0.0)
addressable (>= 2.2.3)
@@ -16,14 +19,27 @@ GEM
eventmachine (>= 1.0.0.beta.1)
eventmachine (1.0.0.beta.3)
http_parser.rb (0.5.3)
- image_size (1.0.6)
+ image_size (1.1.0)
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
+ psych (1.3.2)
rack (1.4.1)
rack-fiber_pool (0.9.2)
rack-protection (1.2.0)
rack
rack-test (0.6.1)
rack (>= 1.0)
rmagick (2.13.1)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
@@ -47,14 +63,22 @@ GEM
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.3.3)
+ vcr (2.0.1)
+ webmock (1.8.6)
+ addressable (>= 2.2.7)
+ crack (>= 0.1.7)
PLATFORMS
ruby
DEPENDENCIES
image_size
+ psych
rack-test
rmagick
+ ruby-debug19
sinatra-contrib
sinatra-synchrony
thin
+ vcr
+ webmock
View
@@ -5,10 +5,10 @@
require "uri"
end
-# The Http module exports Http.get, which returns the body of an URL, and is able to follow
-# a certain number of redirections.
-#
-module Http
+# The SimpleHttp module exports SimpleHttp.get, which returns the body of an URL, and is able to follow
+# a certain number of redirections. Note: This module is named "SimpleHttp" to prevent a name clash with
+# VCR and/or WebMock.
+module SimpleHttp
MAX_REDIRECTIONS=10
module Async
View
@@ -11,7 +11,8 @@ class Robot::UrlGetter < Robot
# always the first robot in an AssemblyLine, as it fetches the remaining
# path completely.
def run
- blob = Http.get @url
+ blob = ::SimpleHttp.get @url
+
image = Magick::Image.from_blob(blob).first
[ {}, image ]
end

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,10 +1,4 @@
-ENV['RACK_ENV'] = 'test'
-Bundler.setup(:default)
-require "#{File.dirname(__FILE__)}/../app.rb"
-require 'test/unit'
-Bundler.require(:test)
-
-Sinatra::Synchrony.patch_tests! if EVENT_MACHINED
+require_relative "test_helper"
class ImgioTest < Test::Unit::TestCase
include Rack::Test::Methods
@@ -17,38 +11,54 @@ def image
ImageSize.new(last_response.body)
end
+ def setup
+ VCR.insert_cassette('imgio')
+ end
+
+ def teardown
+ VCR.eject_cassette
+ end
+
def test_be_nice_to_your_users
get '/'
assert last_response.ok?
- assert_match /Welcome to imgio/, last_response.body
+ assert_match %r{Welcome to imgio}, last_response.body
end
+ def test_raise_exception_on_invalid_request
+ assert_raise(RuntimeError) {
+ get '/fill/100/50/http://www.rubycgi.org/image/ruby_gtk_book_title.jpg'
+ assert_equal 100, image.width
+ assert_equal 50, image.height
+ }
+ end
+
def test_fill_a_rectangle_with_the_image
- get '/fill/100/50/http://www.rubycgi.org/image/ruby_gtk_book_title.jpg'
+ get '/fill/100x50/http://www.rubycgi.org/image/ruby_gtk_book_title.jpg'
assert_equal 100, image.width
assert_equal 50, image.height
end
def test_fit_the_image_into_a_rectangle
- get '/fill/100/50/http://www.rubycgi.org/image/ruby_gtk_book_title.jpg'
+ get '/fill/100x50/http://www.rubycgi.org/image/ruby_gtk_book_title.jpg'
assert_equal 100, image.width
assert_equal 50, image.height
end
- def test_respond_with_jpg_by_default
- get '/fill/100/50/http://www.rubycgi.org/image/ruby_gtk_book_title.jpg'
- assert_equal :jpeg, image.format
- assert_equal 'image/jpeg', last_response.headers['Content-Type']
- end
-
- def test_respond_with_png_if_requested
- get '/fill/png/100/50/http://www.rubycgi.org/image/ruby_gtk_book_title.jpg'
+ def test_respond_with_png_by_default
+ get '/fill/100x50/http://www.rubycgi.org/image/ruby_gtk_book_title.jpg'
assert_equal :png, image.format
assert_equal 'image/png', last_response.headers['Content-Type']
end
+
+ def test_respond_with_jpeg_if_requested
+ get '/jpg/fill/100x50/http://www.rubycgi.org/image/ruby_gtk_book_title.jpg'
+ assert_equal :jpeg, image.format
+ assert_equal 'image/jpg', last_response.headers['Content-Type']
+ end
def test_set_caching_headers
- get '/fill/png/100/50/http://www.rubycgi.org/image/ruby_gtk_book_title.jpg'
+ get '/png/fill/100x50/http://www.rubycgi.org/image/ruby_gtk_book_title.jpg'
assert_equal "public, max-age=86400", last_response.headers['Cache-Control']
assert_not_nil last_response.headers['Expires']
end
View
@@ -0,0 +1,19 @@
+#
+# prepare everything to run all tests.
+
+ENV['RACK_ENV'] = 'test'
+Bundler.setup(:default)
+require "#{File.dirname(__FILE__)}/../app.rb"
+require 'test/unit'
+Bundler.require(:test)
+
+require "ruby-debug"
+
+Sinatra::Synchrony.patch_tests! if EVENT_MACHINED
+
+require 'vcr'
+
+VCR.configure do |c|
+ c.cassette_library_dir = "#{File.dirname(__FILE__)}/fixtures-vcr"
+ c.hook_into :webmock
+end

0 comments on commit 84649ef

Please sign in to comment.