Sha256: 1c422cc169e5573cb086f87fbed5c93515de685dee1f860d3f5bc80f45a052d6

Contents?: true

Size: 1.92 KB

Versions: 24

Compression:

Stored size: 1.92 KB

Contents

require 'minitest/autorun'
require 'rack/show_exceptions'
require 'rack/lint'
require 'rack/mock'

describe Rack::ShowExceptions do
  def show_exceptions(app)
    Rack::Lint.new Rack::ShowExceptions.new(app)
  end

  it "catches exceptions" do
    res = nil

    req = Rack::MockRequest.new(
      show_exceptions(
        lambda{|env| raise RuntimeError }
    ))

    res = req.get("/", "HTTP_ACCEPT" => "text/html")

    res.must_be :server_error?
    res.status.must_equal 500

    assert_match(res, /RuntimeError/)
    assert_match(res, /ShowExceptions/)
  end

  it "responds with HTML only to requests accepting HTML" do
    res = nil

    req = Rack::MockRequest.new(
      show_exceptions(
        lambda{|env| raise RuntimeError, "It was never supposed to work" }
    ))

    [
      # Serve text/html when the client accepts text/html
      ["text/html", ["/", {"HTTP_ACCEPT" => "text/html"}]],
      ["text/html", ["/", {"HTTP_ACCEPT" => "*/*"}]],
      # Serve text/plain when the client does not accept text/html
      ["text/plain", ["/"]],
      ["text/plain", ["/", {"HTTP_ACCEPT" => "application/json"}]]
    ].each do |exmime, rargs|
      res = req.get(*rargs)

      res.must_be :server_error?
      res.status.must_equal 500

      res.content_type.must_equal exmime

      res.body.must_include "RuntimeError"
      res.body.must_include "It was never supposed to work"

      if exmime == "text/html"
        res.body.must_include '</html>'
      else
        res.body.wont_include '</html>'
      end
    end
  end

  it "handles exceptions without a backtrace" do
    res = nil

    req = Rack::MockRequest.new(
      show_exceptions(
        lambda{|env| raise RuntimeError, "", [] }
      )
    )

    res = req.get("/", "HTTP_ACCEPT" => "text/html")

    res.must_be :server_error?
    res.status.must_equal 500

    assert_match(res, /RuntimeError/)
    assert_match(res, /ShowExceptions/)
    assert_match(res, /unknown location/)
  end
end

Version data entries

24 entries across 23 versions & 9 rubygems

Version Path
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-2.0.5/test/spec_show_exceptions.rb
tdiary-5.0.11 vendor/bundle/gems/rack-2.0.5/test/spec_show_exceptions.rb
sprokovuln-0.2.0 vendor/ruby/gems/rack-2.0.5/test/spec_show_exceptions.rb
tdiary-5.0.9 vendor/bundle/gems/rack-2.0.5/test/spec_show_exceptions.rb
rack-2.0.5 test/spec_show_exceptions.rb
tdiary-5.0.8 vendor/bundle/gems/rack-2.0.4/test/spec_show_exceptions.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/rack-2.0.4/test/spec_show_exceptions.rb
rack-2.0.4 test/spec_show_exceptions.rb
pract6-0.1.0 .gem/ruby/2.3.0/gems/rack-2.0.3/test/spec_show_exceptions.rb
rack-2.0.3 test/spec_show_exceptions.rb
rack-2.0.2 test/spec_show_exceptions.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/rack-2.0.1/test/spec_show_exceptions.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/rack-2.0.1/test/spec_show_exceptions.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/rack-2.0.1/test/spec_show_exceptions.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/rack-2.0.1/test/spec_show_exceptions.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/rack-2.0.1/test/spec_show_exceptions.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/rack-2.0.1/test/spec_show_exceptions.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/rack-2.0.1/test/spec_show_exceptions.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/rack-2.0.1/test/spec_show_exceptions.rb
abaci-0.3.0 vendor/bundle/gems/rack-2.0.1/test/spec_show_exceptions.rb