'Testing Middleware with Rspec

I've written some Rack-Middleware and now I'm trying to test it with Rspec. But all Rack-Middleware is instantiated with an 'app' argument, that represents the Rails app itself. How do you guys mock this up in Rspec?

For example,

 describe MyMiddleWare do
    let(:app) { # How do I mock a Rails app object here? }
    subject { MyMiddleWare.new(app: app) }

    it 'should blah blah blah' do
       # a bunch of tests go here
    end
 end


Solution 1:[1]

I believe you should use request specs simulating an http request (your middleware should be included in rails middleware stack). See more details on rspec request specs here.

UPD I think I've found exactly what you need, with Test::Unit, but it's easy to rewrite for RSpec: rack-ssl-enforcer

Solution 2:[2]

I tested mine like so

describe Support::CharConverter do

  let(:env_hash) do
    {
      "HTTP_REFERER" => "", 
      "PATH_INFO" => "foo",
      "QUERY_STRING" => "bar",
      "REQUEST_PATH" => "is",
      "REQUEST_URI" => "here",
    }
  end

  subject do 
    Support::CharConverter.new(env_hash)
  end

  context 'sanitize_env' do

    it 'should keep key values the same if nothing to sanitize' do
      sanitized_hash = subject.sanitize_env(env_hash)
      # k = env_hash.keys[5]
      # v = env_hash.values[5]
      env_hash.each do |k, v|
        sanitized_hash[k].encoding.name.should eq("US-ASCII")
        sanitized_hash[k].should eq(v)
        sanitized_hash[k].valid_encoding?.should eq(true)
      end
    end
  end
end

Solution 3:[3]

I'm writing a Sinatra app with a middleware and I followed @ritchie's path, and ended up creating an app that looks like this:

# Return the params in the response body as JSON
class ParamsInResponseApp < Sinatra::Base
  def call(env)
    super

    rack_params = env["rack.request.query_hash"]

    [200, {"Content-Type" => "application/json"}, [{"app" => rack_params}.to_json]]
  end
end

Inheriting from Sinatra::Base and calling super allowed me to get the params from env["rack.request.query_hash"], which was otherwise nil.

Note that this app puts the params in the response, so I can check them without having to stub anything.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Davinj
Solution 3 ecoologic