require 'backports' if RUBY_VERSION =~ /1.8/ require_relative "helper.rb" require_relative File.join("..", "lib", "assert-response.rb") ENV['RACK_ENV'] = 'test' 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 '/create_with_json' headers['Content-Type'] = 'application/json' body = '{"id":"new_id"}' status = 201 when '/bad_request' body = 'you are so bad!' status = 400 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 '/moved' headers['Location'] = '/target' status = 301 when '/arg_error' handle_exception(env) do tester 234 end status = 500 when '/error_code' status = 500 body = "some error message" when '/error_with_wrong_contenttype' status = 500 headers['Content-Type'] = 'application/doesnotexists' body = "some error message" when '/not_found' status = 404 body = "Not found" else status = 404 end [status, headers, [body]] } end describe "without assert-response" do it "should raise no error" do get '/error' assert_equal 500, last_response.status end end describe "assert-response" do it "should check json" do get '/json' assert_response_body /ok/, :ok assert_response_json /ok/ end it "should check html" do get '/html' assert_response_html '' end it "should check for status regex" do get '/html' assert_response_status /200/ end it "should check for json body content" do get '/json' assert_response_json do assert_equal "ok", json["response"] end end it "should check for json body content for a created ressource" do get '/create_with_json' assert_response_json :created do assert_equal "new_id", json["id"] end end it "should check for json body content without assert prefix" do get '/create_with_json' assert_response_json :created do equal "new_id", json["id"] end 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 check moved" do get '/moved' assert_response_moved '/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 "check server-error without a raised error" do get '/error_code' assert_response_error assert_response_body "some error message", :error end it "should raise errors for some other sinatra errors" do get '/error' assert_raises RuntimeError do assert_response_ok end end it "should check the error before the content type" do get '/error_with_wrong_contenttype' assert_raises AssertResponse::HttpError do assert_response_html "huhi" 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 describe "status tests" do it "should check for ok status (20x) if asked to do, or if nil is passed to status" do get '/html' assert_response_ok assert_response_status nil assert_response_status 200 get '/create_with_json' assert_response_ok assert_response_status nil assert_response_status 201 get '/not_found' assert_raises AssertResponse::Status404 do assert_response_ok end assert_raises AssertResponse::Status404 do assert_response_status nil end assert_raises AssertResponse::Status404 do assert_response_status 201 end end it "check for a named status if asked to do so" do get '/create_with_json' assert_response_created assert_response_status :created assert_response_status 201 get '/not_found' end it "check for a regexp status if asked to do so" do get '/create_with_json' assert_response_created assert_response_status /20/ get '/html' assert_response_status 200 assert_response_status /20/ end it "should raise an error if asked for a non 4xx/5xx code and a 4xx/5xx code is returned" do { '/error_code' => AssertResponse::HttpError, '/error' => RuntimeError, '/not_found' => AssertResponse::Status404 }.each do |url,error| get url assert_raises error do assert_response_ok end assert_raises error do assert_response_status nil end assert_raises error do assert_response_created end assert_raises error do assert_response_status :moved end assert_raises error do assert_response_status /^3/ end end end it "should raise no error if asked for a 4xx/5xx code and a 4xx/5xx code is returned" do get '/error_code' assert_response_status 500 assert_response_status /^5/ assert_response_status :error get '/not_found' assert_response_status 404 assert_response_status /^4/ assert_response_status :not_found end end