Created
April 23, 2010 17:13
-
-
Save bhb/376828 to your computer and use it in GitHub Desktop.
example code for http://bbrinck.com/post/543491001/intermediate-rack-middleware-tips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Rack | |
class Cap | |
def initialize(app, options = {}) | |
@app = app | |
end | |
def call(env) | |
response = @app.call(env) | |
status, headers, body = response | |
headers['Content-Length']= body.length.to_s | |
[status, headers, body.map {|x| x.upcase+"!"}] | |
end | |
end | |
end | |
require 'test/unit' | |
require 'shoulda' | |
require 'rack' | |
class CapTest < Test::Unit::TestCase | |
should 'capitalize body' do | |
inner_app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['Hello']] } | |
app = Rack::Cap.new(inner_app) | |
env = Rack::MockRequest.env_for('/hello') | |
status, headers, body = app.call(env) | |
assert_equal ['HELLO!'], body | |
end | |
should 'pass all Lint checks' do | |
inner_app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['Hello']] } | |
app = Rack::Lint.new(Rack::Cap.new(inner_app)) | |
env = Rack::MockRequest.env_for('/hello') | |
app.call(env) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment