Skip to content

Simple HTTP server launcher for Rack application (Sinatra, Rails, etc...)

License

Notifications You must be signed in to change notification settings

YusukeIwaki/rack-test_server

Repository files navigation

Gem Version

Rack::TestServer

Just launch HTTP server for testing your Rack application.

require 'rack/test_server'

# Configuration with `rackup` compatible options.
server = Rack::TestServer.new(
           app: Rails.application,
           server: :puma,
           Host: '127.0.0.1',
           Port: 3000)

before(:suite) do
  # Just launch it on a Thread
  server.start_async
  server.wait_for_ready
end

Background

"System testing", introduced in Rails 5.1, consists of two remarkable features: Sharing a DB connection and Capybara integration.

Since transactional fixtures are available by sharing a DB connection, we can see any mocked situation (ex. logged in as admin, receiving a lot of notifications, ...) from real browsers, which dramatically improved the testing experiences. Only one limitations for using this feature is that we have to launch the HTTP server within the same process as test runnner's.

This library is designed just for launching Rack application in a Thread. We can simply use Selenium, Playwright or other browser automation libraries in system testing, without studying any DSL :)

Installation

gem 'rack-test_server'

and then bundle install.

NOTE: Ruby 2.3 users can use 0.1.5. (without rackup gem), however it is no longer supported.

Usage

If you are working with Rails application, add configuration like below in spec/support/system_testing_helper.rb:

require 'rack/test_server'

# Configure Rack server
#
# options for Rackup::Server
# @see https://github.com/rack/rack/blob/2.2.3/lib/rack/server.rb#L173
# options for Rack::Handler::Puma
# @see https://github.com/puma/puma/blob/v5.4.0/lib/rack/handler/puma.rb#L84
server = Rack::TestServer.new(
           app: Rails.application,
           server: :puma,
           Host: '127.0.0.1',
           Port: 3000)

RSpec.configure do
  # Launch Rails application.
  config.before(:suite) do
    server.start_async
    server.wait_for_ready
  end
end

If you are not Rails user, just replace Rails.application with your Rack application, and put the configuration file as you prefer. :)

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Rack::TestServer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

Simple HTTP server launcher for Rack application (Sinatra, Rails, etc...)

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published