require 'backports' if RUBY_VERSION =~ /1.8/ require_relative "helper.rb" require_relative File.join("..", "lib", "assert-response.rb") include Rack::Test::Methods def handle_exception(env) begin yield rescue Exception => err env['rack.errors'] = StringIO.new("#{err.class} - #{err.message}\n#{err.backtrace.join("\n")}") end end def tester end def app proc { |env| status = 200 headers = { 'Content-Type' => 'text/html' } body = 'nothing to see here, move along' case env['PATH_INFO'] when '/html' body = 'my html' when '/js' headers['Content-Type'] = 'application/javascript' body = 'alert("hi");' when '/css' headers['Content-Type'] = 'text/css' body = 'body{ color: black; }' when '/json' headers['Content-Type'] = 'application/json' body = '{"response":"ok"}' when '/text' headers['Content-Type'] = 'text/plain' body = 'foo-bar and others' when '/error' handle_exception(env) do raise "my error" end status = 500 when '/redirect' headers['Location'] = '/target' status = 302 when '/arg_error' handle_exception(env) do tester 234 end status = 500 when '/not_found' status = 404 body = "Not found" else status = 404 end [status, headers, [body]] } end describe "assert-response" do it "should check json" do get '/json' assert_response_json /ok/ end it "should check html" do get '/html' assert_response_html '' end it "should check css" do get '/css' assert_response_css 'color' end it "should check js" do get '/js' assert_response_js 'alert' end it "should check text" do get '/text' assert_response do is_text body 'foo-bar' end end it "should check redirect" do get '/redirect' assert_response_redirect '/target' end it "should not find missing pages" do get '/settings' assert_response_not_found end it "check errors for some kind of sinatra errors" do get '/error' assert_response_raises end it "check errors for some other sinatra errors" do get '/arg_error' assert_response_raises ArgumentError end it "should raise errors for some other sinatra errors" do get '/error' assert_raises RuntimeError do assert_response_ok end end it "should raise errors for some other sinatra errors" do get '/arg_error' assert_raises ArgumentError do assert_response_ok end end it "should raise error for not_found when body is expected" do get '/not_there' assert_raises AssertResponse::Status404 do assert_response_html "hiho" end end it "should check not found html page" do get '/not_found' assert_response_not_found_html "Not found" end end