Browse files

Rack::Evil - The name says it all

  • Loading branch information...
1 parent b472765 commit 50fd7a330cbc905d87a81c6ae4e2e468e9d65496 @lifo lifo committed Jan 13, 2009
Showing with 31 additions and 0 deletions.
  1. +1 −0 lib/rack/contrib.rb
  2. +12 −0 lib/rack/contrib/evil.rb
  3. +18 −0 test/spec_rack_evil.rb
View
1 lib/rack/contrib.rb
@@ -18,4 +18,5 @@ def self.release
autoload :Profiler, "rack/contrib/profiler"
autoload :Sendfile, "rack/contrib/sendfile"
autoload :TimeZone, "rack/contrib/time_zone"
+ autoload :Evil, "rack/contrib/evil"
end
View
12 lib/rack/contrib/evil.rb
@@ -0,0 +1,12 @@
+module Rack
+ class Evil
+ # Lets you return a response to the client immediately from anywhere ( M V or C ) in the code.
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ catch(:response) { @app.call(env) }
+ end
+ end
+end
View
18 test/spec_rack_evil.rb
@@ -0,0 +1,18 @@
+require 'rack/mock'
+require 'rack/contrib/evil'
+require 'erb'
+
+context "Rack::Evil" do
+ app = lambda do |env|
+ template = ERB.new("<%= throw :response, [404, {'Content-Type' => 'text/html'}, 'Never know where it comes from'] %>")
+ [200, {'Content-Type' => 'text/plain'}, template.result(binding)]
+ end
+
+ specify "should enable the app to return the response from anywhere" do
+ status, headers, body = Rack::Evil.new(app).call({})
+
+ status.should.equal 404
+ headers['Content-Type'].should.equal 'text/html'
+ body.should.equal 'Never know where it comes from'
+ end
+end

0 comments on commit 50fd7a3

Please sign in to comment.