Skip to content

Instantly share code, notes, and snippets.

@kesor
Created June 5, 2012 05:44
Show Gist options
  • Save kesor/2872916 to your computer and use it in GitHub Desktop.
Save kesor/2872916 to your computer and use it in GitHub Desktop.
rack-middleware blog post
require 'rack/test'
describe SampleMiddleware do
include Rack::Test::Methods
let(:inner_app) do
lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['All good!'] }
end
let(:app) { SampleMiddleware.new(inner_app) }
it "adds hello:world to session" do
get "/"
last_request.session['hello'].should == 'world'
end
it "makes no change to response status" do
get "/"
last_response.should be_ok
end
end
class SampleMiddleware
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request(env)
request.session['hello'] = 'world'
@app.call(env)
end
end
class SomeMiddleware(object):
def process_request(self, request):
# alter the request in some way
return None # or return a response
def process_response(self, request, response):
# alter the response in some way
return response
class SomeMiddleware
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request(env)
# alter the request in some way
response = @app.call(env)
# alter the response in some way
return response
end
end
@cmaujean
Copy link

cmaujean commented May 9, 2013

I'm pretty sure line 7 is missing a closing ] and should be:

  lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['All good!'] ] }

@potomak
Copy link

potomak commented Aug 30, 2013

Rack::Request.new(env)

instead of

Rack::Request(env)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment