-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a basic test suite using Rack::Test and vanilla Test::Unit.
- Loading branch information
Mike Leone
committed
Aug 13, 2010
1 parent
9b025a2
commit e06f5d4
Showing
26 changed files
with
2,273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require File.join(File.dirname(__FILE__), 'environment') | ||
require 'test/unit' | ||
require 'rack/test' | ||
|
||
ENV['RACK_ENV'] = 'test' | ||
|
||
class HelloWorldTest < Test::Unit::TestCase | ||
include Rack::Test::Methods | ||
|
||
def app | ||
Broadcast | ||
end | ||
|
||
def test_it_shows_index_page | ||
get '/' | ||
assert last_response.ok? | ||
end | ||
|
||
def test_get_snapshot | ||
get '/snapshot.jpg' | ||
assert last_response.ok? | ||
assert last_response.body.length > 10000 | ||
end | ||
|
||
def test_get_update | ||
get '/update.jpg' | ||
assert last_response.ok? | ||
assert last_response.body.length > 10000 | ||
end | ||
|
||
def test_get_json | ||
get 'location.json' | ||
assert last_response.ok? | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
README.rdoc | ||
lib/**/*.rb | ||
History.txt | ||
MIT-LICENSE.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pkg | ||
doc | ||
coverage | ||
VERSION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
== 0.5.3 / 2009-11-27 | ||
|
||
* Bug fixes | ||
|
||
* Fix cookie matching for subdomains (Marcin Kulik) | ||
|
||
== 0.5.2 / 2009-11-13 | ||
|
||
* Bug fixes | ||
|
||
* Call close on response body after iteration, not before (Simon Rozet) | ||
* Add missing require for time in cookie_jar.rb (Jerry West) | ||
|
||
== 0.5.1 / 2009-10-27 | ||
|
||
* Bug fixes | ||
|
||
* Escape cookie values (John Pignata) | ||
* Close the response body after each request, as per the Rack spec (Elomar França) | ||
|
||
== 0.5.0 / 2009-09-19 | ||
|
||
* Bug fixes | ||
|
||
* Set HTTP_X_REQUESTED_WITH in the Rack env when a request is made with :xhr => true (Ben Sales) | ||
* Set headers in the Rack env in HTTP_USER_AGENT form | ||
* Rack::Test now generates no Ruby warnings | ||
|
||
== 0.4.2 / 2009-09-01 | ||
|
||
* Minor enhancements | ||
|
||
* Merge in rack/master's build_multipart method which covers additional cases | ||
* Accept raw :params string input and merge it with the query string | ||
* Stringify and upcase request method (e.g. :post => "POST") (Josh Peek) | ||
|
||
* Bug fixes | ||
|
||
* Properly convert hashes with nil values (e.g. :foo => nil becomes simply "foo", not "foo=") | ||
* Prepend a slash to the URI path if it doesn't start with one (Josh Peek) | ||
* Requiring Rack-Test never modifies the Ruby load path anymore (Josh Peek) | ||
* Fixed using multiple cookies in a string on Ruby 1.8 (Tuomas Kareinen and Hermanni Hyytiälä) | ||
|
||
== 0.4.1 / 2009-08-06 | ||
|
||
* Minor enhancements | ||
|
||
* Support initializing a Rack::Test::Session with an app in addition to | ||
a Rack::MockSession | ||
* Allow CONTENT_TYPE to be specified in the env and not overwritten when | ||
sending a POST or PUT | ||
|
||
== 0.4.0 / 2009-06-25 | ||
|
||
* Minor enhancements | ||
|
||
* Expose hook for building Rack::MockSessions for frameworks that need | ||
to configure them before use | ||
* Support passing in arrays of raw cookies in addition to a newline | ||
separated string | ||
* Support after_request callbacks in MockSession for things like running | ||
background jobs | ||
* Allow multiple named sessions using with_session | ||
* Initialize Rack::Test::Sessions with Rack::MockSessions instead of apps. | ||
This change should help integration with other Ruby web frameworks | ||
(like Merb). | ||
* Support sending bodies for PUT requests (Larry Diehl) | ||
|
||
== 0.3.0 / 2009-05-17 | ||
|
||
* Major enhancements | ||
|
||
* Ruby 1.9 compatible (Simon Rozet, Michael Fellinger) | ||
|
||
* Minor enhancements | ||
|
||
* Add CookieJar#[] and CookieJar#[]= methods | ||
* Make the default host configurable | ||
* Use Rack::Lint and fix errors (Simon Rozet) | ||
* Extract Rack::MockSession from Rack::Test::Session to handle tracking | ||
the last request and response and the cookie jar | ||
* Add #set_cookie and #clear_cookies methods | ||
* Rename #authorize to #basic_authorize (#authorize remains as an alias) | ||
(Simon Rozet) | ||
|
||
== 0.2.0 / 2009-04-26 | ||
|
||
Because #last_response is now a MockResponse instead of a Rack::Response, | ||
#last_response.body now returns a string instead of an array. | ||
|
||
* Major enhancements | ||
|
||
* Support multipart requests via the UploadedFile class (thanks, Rails) | ||
|
||
* Minor enhancements | ||
|
||
* Updated for Rack 1.0 | ||
* Don't require rubygems (See http://gist.github.com/54177) | ||
* Support HTTP Digest authentication with the #digest_authorize method | ||
* #last_response returns a MockResponse instead of a Response | ||
(Michael Fellinger) | ||
|
||
== 0.1.0 / 2009-03-02 | ||
|
||
* 1 major enhancement | ||
|
||
* Birthday! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2008-2009 Bryan Helmkamp, Engine Yard Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
= Rack::Test | ||
|
||
- Code: http://github.com/brynary/rack-test | ||
- Build: http://runcoderun.com/brynary/rack-test | ||
|
||
== Description | ||
|
||
Rack::Test is a small, simple testing API for Rack apps. It can be used on its | ||
own or as a reusable starting point for Web frameworks and testing libraries | ||
to build on. Most of its initial functionality is an extraction of Merb 1.0's | ||
request helpers feature. | ||
|
||
== Features | ||
|
||
* Maintains a cookie jar across requests | ||
* Easily follow redirects when desired | ||
* Set request headers to be used by all subsequent requests | ||
* Small footprint. Approximately 200 LOC | ||
|
||
== Example | ||
|
||
require "rack/test" | ||
|
||
class HomepageTest < Test::Unit::TestCase | ||
include Rack::Test::Methods | ||
|
||
def app | ||
MyApp.new | ||
end | ||
|
||
def test_redirect_logged_in_users_to_dashboard | ||
authorize "bryan", "secret" | ||
get "/" | ||
follow_redirect! | ||
|
||
assert_equal "http://example.org/redirected", last_request.url | ||
assert last_response.ok? | ||
end | ||
|
||
end | ||
|
||
== Install | ||
|
||
To install the latest release as a gem: | ||
|
||
sudo gem install rack-test | ||
|
||
== Authors | ||
|
||
- Maintained by {Bryan Helmkamp}[mailto:[email protected]] | ||
- Contributions from Simon Rozet and Pat Nakajima | ||
- Much of the original code was extracted from Merb 1.0's request helper | ||
|
||
== License | ||
|
||
Copyright (c) 2008-2009 Bryan Helmkamp, Engine Yard Inc. | ||
See MIT-LICENSE.txt in this directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "rubygems" | ||
|
||
begin | ||
require "spec/rake/spectask" | ||
rescue LoadError | ||
desc "Run specs" | ||
task(:spec) { $stderr.puts '`gem install rspec` to run specs' } | ||
else | ||
Spec::Rake::SpecTask.new do |t| | ||
t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""] | ||
t.libs << 'lib' | ||
t.libs << 'spec' | ||
t.warning = true | ||
end | ||
|
||
task :default => :spec | ||
|
||
desc "Run all specs in spec directory with RCov" | ||
Spec::Rake::SpecTask.new(:rcov) do |t| | ||
t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""] | ||
t.libs << 'lib' | ||
t.libs << 'spec' | ||
t.warning = true | ||
t.rcov = true | ||
t.rcov_opts = ['-x spec'] | ||
end | ||
end | ||
|
||
desc "Generate RDoc" | ||
task :docs do | ||
FileUtils.rm_rf("doc") | ||
require "rack/test" | ||
system "hanna --title 'Rack::Test #{Rack::Test::VERSION} API Documentation'" | ||
end | ||
|
||
desc 'Removes trailing whitespace' | ||
task :whitespace do | ||
sh %{find . -name '*.rb' -exec sed -i '' 's/ *$//g' {} \\;} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
module GemHelpers | ||
|
||
def generate_gemspec | ||
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "lib"))) | ||
require "rack/test" | ||
|
||
Gem::Specification.new do |s| | ||
s.name = "rack-test" | ||
s.version = Rack::Test::VERSION | ||
s.author = "Bryan Helmkamp" | ||
s.email = "[email protected]" | ||
s.homepage = "http://github.com/brynary/rack-test" | ||
s.summary = "Simple testing API built on Rack" | ||
s.description = <<-EOS.strip | ||
Rack::Test is a small, simple testing API for Rack apps. It can be used on its | ||
own or as a reusable starting point for Web frameworks and testing libraries | ||
to build on. Most of its initial functionality is an extraction of Merb 1.0's | ||
request helpers feature. | ||
EOS | ||
s.rubyforge_project = "rack-test" | ||
|
||
require "git" | ||
repo = Git.open(".") | ||
|
||
s.files = normalize_files(repo.ls_files.keys - repo.lib.ignored_files) | ||
s.test_files = normalize_files(Dir['spec/**/*.rb'] - repo.lib.ignored_files) | ||
|
||
s.has_rdoc = true | ||
s.extra_rdoc_files = %w[README.rdoc MIT-LICENSE.txt] | ||
|
||
s.add_dependency "rack", ">= 1.0" | ||
end | ||
end | ||
|
||
def normalize_files(array) | ||
# only keep files, no directories, and sort | ||
array.select do |path| | ||
File.file?(path) | ||
end.sort | ||
end | ||
|
||
# Adds extra space when outputting an array. This helps create better version | ||
# control diffs, because otherwise it is all on the same line. | ||
def prettyify_array(gemspec_ruby, array_name) | ||
gemspec_ruby.gsub(/s\.#{array_name.to_s} = \[.+?\]/) do |match| | ||
leadin, files = match[0..-2].split("[") | ||
leadin + "[\n #{files.split(",").join(",\n ")}\n ]" | ||
end | ||
end | ||
|
||
def read_gemspec | ||
@read_gemspec ||= eval(File.read("rack-test.gemspec")) | ||
end | ||
|
||
def sh(command) | ||
puts command | ||
system command | ||
end | ||
end | ||
|
||
class Default < Thor | ||
include GemHelpers | ||
|
||
desc "gemspec", "Regenerate rack-test.gemspec" | ||
def gemspec | ||
File.open("rack-test.gemspec", "w") do |file| | ||
gemspec_ruby = generate_gemspec.to_ruby | ||
gemspec_ruby = prettyify_array(gemspec_ruby, :files) | ||
gemspec_ruby = prettyify_array(gemspec_ruby, :test_files) | ||
gemspec_ruby = prettyify_array(gemspec_ruby, :extra_rdoc_files) | ||
|
||
file.write gemspec_ruby | ||
end | ||
|
||
puts "Wrote gemspec to rack-test.gemspec" | ||
read_gemspec.validate | ||
end | ||
|
||
desc "build", "Build a rack-test gem" | ||
def build | ||
sh "gem build rack-test.gemspec" | ||
FileUtils.mkdir_p "pkg" | ||
FileUtils.mv read_gemspec.file_name, "pkg" | ||
end | ||
|
||
desc "install", "Install the latest built gem" | ||
def install | ||
sh "gem install --local pkg/#{read_gemspec.file_name}" | ||
end | ||
|
||
desc "release", "Release the current branch to GitHub and Gemcutter" | ||
def release | ||
gemspec | ||
build | ||
Release.new.tag | ||
Release.new.gem | ||
end | ||
end | ||
|
||
class Release < Thor | ||
include GemHelpers | ||
|
||
desc "tag", "Tag the gem on the origin server" | ||
def tag | ||
release_tag = "v#{read_gemspec.version}" | ||
sh "git tag -a #{release_tag} -m 'Tagging #{release_tag}'" | ||
sh "git push origin #{release_tag}" | ||
end | ||
|
||
desc "gem", "Push the gem to Gemcutter" | ||
def gem | ||
sh "gem push pkg/#{read_gemspec.file_name}" | ||
end | ||
end |
Oops, something went wrong.