From e9d9bcd1962c61c592fa6473688e10ce7a4fa652 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Sat, 3 Jan 2015 10:38:22 -0500 Subject: [PATCH] Add test suite for back-end app --- Gemfile | 1 + Gemfile.lock | 1 + lib/app.rb | 8 ++- test/app_test.rb | 175 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 test/app_test.rb diff --git a/Gemfile b/Gemfile index 50ad6a0d..feaf9539 100644 --- a/Gemfile +++ b/Gemfile @@ -38,6 +38,7 @@ end group :test do gem 'minitest' gem 'rr', require: false + gem 'rack-test', require: false end if ENV['SELENIUM'] == '1' diff --git a/Gemfile.lock b/Gemfile.lock index f5a80460..e576093e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -104,6 +104,7 @@ DEPENDENCIES progress_bar pry (~> 0.9.12) rack + rack-test rr sass sinatra diff --git a/lib/app.rb b/lib/app.rb index e7e8e047..05e10389 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -40,12 +40,14 @@ class App < Sinatra::Application end end - configure :development do - register Sinatra::Reloader - + configure :test, :development do require 'active_support/per_thread_registry' require 'active_support/cache' sprockets.cache = ActiveSupport::Cache.lookup_store :file_store, root.join('tmp', 'cache', 'assets') + end + + configure :development do + register Sinatra::Reloader use BetterErrors::Middleware BetterErrors.application_root = File.expand_path('..', __FILE__) diff --git a/test/app_test.rb b/test/app_test.rb new file mode 100644 index 00000000..8b614b14 --- /dev/null +++ b/test/app_test.rb @@ -0,0 +1,175 @@ +require 'test_helper' +require 'rack/test' +require 'app' + +class AppTest < MiniTest::Spec + include Rack::Test::Methods + + def app + App + end + + describe "/" do + it "works" do + get '/' + assert last_response.ok? + end + + it "redirects without the query string" do + get '/', foo: 'bar' + assert last_response.redirect? + assert_equal 'http://example.org/', last_response['Location'] + end + end + + describe "/[static-page]" do + it "redirects to /#/[static-page]" do + %w(offline about news help).each do |page| + get "/#{page}" + assert last_response.redirect? + assert_equal "http://example.org/#/#{page}", last_response['Location'] + end + end + end + + describe "/search" do + it "redirects to /#q=" do + get '/search' + assert last_response.redirect? + assert_equal 'http://example.org/#q=', last_response['Location'] + + get '/search', q: 'foo' + assert last_response.redirect? + assert_equal 'http://example.org/#q=foo', last_response['Location'] + end + end + + describe "/manifest.appcache" do + it "works" do + get '/manifest.appcache' + assert last_response.ok? + end + + it "works with cookie" do + set_cookie('docs=css/html') + get '/manifest.appcache' + assert last_response.ok? + assert_includes last_response.body, '/css/index.json' + assert_includes last_response.body, '/html/index.json' + end + end + + describe "/[doc]" do + it "works when the doc exists" do + get '/html/' + assert last_response.ok? + end + + it "returns 404 when the doc doesn't exist" do + get '/foo/' + assert last_response.not_found? + end + + it "redirects with trailing slash" do + get '/html' + assert last_response.redirect? + assert_equal 'http://example.org/html/', last_response['Location'] + + get '/html', bar: 'baz' + assert last_response.redirect? + assert_equal 'http://example.org/html/?bar=baz', last_response['Location'] + end + end + + describe "/[doc]-[type]" do + it "works when the doc exists" do + get '/html-foo-bar_42/' + assert last_response.ok? + end + + it "returns 404 when the type is blank" do + get '/html-/' + assert last_response.not_found? + end + + it "returns 404 when the type is not alpha-numeric" do + get '/html-foo:bar/' + assert last_response.not_found? + end + + it "returns 404 when the doc doesn't exist" do + get '/foo-bar/' + assert last_response.not_found? + end + + it "redirects with trailing slash" do + get '/html-foo' + assert last_response.redirect? + assert_equal 'http://example.org/html-foo/', last_response['Location'] + + get '/html-foo', bar: 'baz' + assert last_response.redirect? + assert_equal 'http://example.org/html-foo/?bar=baz', last_response['Location'] + end + end + + describe "/[doc+type]/[path]" do + it "works when the doc exists" do + get '/html/foo' + assert last_response.ok? + + get '/html-bar/foo' + assert last_response.ok? + end + + it "returns 404 when the doc doesn't exist" do + get '/foo/bar' + assert last_response.not_found? + end + + it "redirects without trailing slash" do + get '/html/foo/' + assert last_response.redirect? + assert_equal 'http://example.org/html/foo', last_response['Location'] + + get '/html/foo/', bar: 'baz' + assert last_response.redirect? + assert_equal 'http://example.org/html/foo?bar=baz', last_response['Location'] + end + end + + describe "/feed" do + it "returns an atom feed" do + get '/feed' + assert last_response.ok? + assert_equal 'application/atom+xml', last_response['Content-Type'] + + get '/feed.atom' + assert last_response.ok? + assert_equal 'application/atom+xml', last_response['Content-Type'] + end + end + + describe "/s/[link]" do + it "redirects" do + %w(maxcdn shopify tw fb re).each do |link| + get "/s/#{link}" + assert last_response.redirect? + end + end + end + + describe "/ping" do + it "works" do + get '/ping' + assert last_response.ok? + end + end + + describe "404" do + it "works" do + get '/foo' + assert last_response.not_found? + end + end +end