spec_showexceptions.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. require 'rack/showexceptions'
  2. require 'rack/mock'
  3. describe Rack::ShowExceptions do
  4. it "catches exceptions" do
  5. res = nil
  6. req = Rack::MockRequest.new(
  7. Rack::ShowExceptions.new(
  8. lambda{|env| raise RuntimeError }
  9. ))
  10. lambda{
  11. res = req.get("/")
  12. }.should.not.raise
  13. res.should.be.a.server_error
  14. res.status.should.equal 500
  15. res.should =~ /RuntimeError/
  16. res.should =~ /ShowExceptions/
  17. end
  18. it "responds with plain text on AJAX requests accepting anything but HTML" do
  19. res = nil
  20. req = Rack::MockRequest.new(
  21. Rack::ShowExceptions.new(
  22. lambda{|env| raise RuntimeError, "It was never supposed to work" }
  23. ))
  24. lambda{
  25. res = req.get("/", "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest")
  26. }.should.not.raise
  27. res.should.be.a.server_error
  28. res.status.should.equal 500
  29. res.content_type.should.equal "text/plain"
  30. res.body.should.include "RuntimeError: It was never supposed to work\n"
  31. res.body.should.include __FILE__
  32. end
  33. it "responds with HTML on AJAX requests accepting HTML" do
  34. res = nil
  35. req = Rack::MockRequest.new(
  36. Rack::ShowExceptions.new(
  37. lambda{|env| raise RuntimeError, "It was never supposed to work" }
  38. ))
  39. lambda{
  40. res = req.get("/", "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest", "HTTP_ACCEPT" => "text/html")
  41. }.should.not.raise
  42. res.should.be.a.server_error
  43. res.status.should.equal 500
  44. res.content_type.should.equal "text/html"
  45. res.body.should.include "RuntimeError"
  46. res.body.should.include "It was never supposed to work"
  47. res.body.should.include Rack::Utils.escape_html(__FILE__)
  48. end
  49. it "handles exceptions without a backtrace" do
  50. res = nil
  51. req = Rack::MockRequest.new(
  52. Rack::ShowExceptions.new(
  53. lambda{|env| raise RuntimeError, "", [] }
  54. )
  55. )
  56. lambda{
  57. res = req.get("/")
  58. }.should.not.raise
  59. res.should.be.a.server_error
  60. res.status.should.equal 500
  61. res.should =~ /RuntimeError/
  62. res.should =~ /ShowExceptions/
  63. res.should =~ /unknown location/
  64. end
  65. end