Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:07:41+00:00 2026-05-27T19:07:41+00:00

Context : an application uses a piece of Rack middleware that must be setup

  • 0

Context: an application uses a piece of Rack middleware that must be setup in config.ru, rather than Rails’s internal Middleware chain. This is for reasons not relevant to this question.

Question: how do I make my tests (functional and integration) aware of this middleware?

I’ll ellaborate with an example. Let’s create a pristine Rails 3 app, using rack-rewrite for illustration purposes.

# /config/initializers/example.rb
Rails.application.middleware.insert 0, 'Rack::Rewrite' do
 r301 '/so', 'http://stackoverflow.com'
end

# /test/integration/the_test.rb
require 'test_helper'

class TheTest < ActionDispatch::IntegrationTest
 test "redirect from /so to http://stackoverflow.com" do
   get '/so'
   assert_redirected_to 'http://stackoverflow.com'
 end
end

If you run the above test, all is good, and with the browser you can confirm that visiting the path /so will redirect you to StackOverflow indeed.

Cool, let’s now set this up outside Rails then. Remove the file /config/initializers/example.rb described above, and change config.ru to the following:

# /config.ru
require ::File.expand_path('../config/environment',  __FILE__)

map '/so' do
  run Rack::Rewrite do
    r301 '', 'http://stackoverflow.com'
  end
end

map '/' do
  run Deleteme::Application
end

Now, the test will stop working. The functionality does work, as evidenced if you visit /so with your browser. It’s only that the tests are not aware of that Rack setup.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:07:41+00:00Added an answer on May 27, 2026 at 7:07 pm

    (Thanks Dan Croak for setting me on the right track. This answer completes what he said).

    Short answer

    Can’t be done for functional tests.

    For integration tests, add the following to your test_helper.rb:

    ActionDispatch::IntegrationTest.app = Rack::Builder.new do
      eval File.read(Rails.root.join('config.ru'))
    end
    

    Long answer

    Functional tests are just unit tests run against controllers. When you say, for example get :index, you are not resolving a route. Instead, this is a shorthand for calling the index method, with a request environment that includes a mention to the HTTP method being GET.

    Therefore, it makes sense that your Rack stack does not influence functional tests.

    Integration tests are a different matter. You are testing your full stack there, so you will want to test your Rack middlewares too. Only problem is that, by default, these tests only see the middlewares that you add using Rails’s own API.

    But I say “by default” because Rails offers a way to change what Rack stack will be tested. By default, integration tests call Rails.application for their requests. You can change this to anything that suits your needs.

    By using the code above, we use Rack::Builder create an ad-hoc Rack application. Inside the block, you can put any code that you would normally put inside a config.ru file. To avoid code duplication, eval loads our config.ru file. Now our integration tests will load exactly the same application that our app servers expose.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is part of my web.xml: <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:application-config.xml </param-value> </context-param> application-config.xml uses
The context : I have a rails engine (gem) that uses omniauth inside it
I have content management system application that uses a polymorphic tree table as the
I know that in a standalone application I create one of the application context
Context In our application, we snapshot data from a database using a query that
I have an application that uses Entity Framework to access the data, and throughout
I'm writing web application that uses Spring MVC to bind Spring beans with REST-like
I have an application that uses a file on the SD card, the application
We have a web application that uses Spring (3.0.5) and CXF (currently 2.4.2 for
I have an application that uses Hibernate/JPA, with Spring and Jersey. In my application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.