Sha256: e31a9ea66dc7b17f67ed8c63f5d20db6a48b110cd8d6a7d3016ba64eeb17da33

Contents?: true

Size: 1.72 KB

Versions: 19

Compression:

Stored size: 1.72 KB

Contents

require "spec_helper"

describe Pliny::Middleware::RescueErrors do
  include Rack::Test::Methods

  class BadMiddleware
    def call(env)
      if env["PATH_INFO"] == "/api-error"
        raise Pliny::Errors::ServiceUnavailable
      else
        raise "Omg!"
      end
    end
  end

  def app
    @app
  end

  it "intercepts Pliny errors and renders" do
    @app = new_rack_app
    get "/api-error"
    assert_equal 503, last_response.status
    error_json = MultiJson.decode(last_response.body)
    assert_equal "service_unavailable", error_json["id"]
    assert_equal "Service unavailable.", error_json["message"]
  end

  it "intercepts exceptions and renders" do
    @app = new_rack_app
    expect(Pliny::ErrorReporters).to receive(:notify)
    get "/"
    assert_equal 500, last_response.status
    error_json = MultiJson.decode(last_response.body)
    assert_equal "internal_server_error", error_json["id"]
    assert_equal "Internal server error.", error_json["message"]
  end

  it "raises given the raise option" do
    @app = new_rack_app(raise: true)
    assert_raises(RuntimeError) do
      get "/"
    end
  end

  it "uses a custom error message with the message option" do
    @app = new_rack_app(message: "Please stand by")
    allow(Pliny::ErrorReporters).to receive(:notify)
    get "/"
    assert_equal 500, last_response.status
    error_json = MultiJson.decode(last_response.body)
    assert_equal "internal_server_error", error_json["id"]
    assert_equal "Please stand by", error_json["message"]
  end

  private

  def new_rack_app(options = {})
    Rack::Builder.new do
      use Rack::Lint
      use Pliny::Middleware::RescueErrors, raise: options[:raise], message: options[:message]
      run BadMiddleware.new
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
pliny-1.1.0 spec/middleware/rescue_errors_spec.rb
pliny-1.0.0 spec/middleware/rescue_errors_spec.rb
pliny-0.32.0 spec/middleware/rescue_errors_spec.rb
pliny-0.31.0 spec/middleware/rescue_errors_spec.rb
pliny-0.30.1 spec/middleware/rescue_errors_spec.rb
pliny-0.30.0 spec/middleware/rescue_errors_spec.rb
pliny-0.29.0 spec/middleware/rescue_errors_spec.rb
pliny-0.28.0 spec/middleware/rescue_errors_spec.rb
pliny-0.27.1 spec/middleware/rescue_errors_spec.rb
pliny-0.27.0 spec/middleware/rescue_errors_spec.rb
pliny-0.26.2 spec/middleware/rescue_errors_spec.rb
pliny-0.26.1 spec/middleware/rescue_errors_spec.rb
pliny-0.26.0 spec/middleware/rescue_errors_spec.rb
pliny-0.25.1 spec/middleware/rescue_errors_spec.rb
pliny-0.25.0 spec/middleware/rescue_errors_spec.rb
pliny-0.24.0 spec/middleware/rescue_errors_spec.rb
pliny-0.23.0 spec/middleware/rescue_errors_spec.rb
pliny-0.22.0 spec/middleware/rescue_errors_spec.rb
pliny-0.21.0 spec/middleware/rescue_errors_spec.rb