File: spec_showexceptions.rb

package info (click to toggle)
ruby-rack 1.4.1-2.1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,312 kB
  • sloc: ruby: 11,357; makefile: 2
file content (87 lines) | stat: -rw-r--r-- 2,109 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'rack/showexceptions'
require 'rack/mock'

describe Rack::ShowExceptions do
  it "catches exceptions" do
    res = nil

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

    lambda{
      res = req.get("/")
    }.should.not.raise

    res.should.be.a.server_error
    res.status.should.equal 500

    res.should =~ /RuntimeError/
    res.should =~ /ShowExceptions/
  end

  it "responds with plain text on AJAX requests accepting anything but HTML" do
    res = nil

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

    lambda{
      res = req.get("/", "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest")
    }.should.not.raise

    res.should.be.a.server_error
    res.status.should.equal 500

    res.content_type.should.equal "text/plain"

    res.body.should.include "RuntimeError: It was never supposed to work\n"
    res.body.should.include __FILE__
  end

  it "responds with HTML on AJAX requests accepting HTML" do
    res = nil

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

    lambda{
      res = req.get("/", "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest", "HTTP_ACCEPT" => "text/html")
    }.should.not.raise

    res.should.be.a.server_error
    res.status.should.equal 500

    res.content_type.should.equal "text/html"

    res.body.should.include "RuntimeError"
    res.body.should.include "It was never supposed to work"
    res.body.should.include Rack::Utils.escape_html(__FILE__)
  end

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

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

    lambda{
      res = req.get("/")
    }.should.not.raise

    res.should.be.a.server_error
    res.status.should.equal 500

    res.should =~ /RuntimeError/
    res.should =~ /ShowExceptions/
    res.should =~ /unknown location/
  end
end