Sha256: 3d68df362f4caf6aefad2dd4c7a013548742a1b001f1c3bbefa5080d53f1be76

Contents?: true

Size: 1.95 KB

Versions: 35

Compression:

Stored size: 1.95 KB

Contents

require 'spec_helper'
require 'singleton'
require 'startback/web/catch_all'

module Startback
  module Web
    describe CatchAll do
      include Rack::Test::Methods

      context 'when used without context' do
        def app
          Rack::Builder.new do
            use CatchAll
            run ->(env){ raise "Hello error" }
          end
        end

        it 'returns a 500 with json explanation' do
          get '/'
          expect(last_response.status).to eql(500)
          expect(last_response.content_type).to eql("application/json")
          result = JSON.parse(last_response.body)
          expect(result).to eql({
            "code" => "Startback::Errors::InternalServerError",
            "description" => "An error occured, sorry"
          })
        end
      end

      context 'when used with a context providing an error handler' do

        class AnError < StandardError
        end

        class ErrorHandler
          include Singleton

          attr_reader :ex

          def fatal(ex)
            @ex = ex
          end

        end

        class MyContextWithErrorHandler < Startback::Context

          def error_handler
            ErrorHandler.instance
          end

        end

        def app
          Rack::Builder.new do
            use Context::Middleware, MyContextWithErrorHandler.new
            use CatchAll
            run ->(env){ raise AnError, "Hello error" }
          end
        end

        it 'returns a 500 with json explanation' do
          get '/'
          expect(last_response.status).to eql(500)
          expect(last_response.content_type).to eql("application/json")
          result = JSON.parse(last_response.body)
          expect(result).to eql({
            "code" => "Startback::Errors::InternalServerError",
            "description" => "An error occured, sorry"
          })
          expect(ErrorHandler.instance.ex).to be_a(AnError)
        end
      end

    end # CatchAll
  end # module Web
end # module Startback

Version data entries

35 entries across 35 versions & 3 rubygems

Version Path
startback-1.1.0 spec/unit/web/test_catch_all.rb
startback-1.0.3 spec/unit/web/test_catch_all.rb
startback-1.0.2 spec/unit/web/test_catch_all.rb
startback-1.0.1 spec/unit/web/test_catch_all.rb
startback-1.0.0 spec/unit/web/test_catch_all.rb
startback-0.19.4 spec/unit/web/test_catch_all.rb
startback-0.19.3 spec/unit/web/test_catch_all.rb
startback-0.19.1 spec/unit/web/test_catch_all.rb
startback-0.19.0 spec/unit/web/test_catch_all.rb
startback-0.18.2 spec/unit/web/test_catch_all.rb
startback-0.18.1 spec/unit/web/test_catch_all.rb
startback-0.18.0 spec/unit/web/test_catch_all.rb
startback-0.17.4 spec/unit/web/test_catch_all.rb
startback-0.17.3 spec/unit/web/test_catch_all.rb
startback-0.17.2 spec/unit/web/test_catch_all.rb
startback-0.17.1 spec/unit/web/test_catch_all.rb
startback-0.17.0 spec/unit/web/test_catch_all.rb
startback-0.16.0 spec/unit/web/test_catch_all.rb
startback-0.15.5 spec/unit/web/test_catch_all.rb
startback-0.15.4 spec/unit/web/test_catch_all.rb