Sha256: 3745c1c65cb0cfdbccb642e487fbd0610d7b395eb5379fb97c0ac1445e27f3c9

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 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

  private

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pliny-0.20.2 spec/middleware/rescue_errors_spec.rb
pliny-0.20.1 spec/middleware/rescue_errors_spec.rb
pliny-0.20.0 spec/middleware/rescue_errors_spec.rb
pliny-0.19.0 spec/middleware/rescue_errors_spec.rb
pliny-0.18.0 spec/middleware/rescue_errors_spec.rb