rack-test

Rack::Test is a layer on top of Rack's MockRequest similar to Merb's RequestHelper

  • 所有者: rack/rack-test
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Rack::Test

Gem Version

Code Climate
Test Coverage

Code: https://github.com/rack-test/rack-test

Description

Rack::Test is a small, simple testing API for Rack apps. It can be used on its
own or as a reusable starting point for Web frameworks and testing libraries
to build on.

Features

  • Maintains a cookie jar across requests
  • Easily follow redirects when desired
  • Set request headers to be used by all subsequent requests
  • Small footprint. Approximately 200 LOC

Supported platforms

  • 2.2.2+
  • 2.3
  • 2.4
  • JRuby 9.1.+

If you are using Ruby 1.8, 1.9 or JRuby 1.7, use rack-test 0.6.3.

Known incompatibilites

  • rack-test >= 0.71 does not work with older Capybara versions (< 2.17). See #214 for more details.

Examples

(The examples use Test::Unit but it's equally possible to use rack-test with other testing frameworks like rspec.)

require "test/unit"
require "rack/test"

class HomepageTest < Test::Unit::TestCase
  include Rack::Test::Methods

  def app
    app = lambda {, env, [200, {'Content-Type' => 'text/plain'}, ['All responses are OK']] }
    builder = Rack::Builder.new
    builder.run app
  end

  def test_response_is_ok
    get '/'

    assert last_response.ok?
    assert_equal last_response.body, 'All responses are OK'
  end

  def set_request_headers
    header 'Accept-Charset', 'utf-8'
    get '/'

    assert last_response.ok?
    assert_equal last_response.body, 'All responses are OK'
  end

  def test_response_is_ok_for_other_paths
    get '/other_paths'

    assert last_response.ok?
    assert_equal last_response.body, 'All responses are OK'
  end

  def post_with_json
    # No assertion in this, we just demonstrate how you can post a JSON-encoded string.
    # By default, Rack::Test will use HTTP form encoding if you pass in a Hash as the
    # parameters, so make sure that `json` below is already a JSON-serialized string.
    post(uri, json, { 'CONTENT_TYPE' => 'application/json' })
  end

  def delete_with_url_params_and_body
    delete '/?foo=bar', JSON.generate('baz' => 'zot')
  end
end

If you want to test one app in isolation, you just return that app as shown above. But if you want to test the entire app stack, including middlewares, cascades etc. you need to parse the app defined in config.ru.

OUTER_APP = Rack::Builder.parse_file("config.ru").first

class TestApp < Test::Unit::TestCase
  include Rack::Test::Methods

  def app
    OUTER_APP
  end

  def test_root
    get "/"
    assert last_response.ok?
  end
end

Install

To install the latest release as a gem:

gem install rack-test

Or via Bundler:

gem 'rack-test'

Or to install unreleased version via Bundler:

gem 'rack-test', github: 'rack-test', branch: 'master'

Authors

  • Contributions from Bryan Helmkamp, Simon Rozet, Pat Nakajima and others
  • Much of the original code was extracted from Merb 1.0's request helper

License

rack-test is released under the MIT License.

Contribution

Contributions are welcome. Please make sure to:

  • Use a regular forking workflow
  • Write tests for the new or changed behaviour
  • Provide an explanation/motivation in your commit message / PR message
  • Ensure History.md is updated

Releasing

  • Ensure History.md is up-to-date
  • Bump VERSION in lib/rack/test/version.rb
  • git commit . -m 'Release 1.1.0'
  • git push
  • bundle exec thor :release
  • Updated the GitHub releases page

主要指标

概览
名称与所有者rack/rack-test
主编程语言Ruby
编程语言Ruby (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2009-02-24 02:13:47
推送于2025-05-22 03:58:26
最后一次提交
发布数29
最新版本名称v2.2.0 (发布于 2024-12-23 10:48:02)
第一版名称v0.1.0 (发布于 2009-03-04 23:21:12)
用户参与
星数0.9k
关注者数29
派生数246
提交数545
已启用问题?
问题数139
打开的问题数0
拉请求数118
打开的拉请求数1
关闭的拉请求数90
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?