summary refs log tree commit
path: root/test/spec_show_exceptions.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/spec_show_exceptions.rb')
-rw-r--r--test/spec_show_exceptions.rb63
1 files changed, 59 insertions, 4 deletions
diff --git a/test/spec_show_exceptions.rb b/test/spec_show_exceptions.rb
index cd44c816..a4ade121 100644
--- a/test/spec_show_exceptions.rb
+++ b/test/spec_show_exceptions.rb
@@ -1,4 +1,6 @@
-require 'minitest/autorun'
+# frozen_string_literal: true
+
+require 'minitest/global_expectations/autorun'
 require 'rack/show_exceptions'
 require 'rack/lint'
 require 'rack/mock'
@@ -25,6 +27,24 @@ describe Rack::ShowExceptions do
     assert_match(res, /ShowExceptions/)
   end
 
+  it "works with binary data in the Rack environment" do
+    res = nil
+
+    # "\xCC" is not a valid UTF-8 string
+    req = Rack::MockRequest.new(
+      show_exceptions(
+        lambda{|env| env['foo'] = "\xCC"; 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
 
@@ -35,11 +55,11 @@ describe Rack::ShowExceptions do
 
     [
       # Serve text/html when the client accepts text/html
-      ["text/html", ["/", {"HTTP_ACCEPT" => "text/html"}]],
-      ["text/html", ["/", {"HTTP_ACCEPT" => "*/*"}]],
+      ["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"}]]
+      ["text/plain", ["/", { "HTTP_ACCEPT" => "application/json" }]]
     ].each do |exmime, rargs|
       res = req.get(*rargs)
 
@@ -77,4 +97,39 @@ describe Rack::ShowExceptions do
     assert_match(res, /ShowExceptions/)
     assert_match(res, /unknown location/)
   end
+
+  it "allows subclasses to override template" do
+    c = Class.new(Rack::ShowExceptions) do
+      TEMPLATE = ERB.new("foo")
+
+      def template
+        TEMPLATE
+      end
+    end
+
+    app = lambda { |env| raise RuntimeError, "", [] }
+
+    req = Rack::MockRequest.new(
+      Rack::Lint.new c.new(app)
+    )
+
+    res = req.get("/", "HTTP_ACCEPT" => "text/html")
+
+    res.must_be :server_error?
+    res.status.must_equal 500
+    res.body.must_equal "foo"
+  end
+
+  it "knows to prefer plaintext for non-html" do
+    # We don't need an app for this
+    exc = Rack::ShowExceptions.new(nil)
+
+    [
+      [{ "HTTP_ACCEPT" => "text/plain" }, true],
+      [{ "HTTP_ACCEPT" => "text/foo" }, true],
+      [{ "HTTP_ACCEPT" => "text/html" }, false]
+    ].each do |env, expected|
+      assert_equal(expected, exc.prefers_plaintext?(env))
+    end
+  end
 end