|
9 | 9 | describe Utopia::Redirection do |
10 | 10 | include ProtocolApplication |
11 | 11 |
|
| 12 | + def tracked_body(name, events) |
| 13 | + body = Protocol::HTTP::Body::Buffered.wrap([name.to_s]) |
| 14 | + |
| 15 | + body.define_singleton_method(:close) do |error = nil| |
| 16 | + events << [name, error] |
| 17 | + super(error) |
| 18 | + end |
| 19 | + |
| 20 | + return body |
| 21 | + end |
| 22 | + |
12 | 23 | let(:app) do |
13 | 24 | Utopia::Application.build(lambda{|request| |
14 | 25 | case request.path_info |
|
72 | 83 | expect(body).to be == "File not found :(" |
73 | 84 | end |
74 | 85 |
|
| 86 | + it "closes the response replaced by an error document" do |
| 87 | + events = [] |
| 88 | + application = Utopia::Application.build(lambda do |request| |
| 89 | + if request.path_info == "/error" |
| 90 | + Utopia::Response[200, {}, tracked_body(:error, events)] |
| 91 | + else |
| 92 | + Utopia::Response[404, {}, tracked_body(:original, events)] |
| 93 | + end |
| 94 | + end) do |
| 95 | + use Utopia::Redirection::Errors, 404 => "/error" |
| 96 | + end |
| 97 | + |
| 98 | + response = application.call(Protocol::HTTP::Request["GET", "/missing"]) |
| 99 | + |
| 100 | + expect(response.status).to be == 404 |
| 101 | + expect(events).to be == [[:original, nil]] |
| 102 | + expect(response.read).to be == "error" |
| 103 | + end |
| 104 | + |
75 | 105 | it "should blow up if internal error redirect also fails" do |
76 | 106 | expect{get "/teapot"}.to raise_exception Utopia::Redirection::RequestFailure |
77 | 107 | end |
78 | 108 |
|
| 109 | + it "closes both responses when the error document fails" do |
| 110 | + events = [] |
| 111 | + application = Utopia::Application.build(lambda do |request| |
| 112 | + if request.path_info == "/error" |
| 113 | + Utopia::Response[500, {}, tracked_body(:error, events)] |
| 114 | + else |
| 115 | + Utopia::Response[404, {}, tracked_body(:original, events)] |
| 116 | + end |
| 117 | + end) do |
| 118 | + use Utopia::Redirection::Errors, 404 => "/error" |
| 119 | + end |
| 120 | + |
| 121 | + expect do |
| 122 | + application.call(Protocol::HTTP::Request["GET", "/missing"]) |
| 123 | + end.to raise_exception(Utopia::Redirection::RequestFailure) |
| 124 | + |
| 125 | + expect(events.size).to be == 2 |
| 126 | + expect(events[0]).to be == [:original, nil] |
| 127 | + expect(events[1][0]).to be == :error |
| 128 | + expect(events[1][1]).to be_a(Utopia::Redirection::RequestFailure) |
| 129 | + end |
| 130 | + |
79 | 131 | it "should redirect deep url to top" do |
80 | 132 | get "/hierarchy/a/b/c/d/e" |
81 | 133 |
|
|
0 commit comments