File: error.rb

package info (click to toggle)
ruby-ramaze 2012.12.08-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,060 kB
  • ctags: 1,200
  • sloc: ruby: 10,446; makefile: 8
file content (88 lines) | stat: -rw-r--r-- 2,386 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
88
#          Copyright (c) 2009 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the MIT license.

require File.expand_path('../../../spec/helper', __FILE__)
require 'rexml/document'
require 'rexml/xpath'

class SpecError < Ramaze::Controller
  map '/'

  def raises
    blah
  end

  def empty
    response.status = 404
    ''
  end
end

class SpecErrorHandling < SpecError
  map '/handle'

  def self.action_missing(path)
    try_resolve("/not_found")
  end

  def not_found
    "Sorry, this document doesn't exist"
  end

  def name_error
    "Sorry, this name doesn't exist"
  end
end

describe 'Error handling' do
  behaves_like :rack_test

  Ramaze.options.mode = :dev

  it 'uses Rack::ShowException to display errors' do
    got = get('/raises')
    [got.status, got['Content-Type']].should == [500, 'text/html']

    # we use this xpath notation because otherwise rexml is really slow...
    doc = REXML::Document.new(got.body)
    REXML::XPath.first(doc, "/html/body/div[1]/h1").text.
      should == "NameError at /raises"
    REXML::XPath.first(doc, "/html/body/div[4]/p/code").text.
      should == "Rack::ShowExceptions"
  end

  it 'uses original action_missing when no action was found' do
    got = get('/missing')
    [got.status, got['Content-Type']].should == [404, 'text/plain']

    got.body.should == 'No action found at: "/missing"'
  end

  it 'uses custom action_missing when no action was found' do
    got = get('/handle/mssing')
    [got.status, got['Content-Type']].should == [200, 'text/html']

    got.body.should == "Sorry, this document doesn't exist"
  end

  it 'uses Rack::RouteExceptions when a route is set' do
    Rack::RouteExceptions.route(NameError, '/handle/name_error')

    got = get('/raises')
    [got.status, got['Content-Type']].should == [200, 'text/html']

    got.body.should == "Sorry, this name doesn't exist"
  end

  it 'uses Rack::ShowStatus for empty responses > 400' do
    got = get('/empty')
    [got.status, got['Content-Type']].should == [404, 'text/html']

    # we use this xpath notation because otherwise rexml is really slow...
    doc = REXML::Document.new(got.body)
    REXML::XPath.first(doc, "/html/body/div[1]/h1").text.strip.
      should == "Not Found"
    REXML::XPath.first(doc, "/html/body/div[3]/p/code").text.
      should == "Rack::ShowStatus"
  end
end