pax_global_header00006660000000000000000000000064144041254760014521gustar00rootroot0000000000000052 comment=867a6f1fc8e5d5ed79d8393f881f7e4c101edbfa rack-test-2.1.0/000077500000000000000000000000001440412547600134165ustar00rootroot00000000000000rack-test-2.1.0/.document000066400000000000000000000000641440412547600152350ustar00rootroot00000000000000README.rdoc lib/**/*.rb History.txt MIT-LICENSE.txt rack-test-2.1.0/.github/000077500000000000000000000000001440412547600147565ustar00rootroot00000000000000rack-test-2.1.0/.github/workflows/000077500000000000000000000000001440412547600170135ustar00rootroot00000000000000rack-test-2.1.0/.github/workflows/ci.yml000066400000000000000000000013241440412547600201310ustar00rootroot00000000000000name: CI on: [push, pull_request] permissions: contents: read jobs: test: strategy: fail-fast: false matrix: os: [ ubuntu-20.04 ] ruby: [ 2.0.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 3.1, 3.2, jruby-9.3, jruby-9.4 ] gemfile: [ Gemfile, Gemfile.rack-1.x ] exclude: - ruby: 2.0.0 gemfile: Gemfile - ruby: 2.1 gemfile: Gemfile runs-on: ${{ matrix.os }} env: BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }} steps: - uses: actions/checkout@v2 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} bundler-cache: true - run: bundle exec rake spec rack-test-2.1.0/.gitignore000066400000000000000000000001251440412547600154040ustar00rootroot00000000000000pkg doc coverage /Gemfile*.lock VERSION *.rbc *.swp /rack-test-*.gem /.bundle /.idea rack-test-2.1.0/Gemfile000066400000000000000000000003171440412547600147120ustar00rootroot00000000000000source 'https://rubygems.org' gemspec # Runtime dependency if RUBY_VERSION >= '3.1' if RUBY_VERSION < '3.2' gem 'cgi', '0.3.6' end gem 'rack', github: 'rack/rack' else gem 'rack', '~> 2.0' end rack-test-2.1.0/Gemfile.rack-1.x000066400000000000000000000002101440412547600162250ustar00rootroot00000000000000source 'https://rubygems.org' gemspec # Runtime dependency if RUBY_VERSION < '2.1' gem 'rack', '< 1.4' else gem 'rack', '< 2' end rack-test-2.1.0/History.md000066400000000000000000000353301440412547600154050ustar00rootroot00000000000000## 2.1.0 / 2023-03-14 * Breaking changes: * Digest authentication support, deprecated in 2.0.0, has been removed (Jeremy Evans #307) * requiring rack/mock_session, deprecated in 2.0.0, has been removed (Jeremy Evans #307) * Minor enhancements: * The `original_filename` for `Rack::Test::UploadedFile` can now be set even if the content of the file comes from a file path (Stuart Chinery #314) * Add `Rack::Test::Session#restore_state`, for executing a block and restoring current state (last request, last response, and cookies) after the block (Jeremy Evans #316) * Make `Rack::Test::Methods` support `default_host` method similar to `app`, which will set the default host used for requests to the app (Jeremy Evans #317 #318) * Allow responses to set cookie paths not matching the current request URI. Such cookies will only be sent for paths matching the cookie path (Chris Waters #322) * Ignore leading dot for cookie domains, per RFC 6265 (Stephen Crosby #329) * Avoid creating empty multipart body if params is empty in `Rack::Test::Session#env_for` (Ryunosuke Sato #331) ## 2.0.2 / 2022-06-28 * Bug fixes: * Fix additional incompatible character encodings error when building uploaded bodies (Jeremy Evans #311) ## 2.0.1 / 2022-06-27 * Bug fixes: * Fix incompatible character encodings error when building uploaded file bodies (Jeremy Evans #308 #309) ## 2.0.0 / 2022-06-24 * Breaking changes: * Digest authentication support is now deprecated, as it relies on digest authentication support in rack, which has been deprecated (Jeremy Evans #294) * `Rack::Test::Utils.build_primitive_part` no longer handles array values (Jeremy Evans #292) * `Rack::Test::Utils` module methods other than `build_nested_query` and `build_multipart` are now private methods (Jeremy Evans #297) * `Rack::MockSession` has been combined into `Rack::Test::Session`, and remains as an alias to `Rack::Test::Session`, but to keep some backwards compatibility, `Rack::Test::Session.new` will accept a `Rack::Test::Session` instance and return it (Jeremy Evans #297) * Previously protected methods in `Rack::Test::Cookie{,Jar}` are now private methods (Jeremy Evans #297) * `Rack::Test::Methods` no longer defines `build_rack_mock_session`, but for backwards compatibility, `build_rack_test_session` will call `build_rack_mock_session` if it is defined (Jeremy Evans #297) * `Rack::Test::Methods::METHODS` is no longer defined (Jeremy Evans #297) * `Rack::Test::Methods#_current_session_names` has been removed (Jeremy Evans #297) * Headers used/accessed by rack-test are now lower case, for rack 3 compliance (Jeremy Evans #295) * Frozen literal strings are now used internally, which may break code that mutates static strings returned by rack-test, if any (Jeremy Evans #304) * Minor enhancements: * rack-test now works with the rack main branch (what will be rack 3) (Jeremy Evans #280 #292) * rack-test only loads the parts of rack it uses when running on the rack main branch (what will be rack 3) (Jeremy Evans #292) * Development dependencies have been significantly reduced, and are now a subset of the development dependencies of rack itself (Jeremy Evans #292) * Avoid creating multiple large copies of uploaded file data in memory (Jeremy Evans #286) * Specify HTTP/1.0 when submitting requests, to avoid responses with Transfer-Encoding: chunked (Jeremy Evans #288) * Support `:query_params` in rack environment for parameters that are appended to the query string instead of used in the request body (Jeremy Evans #150 #287) * Reduce required ruby version to 2.0, since tests run fine on Ruby 2.0 (Jeremy Evans #292) * Support :multipart env key for request methods to force multipart input (Jeremy Evans #303) * Force multipart input for request methods if content type starts with multipart (Jeremy Evans #303) * Improve performance of Utils.build_multipart by using an append-only design (Jeremy Evans #304) * Improve performance of Utils.build_nested_query for array values (Jeremy Evans #304) * Bug fixes: * The `CONTENT_TYPE` of multipart requests is now respected, if it starts with `multipart/` (Tom Knig #238) * Work correctly with responses that respond to `to_a` but not `to_ary` (Sergio Faria #276) * Raise an ArgumentError instead of a TypeError when providing a StringIO without an original filename when creating an UploadedFile (Nuno Correia #279) * Allow combining both an UploadedFile and a plain string when building a multipart upload (Mitsuhiro Shibuya #278) * Fix the generation of filenames with spaces to use path escaping instead of regular escaping, since path unescaping is used to decode it (Muir Manders, Jeremy Evans #275 #284) * Rewind tempfile used for multipart uploads before it is submitted to the application (Jeremy Evans, Alexander Dervish #261 #268 #286) * Fix Rack::Test.encoding_aware_strings to be true only on rack 1.6+ (Jeremy Evans #292) * Make Rack::Test::CookieJar#valid? return true/false (Jeremy Evans #292) * Cookies without a domain attribute no longer are submitted to requests for subdomains of that domain, for RFC 6265 compliance (Jeremy Evans #292) * Increase required rack version to 1.3, since tests fail on rack 1.2 and below (Jeremy Evans #293) ## 1.1.0 / 2018-07-21 * Breaking changes: * None * Minor enhancements / new functionality: * [GitHub] Added configuration for Stale (Per Lundberg #232) * follow_direct: Include rack.session.options (Mark Edmondson #233) * [CI] Add simplecov (fatkodima #227) * Bug fixes: * Follow relative locations correctly. (Samuel Williams #230) ## 1.0.0 / 2018-03-27 * Breaking changes: * Always set CONTENT_TYPE for non-GET requests (Per Lundberg #223) * Minor enhancements / bug fixes: * Create tempfile using the basename without extension (Edouard Chin #201) * Save `session` during `follow_redirect!` (Alexander Popov #218) * Document how to use URL params with DELETE method (Timur Platonov #220) ## 0.8.3 / 2018-02-27 * Bug fixes: * Do not set Content-Type if params are explicitly set to nil (Bartek Bułat #212). Fixes #200. * Fix `UploadedFile#new` regression (Per Lundberg #215) * Minor enhancements * [CI] Test against Ruby 2.5 (Nicolas Leger #217) ## 0.8.2 / 2017-11-21 * Bug fixes: * Bugfix for `UploadedFile.new` unintended API breakage. (Per Lundberg #210) ## 0.8.0 / 2017-11-20 * Known Issue * In `UploadedFile.new`, when passing e.g. a `Pathname` object, errors can be raised (eg. `ArgumentError: Missing original_filename for IO`, or `NoMethodError: undefined method 'size'`) See #207, #209. * Minor enhancements * Add a required_ruby_version of >= 2.2.2, similar to rack 2.0.1. (Samuel Giddins #194) * Remove new line from basic auth. (Felix Kleinschmidt #185) * Rubocop fixes (Per Lundberg #196) * Add how to install rack-test from github to README. (Jun Aruga #189) * Update CodeClimate badges (Toshimaru #195) * Add the ability to create Test::UploadedFile instances without the file system (Adam Milligan #149) * Add custom_request, remove duplication (Johannes Barre #184) * README.md: Added note about how to post JSON (Per Lundberg #198) * README.md: Added version badge (Per Lundberg #199) * Bug fixes * Bugfix for Cookies with multiple paths (Kyle Welsby #197) ## 0.7.0 / 2017-07-10 * Major enhancements * The project URL changed to https://github.com/rack-test/rack-test (Per Lundberg, Dennis Sivia, Jun Aruga) * Rack 2 compatible. (Trevor Wennblom #81, Vít Ondruch, Jun Aruga #151) * Minor enhancements * Port to RSpec 3. (Murahashi [Matt] Kenichi #70, Antonio Terceiro #134) * Add Travis CI (Johannes Barre #108, Jun Aruga #161) * Don't append an ampersand when params are empty (sbilharz, #157) * Allow symbol access to cookies (Anorlondo448 #156) * README: Added Travis badge (Olivier Lacan, Per Lundberg #146) * `Rack::Test::Utils#build_multipart`: Allow passing a third parameter to force multipart (Koen Punt #142) * Allow better testing of cookies (Stephen Best #133) * make `build_multipart` work without mixing in `Rack::Test::Utils` (Aaron Patterson #131) * Add license to gemspec (Jordi Massaguer Pla #72, Anatol Pomozov #89, Anatol Pomozov #90, Johannes Barre #109, Mandaryn #115, Chris Marshall #120, Robert Reiz #126, Nic Benders #127, Nic Benders #130) * Feature/bulk pr for readme updates (Patrick Mulder #65, Troels Knak-Nielsen #74, Jeff Casimir #76) * Switch README format to Markdown (Dennis Sivia #176) * Convert History.txt to Markdown (Dennis Sivia #179) * Stop generating gemspec file. (Jun Aruga #181) * Fix errors at rake docs and whitespace. (Jun Aruga #183) * Ensure Rack::Test::UploadedFile closes its tempfile file descriptor on GC (Michael de Silva #180) * Change codeclimate URL correctly. (Jun Aruga #186) * Bug fixes * Initialize digest_username before using it. (Guo Xiang Tan #116, John Drago #124, Mike Perham #154) * Do not set Content-Type for DELETE requests (David Celis #132) * Adds support for empty arrays in params. (Cedric Röck, Tim Masliuchenko #125) * Update README code example quotes to be consistent. (Dmitry Gritsay #112) * Update README not to recommend installing gem with sudo. (T.J. Schuck #87) * Set scheme when using ENV to enable SSL (Neil Ang #155) * Reuse request method and parameters on HTTP 307 redirect. (Martin Mauch #138) ## 0.6.3 / 2015-01-09 * Minor enhancements * Expose an env helper for persistently configuring the env as needed (Darío Javier Cravero #80) * Expose the tempfile of UploadedFile (Sytse Sijbrandij #67) * Bug fixes * Improve support for arrays of hashes in multipart forms (Murray Steele #69) * Improve test for query strings (Paul Grayson #66) ## 0.6.2 / 2012-09-27 * Minor enhancements * Support HTTP PATCH method (Marjan Krekoten' #33) * Preserve the exact query string when possible (Paul Grayson #63) * Add a #delete method to CookieJar (Paul Grayson #63) * Bug fixes * Fix HTTP Digest authentication when the URI has query params * Don't append default ports to HTTP_HOST (David Lee #57) ## 0.6.1 / 2011-07-27 * Bug fixes * Fix support for params with arrays in multipart forms (Joel Chippindale) * Add `respond_to?` to `Rack::Test::UploadedFile` to match `method_missing` (Josh Nichols) * Set the Referer header on requests issued by follow_redirect! (Ryan Bigg) ## 0.6.0 / 2011-05-03 * Bug fixes * Add support for HTTP OPTIONS verb (Paolo "Nusco" Perrotta) * Call #finish on MockResponses if it's available (Aaron Patterson) * Allow HTTP_HOST to be set via #header (Geoff Buesing) ## 0.5.7 / 2011-01-01 * Bug fixes * If no URI is present, include all cookies (Pratik Naik) ## 0.5.6 / 2010-09-25 * Bug fixes * Use parse_nested_query for parsing URI like Rack does (Eugene Bolshakov) * Don't depend on ActiveSupport extension to String (Bryan Helmkamp) * Do not overwrite HTTP_HOST if it is set (Krekoten' Marjan) ## 0.5.5 / 2010-09-22 * Bug fixes * Fix encoding of file uploads on Ruby 1.9 (Alan Kennedy) * Set env["HTTP_HOST"] when making requests (Istvan Hoka) ## 0.5.4 / 2010-05-26 * Bug fixes * Don't stomp on Content-Type's supplied via #header (Bryan Helmkamp) * Fixed build_multipart to allow for arrays of files (Louis Rose) * Don't raise an error if raw cookies contain a blank line (John Reilly) * Handle parameter names with brackets properly (Tanner Donovan) ## 0.5.3 / 2009-11-27 * Bug fixes * Fix cookie matching for subdomains (Marcin Kulik) ## 0.5.2 / 2009-11-13 * Bug fixes * Call close on response body after iteration, not before (Simon Rozet) * Add missing require for time in cookie_jar.rb (Jerry West) ## 0.5.1 / 2009-10-27 * Bug fixes * Escape cookie values (John Pignata) * Close the response body after each request, as per the Rack spec (Elomar França) ## 0.5.0 / 2009-09-19 * Bug fixes * Set HTTP_X_REQUESTED_WITH in the Rack env when a request is made with :xhr => true (Ben Sales) * Set headers in the Rack env in HTTP_USER_AGENT form * Rack::Test now generates no Ruby warnings ## 0.4.2 / 2009-09-01 * Minor enhancements * Merge in rack/master's build_multipart method which covers additional cases * Accept raw :params string input and merge it with the query string * Stringify and upcase request method (e.g. :post => "POST") (Josh Peek) * Bug fixes * Properly convert hashes with nil values (e.g. :foo => nil becomes simply "foo", not "foo=") * Prepend a slash to the URI path if it doesn't start with one (Josh Peek) * Requiring Rack-Test never modifies the Ruby load path anymore (Josh Peek) * Fixed using multiple cookies in a string on Ruby 1.8 (Tuomas Kareinen and Hermanni Hyytiälä) ## 0.4.1 / 2009-08-06 * Minor enhancements * Support initializing a `Rack::Test::Session` with an app in addition to a `Rack::MockSession` * Allow CONTENT_TYPE to be specified in the env and not overwritten when sending a POST or PUT ## 0.4.0 / 2009-06-25 * Minor enhancements * Expose hook for building `Rack::MockSessions` for frameworks that need to configure them before use * Support passing in arrays of raw cookies in addition to a newline separated string * Support after_request callbacks in MockSession for things like running background jobs * Allow multiple named sessions using with_session * Initialize `Rack::Test::Sessions` with `Rack::MockSessions` instead of apps. This change should help integration with other Ruby web frameworks (like Merb). * Support sending bodies for PUT requests (Larry Diehl) ## 0.3.0 / 2009-05-17 * Major enhancements * Ruby 1.9 compatible (Simon Rozet, Michael Fellinger) * Minor enhancements * Add `CookieJar#[]` and `CookieJar#[]=` methods * Make the default host configurable * Use `Rack::Lint` and fix errors (Simon Rozet) * Extract `Rack::MockSession` from `Rack::Test::Session` to handle tracking the last request and response and the cookie jar * Add #set_cookie and #clear_cookies methods * Rename #authorize to #basic_authorize (#authorize remains as an alias) (Simon Rozet) ## 0.2.0 / 2009-04-26 Because `#last_response` is now a `MockResponse` instead of a `Rack::Response`, `#last_response.body` now returns a string instead of an array. * Major enhancements * Support multipart requests via the UploadedFile class (thanks, Rails) * Minor enhancements * Updated for Rack 1.0 * Don't require rubygems (See http://gist.github.com/54177) * Support HTTP Digest authentication with the `#digest_authorize` method * `#last_response` returns a `MockResponse` instead of a Response (Michael Fellinger) ## 0.1.0 / 2009-03-02 * 1 major enhancement * Birthday! rack-test-2.1.0/MIT-LICENSE.txt000066400000000000000000000021311440412547600156650ustar00rootroot00000000000000Copyright (c) 2008-2009 Bryan Helmkamp, Engine Yard Inc. Copyright (c) 2022 Jeremy Evans Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rack-test-2.1.0/README.md000066400000000000000000000070571440412547600147060ustar00rootroot00000000000000# Rack::Test [![Gem Version](https://badge.fury.io/rb/rack-test.svg)](https://badge.fury.io/rb/rack-test) Code: https://github.com/rack/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 * Allows for submitting requests and testing responses * Maintains a cookie jar across requests * Supports request headers used for subsequent requests * Follow redirects when requested ## Examples These examples use `test/unit` but it's equally possible to use `rack-test` with other testing frameworks such as `minitest` or `rspec`. ```ruby require "test/unit" require "rack/test" require "json" class HomepageTest < Test::Unit::TestCase include Rack::Test::Methods def app lambda { |env| [200, {'content-type' => 'text/plain'}, ['All responses are OK']] } end def test_response_is_ok # Optionally set headers used for all requests in this spec: #header 'accept-charset', 'utf-8' # First argument is treated as the path get '/' assert last_response.ok? assert_equal 'All responses are OK', last_response.body end def delete_with_url_params_and_body # First argument can have a query string # # Second argument is used as the parameters for the request, which will be # included in the request body for non-GET requests. delete '/?foo=bar', JSON.generate('baz' => 'zot') end def post_with_json # Third argument is the rack environment to use for the request. The following # entries in the submitted rack environment are treated specially (in addition # to options supported by `Rack::MockRequest#env_for`: # # :cookie : Set a cookie for the current session before submitting the request. # # :query_params : Set parameters for the query string (as opposed to the body). # Value should be a hash of parameters. # # :xhr : Set HTTP_X_REQUESTED_WITH env key to XMLHttpRequest. post(uri, JSON.generate('baz' => 'zot'), 'CONTENT_TYPE' => 'application/json') end end ``` `rack-test` will test the app returned by the `app` method. If you are loading middleware in a `config.ru` file, and want to test that, you should load the Rack app created from the `config.ru` file: ```ruby 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 add to your `Gemfile`: ``` gem 'rack-test' ``` ## 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 ## Authors - Contributions from Bryan Helmkamp, Jeremy Evans, Simon Rozet, 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](MIT-LICENSE.txt). ## Supported platforms * Ruby 2.0+ * JRuby 9.1+ ## Releasing * Bump VERSION in lib/rack/test/version.rb * Ensure `History.md` is up-to-date, including correct version and date * `git commit . -m 'Release $VERSION'` * `git push` * `git tag -a -m 'Tag the $VERSION release' $VERSION` * `git push --tags` * `gem build rack-test.gemspec` * `gem push rack-test-$VERSION.gem` * Add a discussion post for the release rack-test-2.1.0/Rakefile000066400000000000000000000006511440412547600150650ustar00rootroot00000000000000task default: :spec desc "Run specs" task "spec" do sh "#{FileUtils::RUBY} -w spec/all.rb" end desc "Run specs with coverage" task "spec_cov" do ENV['COVERAGE'] = '1' sh "#{FileUtils::RUBY} -w spec/all.rb" ENV.delete('COVERAGE') end desc 'Generate RDoc' task :docs do FileUtils.rm_rf('doc') require_relative 'lib/rack/test/version' sh "rdoc --title 'Rack::Test #{Rack::Test::VERSION} API Documentation'" end rack-test-2.1.0/lib/000077500000000000000000000000001440412547600141645ustar00rootroot00000000000000rack-test-2.1.0/lib/rack/000077500000000000000000000000001440412547600151045ustar00rootroot00000000000000rack-test-2.1.0/lib/rack/test.rb000066400000000000000000000313751440412547600164210ustar00rootroot00000000000000# frozen_string_literal: true require 'uri' # :nocov: begin require "rack/version" rescue LoadError require "rack" else if Rack.release >= '2.3' require "rack/request" require "rack/mock" require "rack/utils" else require "rack" end end # :nocov: require 'forwardable' require_relative 'test/cookie_jar' require_relative 'test/utils' require_relative 'test/methods' require_relative 'test/uploaded_file' require_relative 'test/version' module Rack module Test # The default host to use for requests, when a full URI is not # provided. DEFAULT_HOST = 'example.org'.freeze # The default multipart boundary to use for multipart request bodies MULTIPART_BOUNDARY = '----------XnJLe9ZIbbGUYtzPQJ16u1'.freeze # The starting boundary in multipart requests START_BOUNDARY = "--#{MULTIPART_BOUNDARY}\r\n".freeze # The ending boundary in multipart requests END_BOUNDARY = "--#{MULTIPART_BOUNDARY}--\r\n".freeze # The common base class for exceptions raised by Rack::Test class Error < StandardError; end # Rack::Test::Session handles a series of requests issued to a Rack app. # It keeps track of the cookies for the session, and allows for setting headers # and a default rack environment that is used for future requests. # # Rack::Test::Session's methods are most often called through Rack::Test::Methods, # which will automatically build a session when it's first used. class Session extend Forwardable include Rack::Test::Utils def self.new(app, default_host = DEFAULT_HOST) # :nodoc: if app.is_a?(self) # Backwards compatibility for initializing with Rack::MockSession app else super end end # The Rack::Test::CookieJar for the cookies for the current session. attr_accessor :cookie_jar # The default host used for the session for when using paths for URIs. attr_reader :default_host # Creates a Rack::Test::Session for a given Rack app or Rack::Test::BasicSession. # # Note: Generally, you won't need to initialize a Rack::Test::Session directly. # Instead, you should include Rack::Test::Methods into your testing context. # (See README.rdoc for an example) # # The following methods are defined via metaprogramming: get, post, put, patch, # delete, options, and head. Each method submits a request with the given request # method, with the given URI and optional parameters and rack environment. # Examples: # # # URI only: # get("/") # GET / # get("/?foo=bar") # GET /?foo=bar # # # URI and parameters # get("/foo", 'bar'=>'baz') # GET /foo?bar=baz # post("/foo", 'bar'=>'baz') # POST /foo (bar=baz in request body) # # # URI, parameters, and rack environment # get("/bar", {}, 'CONTENT_TYPE'=>'foo') # get("/bar", {'foo'=>'baz'}, 'HTTP_ACCEPT'=>'*') # # The above methods as well as #request and #custom_request store the Rack::Request # submitted in #last_request. The methods store a Rack::MockResponse based on the # response in #last_response. #last_response is also returned by the methods. # If a block is given, #last_response is also yielded to the block. def initialize(app, default_host = DEFAULT_HOST) @env = {} @app = app @after_request = [] @default_host = default_host @last_request = nil @last_response = nil clear_cookies end %w[get post put patch delete options head].each do |method_name| class_eval(<<-END, __FILE__, __LINE__+1) def #{method_name}(uri, params = {}, env = {}, &block) custom_request('#{method_name.upcase}', uri, params, env, &block) end END end # Run a block after the each request completes. def after_request(&block) @after_request << block end # Replace the current cookie jar with an empty cookie jar. def clear_cookies @cookie_jar = CookieJar.new([], @default_host) end # Set a cookie in the current cookie jar. def set_cookie(cookie, uri = nil) cookie_jar.merge(cookie, uri) end # Return the last request issued in the session. Raises an error if no # requests have been sent yet. def last_request raise Error, 'No request yet. Request a page first.' unless @last_request @last_request end # Return the last response received in the session. Raises an error if # no requests have been sent yet. def last_response raise Error, 'No response yet. Request a page first.' unless @last_response @last_response end # Issue a request to the Rack app for the given URI and optional Rack # environment. Example: # # request "/" def request(uri, env = {}, &block) uri = parse_uri(uri, env) env = env_for(uri, env) process_request(uri, env, &block) end # Issue a request using the given HTTP verb for the given URI, with optional # params and rack environment. Example: # # custom_request "LINK", "/" def custom_request(verb, uri, params = {}, env = {}, &block) uri = parse_uri(uri, env) env = env_for(uri, env.merge(method: verb.to_s.upcase, params: params)) process_request(uri, env, &block) end # Set a header to be included on all subsequent requests through the # session. Use a value of nil to remove a previously configured header. # # In accordance with the Rack spec, headers will be included in the Rack # environment hash in HTTP_USER_AGENT form. Example: # # header "user-agent", "Firefox" def header(name, value) name = name.upcase name.tr!('-', '_') name = "HTTP_#{name}" unless name == 'CONTENT_TYPE' || name == 'CONTENT_LENGTH' env(name, value) end # Set an entry in the rack environment to be included on all subsequent # requests through the session. Use a value of nil to remove a previously # value. Example: # # env "rack.session", {:csrf => 'token'} def env(name, value) if value.nil? @env.delete(name) else @env[name] = value end end # Set the username and password for HTTP Basic authorization, to be # included in subsequent requests in the HTTP_AUTHORIZATION header. # # Example: # basic_authorize "bryan", "secret" def basic_authorize(username, password) encoded_login = ["#{username}:#{password}"].pack('m0') header('Authorization', "Basic #{encoded_login}") end alias authorize basic_authorize # Rack::Test will not follow any redirects automatically. This method # will follow the redirect returned (including setting the Referer header # on the new request) in the last response. If the last response was not # a redirect, an error will be raised. def follow_redirect! unless last_response.redirect? raise Error, 'Last response was not a redirect. Cannot follow_redirect!' end if last_response.status == 307 request_method = last_request.request_method params = last_request.params else request_method = 'GET' params = {} end # Compute the next location by appending the location header with the # last request, as per https://tools.ietf.org/html/rfc7231#section-7.1.2 # Adding two absolute locations returns the right-hand location next_location = URI.parse(last_request.url) + URI.parse(last_response['Location']) custom_request( request_method, next_location.to_s, params, 'HTTP_REFERER' => last_request.url, 'rack.session' => last_request.session, 'rack.session.options' => last_request.session_options ) end # Yield to the block, and restore the last request, last response, and # cookie jar to the state they were prior to block execution upon # exiting the block. def restore_state request = @last_request response = @last_response cookie_jar = @cookie_jar.dup after_request = @after_request.dup begin yield ensure @last_request = request @last_response = response @cookie_jar = cookie_jar @after_request = after_request end end private # :nocov: if !defined?(Rack::RELEASE) || Gem::Version.new(Rack::RELEASE) < Gem::Version.new('2.2.2') def close_body(body) body.close if body.respond_to?(:close) end # :nocov: else # close() gets called automatically in newer Rack versions. def close_body(body) end end # Normalize URI based on given URI/path and environment. def parse_uri(path, env) uri = URI.parse(path) uri.path = "/#{uri.path}" unless uri.path.start_with?('/') uri.host ||= @default_host uri.scheme ||= 'https' if env['HTTPS'] == 'on' uri end DEFAULT_ENV = { 'rack.test' => true, 'REMOTE_ADDR' => '127.0.0.1', 'SERVER_PROTOCOL' => 'HTTP/1.0', } # :nocov: unless Rack.release >= '2.3' DEFAULT_ENV['HTTP_VERSION'] = DEFAULT_ENV['SERVER_PROTOCOL'] end # :nocov: DEFAULT_ENV.freeze private_constant :DEFAULT_ENV # Update environment to use based on given URI. def env_for(uri, env) env = DEFAULT_ENV.merge(@env).merge!(env) env['HTTP_HOST'] ||= [uri.host, (uri.port if uri.port != uri.default_port)].compact.join(':') env['HTTPS'] = 'on' if URI::HTTPS === uri env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' if env[:xhr] env['REQUEST_METHOD'] ||= env[:method] ? env[:method].to_s.upcase : 'GET' params = env.delete(:params) query_array = [uri.query] if env['REQUEST_METHOD'] == 'GET' # Treat params as query params if params append_query_params(query_array, params) end elsif !env.key?(:input) env['CONTENT_TYPE'] ||= 'application/x-www-form-urlencoded' params ||= {} multipart = env.has_key?(:multipart) ? env.delete(:multipart) : env['CONTENT_TYPE'].start_with?('multipart/') if params.is_a?(Hash) if !params.empty? && data = build_multipart(params, false, multipart) env[:input] = data env['CONTENT_LENGTH'] ||= data.length.to_s env['CONTENT_TYPE'] = "#{multipart_content_type(env)}; boundary=#{MULTIPART_BOUNDARY}" else env[:input] = build_nested_query(params) end else env[:input] = params end end if query_params = env.delete(:query_params) append_query_params(query_array, query_params) end query_array.compact! query_array.reject!(&:empty?) uri.query = query_array.join('&') set_cookie(env.delete(:cookie), uri) if env.key?(:cookie) Rack::MockRequest.env_for(uri.to_s, env) end # Append a string version of the query params to the array of query params. def append_query_params(query_array, query_params) query_params = parse_nested_query(query_params) if query_params.is_a?(String) query_array << build_nested_query(query_params) end # Return the multipart content type to use based on the environment. def multipart_content_type(env) requested_content_type = env['CONTENT_TYPE'] if requested_content_type.start_with?('multipart/') requested_content_type else 'multipart/form-data' end end # Submit the request with the given URI and rack environment to # the mock session. Returns and potentially yields the last response. def process_request(uri, env) env['HTTP_COOKIE'] ||= cookie_jar.for(uri) @last_request = Rack::Request.new(env) status, headers, body = @app.call(env).to_a @last_response = MockResponse.new(status, headers, body, env['rack.errors'].flush) close_body(body) cookie_jar.merge(last_response.headers['set-cookie'], uri) @after_request.each(&:call) @last_response.finish yield @last_response if block_given? @last_response end end # Whether the version of rack in use handles encodings. def self.encoding_aware_strings? Rack.release >= '1.6' end end # For backwards compatibility with 1.1.0 and below MockSession = Test::Session end rack-test-2.1.0/lib/rack/test/000077500000000000000000000000001440412547600160635ustar00rootroot00000000000000rack-test-2.1.0/lib/rack/test/cookie_jar.rb000066400000000000000000000163651440412547600205300ustar00rootroot00000000000000# frozen_string_literal: true require 'uri' require 'time' module Rack module Test # Represents individual cookies in the cookie jar. This is considered private # API and behavior of this class can change at any time. class Cookie # :nodoc: include Rack::Utils # The name of the cookie, will be a string attr_reader :name # The value of the cookie, will be a string or nil if there is no value. attr_reader :value # The raw string for the cookie, without options. Will generally be in # name=value format is name and value are provided. attr_reader :raw def initialize(raw, uri = nil, default_host = DEFAULT_HOST) @default_host = default_host uri ||= default_uri # separate the name / value pair from the cookie options @raw, options = raw.split(/[;,] */n, 2) @name, @value = parse_query(@raw, ';').to_a.first @options = parse_query(options, ';') if domain = @options['domain'] @exact_domain_match = false domain[0] = '' if domain[0] == '.' else # If the domain attribute is not present in the cookie, # the domain must match exactly. @exact_domain_match = true @options['domain'] = (uri.host || default_host) end # Set the path for the cookie to the directory containing # the request if it isn't set. @options['path'] ||= uri.path.sub(/\/[^\/]*\Z/, '') end # Wether the given cookie can replace the current cookie in the cookie jar. def replaces?(other) [name.downcase, domain, path] == [other.name.downcase, other.domain, other.path] end # Whether the cookie has a value. def empty? @value.nil? || @value.empty? end # The explicit or implicit domain for the cookie. def domain @options['domain'] end # Whether the cookie has the secure flag, indicating it can only be sent over # an encrypted connection. def secure? @options.key?('secure') end # Whether the cookie has the httponly flag, indicating it is not available via # a javascript API. def http_only? @options.key?('HttpOnly') || @options.key?('httponly') end # The explicit or implicit path for the cookie. def path ([*@options['path']].first.split(',').first || '/').strip end # A Time value for when the cookie expires, if the expires option is set. def expires Time.parse(@options['expires']) if @options['expires'] end # Whether the cookie is currently expired. def expired? expires && expires < Time.now end # Whether the cookie is valid for the given URI. def valid?(uri) uri ||= default_uri uri.host = @default_host if uri.host.nil? real_domain = domain =~ /^\./ ? domain[1..-1] : domain !!((!secure? || (secure? && uri.scheme == 'https')) && uri.host =~ Regexp.new("#{'^' if @exact_domain_match}#{Regexp.escape(real_domain)}$", Regexp::IGNORECASE)) end # Cookies that do not match the URI will not be sent in requests to the URI. def matches?(uri) !expired? && valid?(uri) && uri.path.start_with?(path) end # Order cookies by name, path, and domain. def <=>(other) [name, path, domain.reverse] <=> [other.name, other.path, other.domain.reverse] end # A hash of cookie options, including the cookie value, but excluding the cookie name. def to_h @options.merge( 'value' => @value, 'HttpOnly' => http_only?, 'secure' => secure? ) end alias to_hash to_h private # The default URI to use for the cookie, including just the host. def default_uri URI.parse('//' + @default_host + '/') end end # Represents all cookies for a session, handling adding and # removing cookies, and finding which cookies apply to a given # request. This is considered private API and behavior of this # class can change at any time. class CookieJar # :nodoc: DELIMITER = '; '.freeze def initialize(cookies = [], default_host = DEFAULT_HOST) @default_host = default_host @cookies = cookies.sort! end # Ensure the copy uses a distinct cookies array. def initialize_copy(other) super @cookies = @cookies.dup end # Return the value for first cookie with the given name, or nil # if no such cookie exists. def [](name) name = name.to_s @cookies.each do |cookie| return cookie.value if cookie.name == name end nil end # Set a cookie with the given name and value in the # cookie jar. def []=(name, value) merge("#{name}=#{Rack::Utils.escape(value)}") end # Return the first cookie with the given name, or nil if # no such cookie exists. def get_cookie(name) @cookies.each do |cookie| return cookie if cookie.name == name end nil end # Delete all cookies with the given name from the cookie jar. def delete(name) @cookies.reject! do |cookie| cookie.name == name end nil end # Add a string of raw cookie information to the cookie jar, # if the cookie is valid for the given URI. # Cookies should be separated with a newline. def merge(raw_cookies, uri = nil) return unless raw_cookies if raw_cookies.is_a? String raw_cookies = raw_cookies.split("\n") raw_cookies.reject!(&:empty?) end raw_cookies.each do |raw_cookie| cookie = Cookie.new(raw_cookie, uri, @default_host) self << cookie if cookie.valid?(uri) end end # Add a Cookie to the cookie jar. def <<(new_cookie) @cookies.reject! do |existing_cookie| new_cookie.replaces?(existing_cookie) end @cookies << new_cookie @cookies.sort! end # Return a raw cookie string for the cookie header to # use for the given URI. def for(uri) buf = String.new delimiter = nil each_cookie_for(uri) do |cookie| if delimiter buf << delimiter else delimiter = DELIMITER end buf << cookie.raw end buf end # Return a hash cookie names and cookie values for cookies in the jar. def to_hash cookies = {} @cookies.each do |cookie| cookies[cookie.name] = cookie.value end cookies end private # Yield each cookie that matches for the URI. # # The cookies are sorted by most specific first. So, we loop through # all the cookies in order and add it to a hash by cookie name if # the cookie can be sent to the current URI. It's added to the hash # so that when we are done, the cookies will be unique by name and # we'll have grabbed the most specific to the URI. def each_cookie_for(uri) @cookies.each do |cookie| yield cookie if !uri || cookie.matches?(uri) end end end end end rack-test-2.1.0/lib/rack/test/methods.rb000066400000000000000000000056021440412547600200560ustar00rootroot00000000000000# frozen_string_literal: true require 'forwardable' module Rack module Test # This module serves as the primary integration point for using Rack::Test # in a testing environment. It depends on an app method being defined in the # same context, and provides the Rack::Test API methods (see Rack::Test::Session # for their documentation). It defines the following methods that are delegated # to the current session: :request, :get, :post, :put, :patch, :delete, :options, # :head, :custom_request, :follow_redirect!, :header, :env, :set_cookie, # :clear_cookies, :authorize, :basic_authorize, :last_response, and :last_request. # # Example: # # class HomepageTest < Test::Unit::TestCase # include Rack::Test::Methods # # def app # MyApp # end # end module Methods extend Forwardable # Return the existing session with the given name, or a new # rack session. Always use a new session if name is nil. def rack_test_session(name = :default) # :nodoc: return build_rack_test_session(name) unless name @_rack_test_sessions ||= {} @_rack_test_sessions[name] ||= build_rack_test_session(name) end # For backwards compatibility with older rack-test versions. alias rack_mock_session rack_test_session # :nodoc: # Create a new Rack::Test::Session for #app. def build_rack_test_session(_name) # :nodoc: if respond_to?(:build_rack_mock_session, true) # Backwards compatibility for capybara build_rack_mock_session else if respond_to?(:default_host) Session.new(app, default_host) else Session.new(app) end end end # Return the currently actively session. This is the session to # which the delegated methods are sent. def current_session @_rack_test_current_session ||= rack_test_session end # Create a new session (or reuse an existing session with the given name), # and make it the current session for the given block. def with_session(name) session = _rack_test_current_session yield(@_rack_test_current_session = rack_test_session(name)) ensure @_rack_test_current_session = session end def_delegators(:current_session, :request, :get, :post, :put, :patch, :delete, :options, :head, :custom_request, :follow_redirect!, :header, :env, :set_cookie, :clear_cookies, :authorize, :basic_authorize, :last_response, :last_request, ) # Private accessor to avoid uninitialized instance variable warning in Ruby 2.* attr_accessor :_rack_test_current_session private :_rack_test_current_session end end end rack-test-2.1.0/lib/rack/test/uploaded_file.rb000066400000000000000000000071621440412547600212120ustar00rootroot00000000000000# frozen_string_literal: true require 'fileutils' require 'tempfile' require 'stringio' module Rack module Test # Wraps a Tempfile with a content type. Including one or more UploadedFile's # in the params causes Rack::Test to build and issue a multipart request. # # Example: # post "/photos", "file" => Rack::Test::UploadedFile.new("me.jpg", "image/jpeg") class UploadedFile # The filename, *not* including the path, of the "uploaded" file attr_reader :original_filename # The tempfile attr_reader :tempfile # The content type of the "uploaded" file attr_accessor :content_type # Creates a new UploadedFile instance. # # Arguments: # content :: is a path to a file, or an {IO} or {StringIO} object representing the content. # content_type :: MIME type of the file # binary :: Whether the file should be set to binmode (content treated as binary). # original_filename :: The filename to use for the file. Required if content is StringIO, optional override if not def initialize(content, content_type = 'text/plain', binary = false, original_filename: nil) @content_type = content_type @original_filename = original_filename case content when StringIO initialize_from_stringio(content) else initialize_from_file_path(content) end @tempfile.binmode if binary end # The path to the tempfile. Will not work if the receiver's content is from a StringIO. def path tempfile.path end alias local_path path # Delegate all methods not handled to the tempfile. def method_missing(method_name, *args, &block) tempfile.public_send(method_name, *args, &block) end # Append to given buffer in 64K chunks to avoid multiple large # copies of file data in memory. Rewind tempfile before and # after to make sure all data in tempfile is appended to the # buffer. def append_to(buffer) tempfile.rewind buf = String.new buffer << tempfile.readpartial(65_536, buf) until tempfile.eof? tempfile.rewind nil end def respond_to_missing?(method_name, include_private = false) #:nodoc: tempfile.respond_to?(method_name, include_private) || super end # A proc that can be used as a finalizer to close and unlink the tempfile. def self.finalize(file) proc { actually_finalize file } end # Close and unlink the given file, used as a finalizer for the tempfile, # if the tempfile is backed by a file in the filesystem. def self.actually_finalize(file) file.close file.unlink end private # Use the StringIO as the tempfile. def initialize_from_stringio(stringio) raise(ArgumentError, 'Missing `original_filename` for StringIO object') unless @original_filename @tempfile = stringio end # Create a tempfile and copy the content from the given path into the tempfile, optionally renaming if # original_filename has been set. def initialize_from_file_path(path) raise "#{path} file does not exist" unless ::File.exist?(path) @original_filename ||= ::File.basename(path) extension = ::File.extname(@original_filename) @tempfile = Tempfile.new([::File.basename(@original_filename, extension), extension]) @tempfile.set_encoding(Encoding::BINARY) ObjectSpace.define_finalizer(self, self.class.finalize(@tempfile)) FileUtils.copy_file(path, @tempfile.path) end end end end rack-test-2.1.0/lib/rack/test/utils.rb000066400000000000000000000111541440412547600175520ustar00rootroot00000000000000# frozen_string_literal: true module Rack module Test module Utils # :nodoc: include Rack::Utils extend self # Build a query string for the given value and prefix. The value # can be an array or hash of parameters. def build_nested_query(value, prefix = nil) case value when Array if value.empty? "#{prefix}[]=" else prefix += "[]" unless unescape(prefix).end_with?('[]') value.map do |v| build_nested_query(v, prefix.to_s) end.join('&') end when Hash value.map do |k, v| build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k)) end.join('&') when NilClass prefix.to_s else "#{prefix}=#{escape(value)}" end end # Build a multipart body for the given params. def build_multipart(params, _first = true, multipart = false) raise ArgumentError, 'value must be a Hash' unless params.is_a?(Hash) unless multipart query = lambda { |value| case value when Array value.each(&query) when Hash value.values.each(&query) when UploadedFile multipart = true end } params.values.each(&query) return nil unless multipart end params = normalize_multipart_params(params, true) buffer = String.new build_parts(buffer, params) buffer end private # Return a flattened hash of parameter values based on the given params. def normalize_multipart_params(params, first=false) flattened_params = {} params.each do |key, value| k = first ? key.to_s : "[#{key}]" case value when Array value.map do |v| if v.is_a?(Hash) nested_params = {} normalize_multipart_params(v).each do |subkey, subvalue| nested_params[subkey] = subvalue end (flattened_params["#{k}[]"] ||= []) << nested_params else flattened_params["#{k}[]"] = value end end when Hash normalize_multipart_params(value).each do |subkey, subvalue| flattened_params[k + subkey] = subvalue end else flattened_params[k] = value end end flattened_params end # Build the multipart content for uploading. def build_parts(buffer, parameters) _build_parts(buffer, parameters) buffer << END_BOUNDARY end # Append each multipart parameter value to the buffer. def _build_parts(buffer, parameters) parameters.map do |name, value| if name =~ /\[\]\Z/ && value.is_a?(Array) && value.all? { |v| v.is_a?(Hash) } value.each do |hash| new_value = {} hash.each { |k, v| new_value[name + k] = v } _build_parts(buffer, new_value) end else [value].flatten.map do |v| if v.respond_to?(:original_filename) build_file_part(buffer, name, v) else build_primitive_part(buffer, name, v) end end end end end # Append the multipart fragment for a parameter that isn't a file upload to the buffer. def build_primitive_part(buffer, parameter_name, value) buffer << START_BOUNDARY << "content-disposition: form-data; name=\"" << parameter_name.to_s.b << "\"\r\n\r\n" << value.to_s.b << "\r\n" buffer end # Append the multipart fragment for a parameter that is a file upload to the buffer. def build_file_part(buffer, parameter_name, uploaded_file) buffer << START_BOUNDARY << "content-disposition: form-data; name=\"" << parameter_name.to_s.b << "\"; filename=\"" << escape_path(uploaded_file.original_filename).b << "\"\r\ncontent-type: " << uploaded_file.content_type.to_s.b << "\r\ncontent-length: " << uploaded_file.size.to_s.b << "\r\n\r\n" # Handle old versions of Capybara::RackTest::Form::NilUploadedFile if uploaded_file.respond_to?(:set_encoding) uploaded_file.set_encoding(Encoding::BINARY) uploaded_file.append_to(buffer) end buffer << "\r\n" end end end end rack-test-2.1.0/lib/rack/test/version.rb000066400000000000000000000001011440412547600200650ustar00rootroot00000000000000module Rack module Test VERSION = '2.1.0'.freeze end end rack-test-2.1.0/rack-test.gemspec000066400000000000000000000023711440412547600166630ustar00rootroot00000000000000require_relative 'lib/rack/test/version' Gem::Specification.new do |s| s.name = 'rack-test' s.version = Rack::Test::VERSION s.platform = Gem::Platform::RUBY s.author = ['Jeremy Evans', 'Bryan Helmkamp'] s.email = ['code@jeremyevans.net', 'bryan@brynary.com'] s.license = 'MIT' s.homepage = 'https://github.com/rack/rack-test' s.summary = 'Simple testing API built on Rack' s.description = <<-EOS.strip 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. EOS s.metadata = { 'source_code_uri' => 'https://github.com/rack/rack-test', 'bug_tracker_uri' => 'https://github.com/rack/rack-test/issues', 'mailing_list_uri' => 'https://github.com/rack/rack-test/discussions', 'changelog_uri' => 'https://github.com/rack/rack-test/blob/main/History.md', } s.require_paths = ['lib'] s.files = `git ls-files -- lib/*`.split("\n") + %w[History.md MIT-LICENSE.txt README.md] s.required_ruby_version = '>= 2.0' s.add_dependency 'rack', '>= 1.3' s.add_development_dependency 'rake' s.add_development_dependency 'minitest', ">= 5.0" s.add_development_dependency 'minitest-global_expectations' end rack-test-2.1.0/spec/000077500000000000000000000000001440412547600143505ustar00rootroot00000000000000rack-test-2.1.0/spec/all.rb000066400000000000000000000001071440412547600154430ustar00rootroot00000000000000Dir['spec/**/*_spec.rb'].each{|f| require_relative f.sub('spec/', '')} rack-test-2.1.0/spec/fixtures/000077500000000000000000000000001440412547600162215ustar00rootroot00000000000000rack-test-2.1.0/spec/fixtures/bar.txt000066400000000000000000000000041440412547600175200ustar00rootroot00000000000000baz rack-test-2.1.0/spec/fixtures/config.ru000066400000000000000000000000541440412547600200350ustar00rootroot00000000000000require 'fake_app' run Rack::Test::FakeApp rack-test-2.1.0/spec/fixtures/fake_app.rb000066400000000000000000000116771440412547600203300ustar00rootroot00000000000000require 'time' require 'rack/lint' module Rack module Test class FakeApp def call(env) _, h, b = res = handle(env) length = 0 b.each{|s| length += s.bytesize} h['content-length'] = length.to_s h['content-type'] = 'text/html;charset=utf-8' res end private def new_cookie_count(req) old_value = req.cookies['count'].to_i || 0 (old_value + 1).to_s end def handle(env) method = env['REQUEST_METHOD'] path = env['PATH_INFO'] req = Rack::Request.new(env) params = req.params session = env['rack.session'] if path == '/' case method when 'HEAD', 'OPTIONS' return [200, {}, []] else return [200, {}, ["Hello, #{method}: #{params.inspect}"]] end end if path == '/redirect' && method == 'GET' return [301, { 'location' => '/redirected' }, []] end if path == '/nested/redirect' && method == 'GET' return [301, { 'location' => 'redirected' }, []] end if path == '/nested/redirected' && method == 'GET' return [200, {}, ['Hello World!']] end if path == '/absolute/redirect' && method == 'GET' return [301, { 'location' => 'https://www.google.com' }, []] end if path == '/redirect' && method == 'POST' if params['status'] return [Integer(params['status']), { 'location' => '/redirected' }, []] else return [302, { 'location' => '/redirected' }, []] end end if path == '/redirect-with-cookie' && method == 'GET' return [302, { 'set-cookie' => "value=1; path=/cookies;", 'location' => '/cookies/show' }, []] end if path == '/redirected' additional_info = if method == 'GET' ", session #{session.inspect} with options #{env['rack.session.options'].inspect}" else " using #{method.downcase} with #{params}" end return [200, {}, ["You've been redirected" + additional_info]] end if path == '/void' && method == 'GET' return [200, {}, []] end if %w[/cookies/show /COOKIES/show /not-cookies/show /cookies/default-path /cookies/default-path/sub].include?(path) && method == 'GET' return [200, {}, [req.cookies.inspect]] end if path == '/cookies/set-secure' && method == 'GET' return [200, { 'set-cookie' => "secure-cookie=#{params['value'] || raise}; secure" }, ['Set']] end if (path == '/cookies/set-simple' && method == 'GET') || (path == '/cookies/default-path' && method == 'POST') return [200, { 'set-cookie' => "simple=#{params['value'] || raise};" }, ['Set']] end if path == '/cookies/delete' && method == 'GET' return [200, { 'set-cookie' => "value=; expires=#{Time.at(0).httpdate}" }, []] end if path == '/cookies/count' && method == 'GET' new_value = new_cookie_count(req) return [200, { 'set-cookie' => "count=#{new_value};" }, [new_value]] end if path == '/cookies/set' && method == 'GET' return [200, { 'set-cookie' => "value=#{params['value'] || raise}; path=/cookies; expires=#{(Time.now+10).httpdate}" }, ['Set']] end if path == '/cookies/domain' && method == 'GET' new_value = new_cookie_count(req) return [200, { 'set-cookie' => "count=#{new_value}; domain=localhost.com" }, [new_value]] end if path == '/cookies/subdomain' && method == 'GET' new_value = new_cookie_count(req) return [200, { 'set-cookie' => "count=#{new_value}; domain=.example.org" }, [new_value]] end if path == '/cookies/set-uppercase' && method == 'GET' return [200, { 'set-cookie' => "VALUE=#{params['value'] || raise}; path=/cookies; expires=#{(Time.now+10).httpdate}" }, ['Set']] end if path == '/cookies/set-multiple' && method == 'GET' value = Rack.release >= '2.3' ? ["key1=value1", "key2=value2"] : "key1=value1\nkey2=value2" return [200, { 'set-cookie' => value }, ['Set']] end [404, {}, []] end end class InputRewinder def initialize(app) @app = app end def call(env) # Rack 3 removes the requirement for rewindable input. # Rack::Lint wraps the input and disallows direct access to rewind. # This breaks a lot of the specs that access last_request and # try to read the input. Work around this by reassigning the input # in env after the request, and rewinding it. input = env['rack.input'] @app.call(env) ensure if input input.rewind env['rack.input'] = input end end end FAKE_APP = InputRewinder.new(Rack::Lint.new(FakeApp.new.freeze)) end end rack-test-2.1.0/spec/fixtures/foo.txt000066400000000000000000000000041440412547600175370ustar00rootroot00000000000000bar rack-test-2.1.0/spec/fixtures/mb.txt000066400000000000000000000000031440412547600173510ustar00rootroot00000000000000⍅rack-test-2.1.0/spec/fixtures/space case.txt000066400000000000000000000000041440412547600207430ustar00rootroot00000000000000bar rack-test-2.1.0/spec/rack/000077500000000000000000000000001440412547600152705ustar00rootroot00000000000000rack-test-2.1.0/spec/rack/test/000077500000000000000000000000001440412547600162475ustar00rootroot00000000000000rack-test-2.1.0/spec/rack/test/cookie_jar_spec.rb000066400000000000000000000037541440412547600217240ustar00rootroot00000000000000# frozen-string-literal: true require_relative '../../spec_helper' describe Rack::Test::CookieJar do cookie_value = 'foo;abc'.freeze cookie_name = 'a_cookie_name'.freeze it 'copies should not share a cookie jar' do jar = Rack::Test::CookieJar.new jar_dup = jar.dup jar_clone = jar.clone jar['a'] = 'b' jar.to_hash.must_equal 'a' => 'b' jar_dup.to_hash.must_be_empty jar_clone.to_hash.must_be_empty end it 'ignores leading dot in domain' do jar = Rack::Test::CookieJar.new jar << Rack::Test::Cookie.new('a=c; domain=.lithostech.com', URI('https://lithostech.com')) jar.get_cookie('a').domain.must_equal 'lithostech.com' end it '#[] and []= should get and set cookie values' do jar = Rack::Test::CookieJar.new jar[cookie_name].must_be_nil jar[cookie_name] = cookie_value jar[cookie_name].must_equal cookie_value jar[cookie_name+'a'].must_be_nil end it '#get_cookie with a populated jar returns full cookie objects' do jar = Rack::Test::CookieJar.new jar.get_cookie(cookie_name).must_be_nil jar[cookie_name] = cookie_value jar.get_cookie(cookie_name).must_be_kind_of Rack::Test::Cookie jar.get_cookie(cookie_name+'a').must_be_nil end it '#for returns the cookie header string delimited by semicolon and a space' do jar = Rack::Test::CookieJar.new jar['a'] = 'b' jar['c'] = 'd' jar.for(nil).must_equal 'a=b; c=d' end it '#to_hash returns a hash of cookies' do jar = Rack::Test::CookieJar.new jar['a'] = 'b' jar['c'] = 'd' jar.to_hash.must_equal 'a' => 'b', 'c' => 'd' end it '#merge merges valid raw cookie strings' do jar = Rack::Test::CookieJar.new jar['a'] = 'b' jar.merge('c=d') jar.to_hash.must_equal 'a' => 'b', 'c' => 'd' end it '#merge does not merge invalid raw cookie strings' do jar = Rack::Test::CookieJar.new jar['a'] = 'b' jar.merge('c=d; domain=example.org; secure', URI.parse('/')) jar.to_hash.must_equal 'a' => 'b' end end rack-test-2.1.0/spec/rack/test/cookie_object_spec.rb000066400000000000000000000041401440412547600224040ustar00rootroot00000000000000# frozen-string-literal: true require_relative '../../spec_helper' require 'cgi' describe Rack::Test::Cookie do value = 'the cookie value'.freeze domain = 'www.example.org'.freeze path = '/foo'.freeze expires = (Time.now + (24 * 60 * 60)).httpdate cookie_string = [ 'cookie_name=' + CGI.escape(value), 'domain=' + domain, 'path=' + path, 'expires=' + expires ].join(Rack::Test::CookieJar::DELIMITER).freeze define_method(:cookie) do |trailer=''| Rack::Test::Cookie.new(cookie_string + trailer) end it '#to_h returns the cookie value and all options' do cookie('; HttpOnly; secure').to_h.must_equal( 'value' => value, 'domain' => domain, 'path' => path, 'expires' => expires, 'HttpOnly' => true, 'secure' => true ) end it '#to_hash is an alias for #to_h' do cookie.to_hash.must_equal cookie.to_h end it '#empty? should only be true for empty values' do cookie.empty?.must_equal false Rack::Test::Cookie.new('value=').empty?.must_equal true end it '#valid? should consider the given URI scheme for secure cookies' do cookie('; secure').valid?(URI.parse('https://www.example.org/')).must_equal true cookie('; secure').valid?(URI.parse('httpx://www.example.org/')).must_equal false cookie('; secure').valid?(URI.parse('/')).must_equal false end it '#valid? is indifferent to matching paths' do cookie.valid?(URI.parse('https://www.example.org/foo')).must_equal true cookie.valid?(URI.parse('https://www.example.org/bar')).must_equal true end it '#matches? demands matching paths' do cookie.matches?(URI.parse('https://www.example.org/foo')).must_equal true cookie.matches?(URI.parse('https://www.example.org/bar')).must_equal false end it '#http_only? for a non HTTP only cookie returns false' do cookie.http_only?.must_equal false end it '#http_only? for an HTTP only cookie returns true' do cookie('; HttpOnly').http_only?.must_equal true end it '#http_only? for an HTTP only cookie returns true' do cookie('; httponly').http_only?.must_equal true end end rack-test-2.1.0/spec/rack/test/cookie_spec.rb000066400000000000000000000202461440412547600210630ustar00rootroot00000000000000# frozen-string-literal: true require_relative '../../spec_helper' describe "Rack::Test::Session" do it 'keeps a cookie jar' do get '/cookies/show' last_request.cookies.must_equal({}) get '/cookies/set', 'value' => '1' get '/cookies/show' last_request.cookies.must_equal 'value' => '1' end it "doesn't send expired cookies" do get '/cookies/set', 'value' => '1' cookie = rack_mock_session.cookie_jar.instance_variable_get(:@cookies).first def cookie.expired?; true end get '/cookies/show' last_request.cookies.must_equal({}) end it 'cookie path defaults to the directory of the document that was requested' do post '/cookies/default-path', 'value' => 'cookie' get '/cookies/default-path' last_request.cookies.must_equal 'simple' => 'cookie' get '/cookies/default-path/sub' last_request.cookies.must_equal 'simple' => 'cookie' get '/' last_request.cookies.must_equal({}) get '/COOKIES/show' last_request.cookies.must_equal({}) end it 'uses the first "path" when multiple paths are defined' do cookie_string = [ '/', 'csrf_id=ABC123', 'path=/, _github_ses=ABC123', 'path=/', 'expires=Wed, 01 Jan 2020 08:00:00 GMT', 'HttpOnly' ].join(Rack::Test::CookieJar::DELIMITER) cookie = Rack::Test::Cookie.new(cookie_string) cookie.path.must_equal '/' end it 'uses the single "path" when only one path is defined' do cookie_string = [ '/', 'csrf_id=ABC123', 'path=/', 'expires=Wed, 01 Jan 2020 08:00:00 GMT', 'HttpOnly' ].join(Rack::Test::CookieJar::DELIMITER) cookie = Rack::Test::Cookie.new(cookie_string) cookie.path.must_equal '/' end it 'escapes cookie values' do jar = Rack::Test::CookieJar.new jar['value'] = 'foo;abc' # Looks like it is not escaping, but actually escapes and unescapes, # otherwise abc would be treated as an attribute and not part of the value. jar['value'].must_equal 'foo;abc' end it 'deletes cookies directly from the CookieJar' do jar = Rack::Test::CookieJar.new jar['abcd'] = '1234' jar['abcd'].must_equal '1234' jar.delete('abcd') jar['abcd'].must_be_nil end it 'allow symbol access' do jar = Rack::Test::CookieJar.new jar['value'] = 'foo;abc' jar[:value].must_equal 'foo;abc' end it "doesn't send cookies with the wrong domain" do get 'http://www.example.com/cookies/set', 'value' => '1' get 'http://www.other.example/cookies/show' last_request.cookies.must_equal({}) end it "doesn't send cookies with the wrong path" do get '/cookies/set', 'value' => '1' get '/not-cookies/show' last_request.cookies.must_equal({}) end it "persists cookies across requests that don't return any cookie headers" do get '/cookies/set', 'value' => '1' get '/void' get '/cookies/show' last_request.cookies.must_equal 'value' => '1' end it 'deletes cookies' do get '/cookies/set', 'value' => '1' get '/cookies/delete' get '/cookies/show' last_request.cookies.must_equal({}) end it 'respects cookie domains when no domain is explicitly set' do request('http://example.org/cookies/count').body.must_equal '1' request('http://www.example.org/cookies/count').body.must_equal '1' request('http://example.org/cookies/count').body.must_equal '2' request('http://www.example.org/cookies/count').body.must_equal '2' end it 'treats domains case insensitively' do get 'http://example.com/cookies/set', 'value' => '1' get 'http://EXAMPLE.COM/cookies/show' last_request.cookies.must_equal 'value' => '1' end it 'treats paths case sensitively' do get '/cookies/set', 'value' => '1' get '/COOKIES/show' last_request.cookies.must_equal({}) end it 'prefers more specific cookies' do get 'http://example.com/cookies/set', 'value' => 'domain' get 'http://sub.example.com/cookies/set', 'value' => 'sub' get 'http://sub.example.com/cookies/show' last_request.cookies.must_equal 'value' => 'sub' get 'http://example.com/cookies/show' last_request.cookies.must_equal 'value' => 'domain' end it 'treats cookie names case insensitively' do get '/cookies/set', 'value' => 'lowercase' get '/cookies/set-uppercase', 'value' => 'UPPERCASE' get '/cookies/show' last_request.cookies.must_equal 'VALUE' => 'UPPERCASE' end it 'defaults the domain to the request domain' do get 'http://example.com/cookies/set-simple', 'value' => 'cookie' get 'http://example.com/cookies/show' last_request.cookies.must_equal 'simple' => 'cookie' get 'http://other.example/cookies/show' last_request.cookies.must_equal({}) end it 'defaults the domain to the request path up to the last slash' do get '/cookies/set-simple', 'value' => '1' get '/not-cookies/show' last_request.cookies.must_equal({}) end it 'supports secure cookies' do get 'https://example.com/cookies/set-secure', 'value' => 'set' get 'http://example.com/cookies/show' last_request.cookies.must_equal({}) get 'https://example.com/cookies/show' last_request.cookies.must_equal('secure-cookie' => 'set') rack_mock_session.cookie_jar['secure-cookie'].must_equal 'set' end it 'supports secure cookies when enabling SSL via env' do get '//example.com/cookies/set-secure', { 'value' => 'set' }, 'HTTPS' => 'on' get '//example.com/cookies/show', nil, 'HTTPS' => 'off' last_request.cookies.must_equal({}) get '//example.com/cookies/show', nil, 'HTTPS' => 'on' last_request.cookies.must_equal('secure-cookie' => 'set') rack_mock_session.cookie_jar['secure-cookie'].must_equal 'set' end it 'keeps separate cookie jars for different domains' do get 'http://example.com/cookies/set', 'value' => 'example' get 'http://example.com/cookies/show' last_request.cookies.must_equal 'value' => 'example' get 'http://other.example/cookies/set', 'value' => 'other' get 'http://other.example/cookies/show' last_request.cookies.must_equal 'value' => 'other' get 'http://example.com/cookies/show' last_request.cookies.must_equal 'value' => 'example' end it 'keeps one cookie jar for domain and its subdomains' do get 'http://example.org/cookies/subdomain' get 'http://example.org/cookies/subdomain' last_request.cookies.must_equal 'count' => '1' get 'http://foo.example.org/cookies/subdomain' last_request.cookies.must_equal 'count' => '2' end it 'allows cookies to be cleared' do get '/cookies/set', 'value' => '1' clear_cookies get '/cookies/show' last_request.cookies.must_equal({}) end it 'allow cookies to be set' do set_cookie 'value=10' get '/cookies/show' last_request.cookies.must_equal 'value' => '10' end it 'allows an array of cookies to be set' do set_cookie ['value=10', 'foo=bar'] get '/cookies/show' last_request.cookies.must_equal 'value' => '10', 'foo' => 'bar' end it 'skips emtpy string cookies' do set_cookie "value=10\n\nfoo=bar" get '/cookies/show' last_request.cookies.must_equal 'value' => '10', 'foo' => 'bar' end it 'parses multiple cookies properly' do get '/cookies/set-multiple' get '/cookies/show' last_request.cookies.must_equal 'key1' => 'value1', 'key2' => 'value2' end it 'supports multiple sessions' do with_session(:first) do get '/cookies/set', 'value' => '1' get '/cookies/show' last_request.cookies.must_equal 'value' => '1' end with_session(:second) do get '/cookies/show' last_request.cookies.must_equal({}) end end it 'uses :default as the default session name' do get '/cookies/set', 'value' => '1' get '/cookies/show' last_request.cookies.must_equal 'value' => '1' with_session(:default) do get '/cookies/show' last_request.cookies.must_equal 'value' => '1' end end it 'accepts explicitly provided cookies' do request '/cookies/show', cookie: 'value=1' last_request.cookies.must_equal 'value' => '1' end it 'sets and subsequently sends cookies when redirecting to the path of the cookie' do get '/redirect-with-cookie' follow_redirect! last_request.cookies.must_equal 'value' => '1' end end rack-test-2.1.0/spec/rack/test/methods_spec.rb000066400000000000000000000034411440412547600212530ustar00rootroot00000000000000# frozen-string-literal: true require_relative '../../spec_helper' describe 'Rack::Test::Methods' do it '#rack_mock_session always creates new session if passed nil/false' do rack_mock_session(nil).wont_be_same_as rack_mock_session(nil) rack_mock_session(false).wont_be_same_as rack_mock_session(false) end it '#rack_mock_session reuses existing session if passed truthy value' do rack_mock_session(true).must_be_same_as rack_mock_session(true) rack_mock_session(:true).must_be_same_as rack_mock_session(:true) end it '#rack_test_session always creates new session if passed nil/false' do rack_test_session(nil).wont_be_same_as rack_test_session(nil) rack_test_session(false).wont_be_same_as rack_test_session(false) end it '#rack_test_session reuses existing session if passed truthy value' do rack_test_session(true).must_be_same_as rack_test_session(true) rack_test_session(:true).must_be_same_as rack_test_session(:true) end it '#build_rack_mock_session will be used if present' do session = Rack::Test::Session.new(app) define_singleton_method(:build_rack_mock_session){session} current_session.must_be_same_as session end it '#build_rack_test_session will use defined app' do envs = [] app = proc{|env| envs << env; [200, {}, []]} define_singleton_method(:app){app} get '/' envs.first['PATH_INFO'].must_equal '/' envs.first['HTTP_HOST'].must_equal 'example.org' end it '#build_rack_test_session will use defined default_host' do envs = [] app = proc{|env| envs << env; [200, {}, []]} define_singleton_method(:app){app} define_singleton_method(:default_host){'foo.example.com'} get '/' envs.first['PATH_INFO'].must_equal '/' envs.first['HTTP_HOST'].must_equal 'foo.example.com' end end rack-test-2.1.0/spec/rack/test/multipart_spec.rb000066400000000000000000000123231440412547600216300ustar00rootroot00000000000000# frozen-string-literal: true require_relative '../../spec_helper' uploaded_files = Module.new do def fixture_path(name) File.join(File.dirname(__FILE__), '..', '..', 'fixtures', name) end def first_test_file_path fixture_path('foo.txt') end def uploaded_file Rack::Test::UploadedFile.new(first_test_file_path) end end describe 'Rack::Test::Session uploading one file' do include uploaded_files it 'sends the multipart/form-data content type if no content type is specified' do post '/', 'photo' => uploaded_file last_request.env['CONTENT_TYPE'].must_include 'multipart/form-data;' end it 'sends multipart/related content type if it is explicitly specified' do post '/', { 'photo' => uploaded_file }, 'CONTENT_TYPE' => 'multipart/related' last_request.env['CONTENT_TYPE'].must_include 'multipart/related;' end it 'sends regular params' do post '/', 'photo' => uploaded_file, 'foo' => 'bar' last_request.POST['foo'].must_equal 'bar' end it 'sends nested params' do post '/', 'photo' => uploaded_file, 'foo' => { 'bar' => 'baz' } last_request.POST['foo']['bar'].must_equal 'baz' end it 'sends multiple nested params' do post '/', 'photo' => uploaded_file, 'foo' => { 'bar' => { 'baz' => 'bop' } } last_request.POST['foo']['bar']['baz'].must_equal 'bop' end it 'sends params with arrays' do post '/', 'photo' => uploaded_file, 'foo' => %w[1 2] last_request.POST['foo'].must_equal %w[1 2] end it 'sends params with encoding sensitive values' do post '/', 'photo' => uploaded_file, 'foo' => 'bar? baz' last_request.POST['foo'].must_equal 'bar? baz' end it 'sends params encoded as ISO-8859-1' do utf8 = "\u2603" post '/', 'photo' => uploaded_file, 'foo' => 'bar', 'utf8' => utf8 last_request.POST['foo'].must_equal 'bar' expected_value = if Rack::Test.encoding_aware_strings? utf8 else utf8.b end last_request.POST['utf8'].must_equal expected_value end it 'sends params with parens in names' do post '/', 'photo' => uploaded_file, 'foo(1i)' => 'bar' last_request.POST['foo(1i)'].must_equal 'bar' end it 'sends params with encoding sensitive names' do post '/', 'photo' => uploaded_file, 'foo bar' => 'baz' last_request.POST['foo bar'].must_equal 'baz' end it 'sends files with the filename' do post '/', 'photo' => uploaded_file last_request.POST['photo'][:filename].must_equal 'foo.txt' end it 'sends files with the text/plain MIME type by default' do post '/', 'photo' => uploaded_file last_request.POST['photo'][:type].must_equal 'text/plain' end it 'sends files with the right name' do post '/', 'photo' => uploaded_file last_request.POST['photo'][:name].must_equal 'photo' end it 'allows overriding the content type' do post '/', 'photo' => Rack::Test::UploadedFile.new(first_test_file_path, 'image/jpeg') last_request.POST['photo'][:type].must_equal 'image/jpeg' end it 'sends files with a content-length in the header' do post '/', 'photo' => uploaded_file last_request.POST['photo'][:head].must_include 'content-length: 4' end it 'sends files as Tempfiles' do post '/', 'photo' => uploaded_file last_request.POST['photo'][:tempfile].class.must_equal Tempfile end it 'escapes spaces in filenames properly' do post '/', 'photo' => Rack::Test::UploadedFile.new(fixture_path('space case.txt')) last_request.POST['photo'][:filename].must_equal 'space case.txt' end end describe 'uploading two files' do include uploaded_files def second_test_file_path fixture_path('bar.txt') end def second_uploaded_file Rack::Test::UploadedFile.new(second_test_file_path) end it 'sends the multipart/form-data content type' do post '/', 'photos' => [uploaded_file, second_uploaded_file] last_request.env['CONTENT_TYPE'].must_include 'multipart/form-data;' end it 'sends files with the filename' do post '/', 'photos' => [uploaded_file, second_uploaded_file] last_request.POST['photos'].collect { |photo| photo[:filename] }.must_equal ['foo.txt', 'bar.txt'] end it 'sends files with the text/plain MIME type by default' do post '/', 'photos' => [uploaded_file, second_uploaded_file] last_request.POST['photos'].collect { |photo| photo[:type] }.must_equal ['text/plain', 'text/plain'] end it 'sends files with the right names' do post '/', 'photos' => [uploaded_file, second_uploaded_file] last_request.POST['photos'].all? { |photo| photo[:name].must_equal 'photos[]' } end it 'allows mixed content types' do image_file = Rack::Test::UploadedFile.new(first_test_file_path, 'image/jpeg') post '/', 'photos' => [uploaded_file, image_file] last_request.POST['photos'].collect { |photo| photo[:type] }.must_equal ['text/plain', 'image/jpeg'] end it 'sends files with a content-length in the header' do post '/', 'photos' => [uploaded_file, second_uploaded_file] last_request.POST['photos'].all? { |photo| photo[:head].must_include 'content-length: 4' } end it 'sends both files as Tempfiles' do post '/', 'photos' => [uploaded_file, second_uploaded_file] last_request.POST['photos'].all? { |photo| photo[:tempfile].class.must_equal Tempfile } end end rack-test-2.1.0/spec/rack/test/uploaded_file_spec.rb000066400000000000000000000056371440412547600224150ustar00rootroot00000000000000# frozen-string-literal: true require_relative '../../spec_helper' describe Rack::Test::UploadedFile do def file_path File.dirname(__FILE__) + '/../../fixtures/foo.txt' end it 'returns an instance of `Rack::Test::UploadedFile`' do uploaded_file = Rack::Test::UploadedFile.new(file_path) uploaded_file.class.must_equal Rack::Test::UploadedFile end it 'responds to things that Tempfile responds to' do uploaded_file = Rack::Test::UploadedFile.new(file_path) Tempfile.public_instance_methods(false).each do |method| uploaded_file.must_respond_to method end end it "creates Tempfiles with original file's extension" do uploaded_file = Rack::Test::UploadedFile.new(file_path) File.extname(uploaded_file.path).must_equal '.txt' end it 'creates Tempfiles with a path that includes a single extension' do uploaded_file = Rack::Test::UploadedFile.new(file_path) regex = /foo#{Time.now.year}.*\.txt\Z/ uploaded_file.path.must_match regex end it 'allows to override the Tempfiles original_filename' do uploaded_file = Rack::Test::UploadedFile.new(file_path, original_filename: 'bar.txt') regex = /bar#{Time.now.year}.*\.txt\Z/ uploaded_file.path.must_match regex end it 'respects binary argument' do Rack::Test::UploadedFile.new(file_path, 'text/plain', true).tempfile.must_be :binmode? Rack::Test::UploadedFile.new(file_path, 'text/plain', false).tempfile.wont_be :binmode? Rack::Test::UploadedFile.new(file_path, 'text/plain').tempfile.wont_be :binmode? end it 'raises for invalid files' do proc{Rack::Test::UploadedFile.new('does_not_exist')}.must_raise RuntimeError end it 'finalizes on garbage collection' do finalized = false c = Class.new(Rack::Test::UploadedFile) do define_singleton_method(:actually_finalize) do |file| finalized = true super(file) end end if RUBY_PLATFORM == 'java' require 'java' java_import 'java.lang.System' 50.times do |_i| c.new(file_path) System.gc end else 50.times do |_i| c.new(file_path) GC.start end end # Due to CRuby's conservative garbage collection, you can never guarantee # an object will be garbage collected, so this is a source of potential # nondeterministic failure finalized.must_equal true end if RUBY_VERSION >= '2.7' || RUBY_ENGINE != 'ruby' it '#initialize with an IO object sets the specified filename' do original_filename = 'content.txt' uploaded_file = Rack::Test::UploadedFile.new(StringIO.new('I am content'), original_filename: original_filename) uploaded_file.original_filename.must_equal original_filename end it '#initialize without an original filename raises an error' do proc { Rack::Test::UploadedFile.new(StringIO.new('I am content')) }.must_raise(ArgumentError, 'Missing `original_filename` for StringIO object') end end rack-test-2.1.0/spec/rack/test/utils_spec.rb000066400000000000000000000271301440412547600207510ustar00rootroot00000000000000# frozen-string-literal: true require_relative '../../spec_helper' describe "Rack::Test::Utils#build_nested_query" do include Rack::Test::Utils it 'converts empty strings to =' do build_nested_query('').must_equal '=' end it 'converts nil to an empty string' do build_nested_query(nil).must_equal '' end it 'converts hashes with nil values' do build_nested_query(a: nil).must_equal 'a' end it 'converts hashes' do build_nested_query(a: 1).must_equal 'a=1' end it 'converts hashes with multiple keys' do hash = { a: 1, b: 2 } build_nested_query(hash).must_equal 'a=1&b=2' end it 'converts empty arrays' do build_nested_query(a: []).must_equal 'a[]=' end it 'converts arrays with one element' do build_nested_query(a: [1]).must_equal 'a[]=1' end it 'converts arrays with multiple elements' do build_nested_query(a: [1, 2]).must_equal 'a[]=1&a[]=2' end it "converts arrays with brackets '[]' in the name" do build_nested_query('a[]' => [1, 2]).must_equal 'a%5B%5D=1&a%5B%5D=2' end it 'converts nested hashes' do build_nested_query(a: { b: 1 }).must_equal 'a[b]=1' end it 'converts arrays nested in a hash' do build_nested_query(a: { b: [1, 2] }).must_equal 'a[b][]=1&a[b][]=2' end it 'converts arrays of hashes' do build_nested_query(a: [{ b: 2 }, { c: 3 }]).must_equal 'a[][b]=2&a[][c]=3' end it 'supports hash keys with empty arrays' do input = { collection: [] } build_nested_query(input).must_equal 'collection[]=' end end describe 'Rack::Test::Utils.build_multipart' do include Rack::Test::Utils it 'builds multipart bodies' do files = Rack::Test::UploadedFile.new(multipart_file('foo.txt')) data = Rack::Test::Utils.build_multipart('submit-name' => 'Larry', 'files' => files) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['submit-name'].must_equal 'Larry' params['files'][:filename].must_equal 'foo.txt' files.pos.must_equal 0 params['files'][:tempfile].read.must_equal files.read end it 'handles uploaded files not responding to set_encoding as empty' do # Capybara::RackTest::Form::NilUploadedFile c = Class.new(Rack::Test::UploadedFile) do def initialize @empty_file = Tempfile.new('nil_uploaded_file') @empty_file.close end def original_filename; ''; end def content_type; 'application/octet-stream'; end def path; @empty_file.path; end def size; 0; end def read; ''; end def respond_to?(m, *a) return false if m == :set_encoding super(m, *a) end end data = Rack::Test::Utils.build_multipart('submit-name' => 'Larry', 'files' => c.new) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['submit-name'].must_equal 'Larry' params['files'].must_be_nil data.must_include 'content-disposition: form-data; name="files"; filename=""' data.must_include 'content-length: 0' end it 'builds multipart bodies from array of files' do files = [Rack::Test::UploadedFile.new(multipart_file('foo.txt')), Rack::Test::UploadedFile.new(multipart_file('bar.txt'))] data = Rack::Test::Utils.build_multipart('submit-name' => 'Larry', 'files' => files) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['submit-name'].must_equal 'Larry' params['files'][0][:filename].must_equal 'foo.txt' params['files'][0][:tempfile].read.must_equal "bar\n" params['files'][1][:filename].must_equal 'bar.txt' params['files'][1][:tempfile].read.must_equal "baz\n" end it 'builds multipart bodies from mixed array of a file and a primitive' do files = [Rack::Test::UploadedFile.new(multipart_file('foo.txt')), 'baz'] data = Rack::Test::Utils.build_multipart('files' => files) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['files'][0][:filename].must_equal 'foo.txt' params['files'][0][:tempfile].read.must_equal "bar\n" params['files'][1].must_equal 'baz' end it 'builds nested multipart bodies' do files = Rack::Test::UploadedFile.new(multipart_file('foo.txt')) data = Rack::Test::Utils.build_multipart('people' => [{ 'submit-name' => 'Larry', 'files' => files }], 'foo' => %w[1 2]) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['people'][0]['submit-name'].must_equal 'Larry' params['people'][0]['files'][:filename].must_equal 'foo.txt' params['people'][0]['files'][:tempfile].read.must_equal "bar\n" params['foo'].must_equal %w[1 2] end it 'builds nested multipart bodies with UTF-8 data' do files = Rack::Test::UploadedFile.new(multipart_file('mb.txt')) data = Rack::Test::Utils.build_multipart('people' => [{ 'submit-name' => "\u1234", 'files' => files }], 'foo' => %w[1 2]) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['people'][0]['submit-name'].b.must_equal "\u1234".b params['people'][0]['files'][:filename].must_equal 'mb.txt' params['people'][0]['files'][:tempfile].read.must_equal "\u2345".b params['foo'].must_equal %w[1 2] files = Rack::Test::UploadedFile.new(multipart_file('mb.txt')) data = Rack::Test::Utils.build_multipart('people' => [{ 'files' => files, 'submit-name' => "\u1234" }], 'foo' => %w[1 2]) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['people'][0]['submit-name'].b.must_equal "\u1234".b params['people'][0]['files'][:filename].must_equal 'mb.txt' params['people'][0]['files'][:tempfile].read.must_equal "\u2345".b params['foo'].must_equal %w[1 2] end it 'builds nested multipart bodies with an array of hashes' do files = Rack::Test::UploadedFile.new(multipart_file('foo.txt')) data = Rack::Test::Utils.build_multipart('files' => files, 'foo' => [{ 'id' => '1', 'name' => 'Dave' }, { 'id' => '2', 'name' => 'Steve' }]) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['files'][:filename].must_equal 'foo.txt' params['files'][:tempfile].read.must_equal "bar\n" params['foo'].must_equal [{ 'id' => '1', 'name' => 'Dave' }, { 'id' => '2', 'name' => 'Steve' }] end it 'builds nested multipart bodies with arbitrarily nested array of hashes' do files = Rack::Test::UploadedFile.new(multipart_file('foo.txt')) data = Rack::Test::Utils.build_multipart('files' => files, 'foo' => { 'bar' => [{ 'id' => '1', 'name' => 'Dave' }, { 'id' => '2', 'name' => 'Steve', 'qux' => [{ 'id' => '3', 'name' => 'mike' }, { 'id' => '4', 'name' => 'Joan' }] }] }) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['files'][:filename].must_equal 'foo.txt' params['files'][:tempfile].read.must_equal "bar\n" params['foo'].must_equal 'bar' => [{ 'id' => '1', 'name' => 'Dave' }, { 'id' => '2', 'name' => 'Steve', 'qux' => [{ 'id' => '3', 'name' => 'mike' }, { 'id' => '4', 'name' => 'Joan' }] }] end it 'does not break with params that look nested, but are not' do files = Rack::Test::UploadedFile.new(multipart_file('foo.txt')) data = Rack::Test::Utils.build_multipart('foo[]' => '1', 'bar[]' => { 'qux' => '2' }, 'files[]' => files) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['files'][0][:filename].must_equal 'foo.txt' params['files'][0][:tempfile].read.must_equal "bar\n" params['foo'][0].must_equal '1' params['bar'][0].must_equal 'qux' => '2' end it 'allows for nested files' do files = Rack::Test::UploadedFile.new(multipart_file('foo.txt')) data = Rack::Test::Utils.build_multipart('foo' => [{ 'id' => '1', 'data' => files }, { 'id' => '2', 'data' => %w[3 4] }]) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['foo'][0]['id'].must_equal '1' params['foo'][0]['data'][:filename].must_equal 'foo.txt' params['foo'][0]['data'][:tempfile].read.must_equal "bar\n" params['foo'][1].must_equal 'id' => '2', 'data' => %w[3 4] end it 'returns nil if no UploadedFiles were used' do Rack::Test::Utils.build_multipart('people' => [{ 'submit-name' => 'Larry', 'files' => 'contents' }]).must_be_nil end it 'allows for forcing multipart uploads even without a file' do data = Rack::Test::Utils.build_multipart({'foo' => [{ 'id' => '2', 'data' => %w[3 4] }]}, true, true) options = { 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}", 'CONTENT_LENGTH' => data.length.to_s, :input => StringIO.new(data) } env = Rack::MockRequest.env_for('/', options) params = Rack::Multipart.parse_multipart(env) params['foo'][0].must_equal 'id' => '2', 'data' => %w[3 4] end it 'raises ArgumentErrors if params is not a Hash' do proc do Rack::Test::Utils.build_multipart('foo=bar') end.must_raise(ArgumentError, 'value must be a Hash') end def multipart_file(name) File.join(File.dirname(__FILE__), '..', '..', 'fixtures', name.to_s) end end rack-test-2.1.0/spec/rack/test_spec.rb000066400000000000000000000563771440412547600176300ustar00rootroot00000000000000# frozen-string-literal: true require_relative '../spec_helper' describe 'Rack::Test::Session' do it 'has alias of Rack::MockSession for backwards compatibility' do Rack::MockSession.must_be_same_as Rack::Test::Session end it 'supports being initialized with a Rack::MockSession app' do Rack::Test::Session.new(Rack::MockSession.new(app)).request('/').must_be :ok? end it 'supports being initialized with an app' do Rack::Test::Session.new(app).request('/').must_be :ok? end end describe 'Rack::Test::Session#request' do it 'requests the URI using GET by default' do request '/' last_request.env['REQUEST_METHOD'].must_equal 'GET' last_response.must_be :ok? end it 'returns last response' do request('/').must_be :ok? end it 'uses the provided env' do request '/', 'X-Foo' => 'bar' last_request.env['X-Foo'].must_equal 'bar' end it 'allows HTTP_HOST to be set' do request '/', 'HTTP_HOST' => 'www.example.ua' last_request.env['HTTP_HOST'].must_equal 'www.example.ua' end it 'sets HTTP_HOST with port for non-default ports' do request 'http://foo.com:8080' last_request.env['HTTP_HOST'].must_equal 'foo.com:8080' request 'https://foo.com:8443' last_request.env['HTTP_HOST'].must_equal 'foo.com:8443' end it 'sets HTTP_HOST without port for default ports' do request 'http://foo.com' last_request.env['HTTP_HOST'].must_equal 'foo.com' request 'http://foo.com:80' last_request.env['HTTP_HOST'].must_equal 'foo.com' request 'https://foo.com:443' last_request.env['HTTP_HOST'].must_equal 'foo.com' end it 'defaults the REMOTE_ADDR to 127.0.0.1' do request '/' last_request.env['REMOTE_ADDR'].must_equal '127.0.0.1' end it 'sets rack.test to true in the env' do request '/' last_request.env['rack.test'].must_equal true end it 'defaults to port 80' do request '/' last_request.env['SERVER_PORT'].must_equal '80' end it 'defaults to example.org' do request '/' last_request.env['SERVER_NAME'].must_equal 'example.org' end it 'yields the response to a given block' do request '/' do |response| response.must_be :ok? end end it 'supports sending :params for GET' do request '/', params: { 'foo' => 'bar' } last_request.GET['foo'].must_equal 'bar' end it 'supports sending :query_params for GET' do request '/', query_params: { 'foo' => 'bar' } last_request.GET['foo'].must_equal 'bar' end it 'supports sending both :params and :query_params for GET' do request '/', query_params: { 'foo' => 'bar' }, params: { 'foo2' => 'bar2' } last_request.GET['foo'].must_equal 'bar' last_request.GET['foo2'].must_equal 'bar2' end it 'supports sending :params for POST' do request '/', method: :post, params: { 'foo' => 'bar' } last_request.POST['foo'].must_equal 'bar' end it 'does not use multipart input for :params for POST by default' do request '/', method: :post, params: { 'foo' => 'bar' } last_request.POST['foo'].must_equal 'bar' last_request.env['rack.input'].rewind last_request.env['rack.input'].read.must_equal 'foo=bar' end it 'supports :multipart when using :params for POST to force multipart input' do request '/', method: :post, params: { 'foo' => 'bar' }, multipart: true last_request.POST['foo'].must_equal 'bar' last_request.env['rack.input'].rewind last_request.env['rack.input'].read.must_include 'content-disposition: form-data; name="foo"' end it 'supports multipart CONTENT_TYPE when using :params for POST to force multipart input' do request '/', method: :post, params: { 'foo' => 'bar' }, 'CONTENT_TYPE'=>'multipart/form-data' last_request.POST['foo'].must_equal 'bar' last_request.env['rack.input'].rewind last_request.env['rack.input'].read.must_include 'content-disposition: form-data; name="foo"' end it 'supports multipart CONTENT_TYPE when using empty :params for POST to be empty body' do request '/', method: :post, params: {}, 'CONTENT_TYPE'=>'multipart/form-data' last_request.POST.must_be_empty last_request.env['rack.input'].rewind last_request.env['rack.input'].read.must_be_empty end it 'supports sending :query_params for POST' do request '/', method: :post, query_params: { 'foo' => 'bar' } last_request.GET['foo'].must_equal 'bar' end it 'supports sending both :params and :query_params for POST' do request '/', method: :post, query_params: { 'foo' => 'bar' }, params: { 'foo2' => 'bar2' } last_request.GET['foo'].must_equal 'bar' last_request.POST['foo2'].must_equal 'bar2' end it "doesn't follow redirects by default" do request '/redirect' last_response.must_be :redirect? last_response.body.must_be_empty end it 'allows passing :input in for POSTs' do request '/', method: :post, input: 'foo' last_request.env['rack.input'].read.must_equal 'foo' end it 'converts method names to a uppercase strings' do request '/', method: :put last_request.env['REQUEST_METHOD'].must_equal 'PUT' end it 'prepends a slash to the URI path' do request 'foo' last_request.env['PATH_INFO'].must_equal '/foo' end it 'accepts params and builds query strings for GET requests' do request '/foo?baz=2', params: { foo: { bar: '1' } } last_request.GET.must_equal 'baz' => '2', 'foo' => { 'bar' => '1' } end it 'parses query strings with repeated variable names correctly' do request '/foo?bar=2&bar=3' last_request.GET.must_equal 'bar' => '3' end it 'accepts raw input in params for GET requests' do request '/foo?baz=2', params: 'foo[bar]=1' last_request.GET.must_equal 'baz' => '2', 'foo' => { 'bar' => '1' } end it 'does not rewrite a GET query string when :params is not supplied' do request '/foo?a=1&b=2&c=3&e=4&d=5+%20' last_request.query_string.must_equal 'a=1&b=2&c=3&e=4&d=5+%20' end it 'does not rewrite a GET query string when :params is empty' do request '/foo?a=1&b=2&c=3&e=4&d=5', params: {} last_request.query_string.must_equal 'a=1&b=2&c=3&e=4&d=5' end it 'does not overwrite multiple query string keys' do request '/foo?a=1&a=2', params: { bar: 1 } last_request.query_string.must_equal 'a=1&a=2&bar=1' end it 'accepts params and builds url encoded params for POST requests' do request '/foo', method: :post, params: { foo: { bar: '1' } } last_request.env['rack.input'].read.must_equal 'foo[bar]=1' end it 'accepts raw input in params for POST requests' do request '/foo', method: :post, params: 'foo[bar]=1' last_request.env['rack.input'].read.must_equal 'foo[bar]=1' end it 'supports a Rack::Response' do app = lambda do |_env| Rack::Response.new('', 200, {}) end session = Rack::Test::Session.new(Rack::MockSession.new(app)) session.request('/').must_be :ok? end closeable_body = Class.new do def initialize @closed = false end def each return if @closed yield 'Hello, World!' end def close @closed = true end def closed? @closed end end it "closes response's body when body responds_to?(:close)" do body = closeable_body.new app = lambda do |_env| [200, { 'content-type' => 'text/html', 'content-length' => '13' }, body] end session = Rack::Test::Session.new(Rack::MockSession.new(app)) body.closed?.must_equal false session.request('/') body.closed?.must_equal true end it "closes response's body after iteration when body responds_to?(:close)" do body = nil app = lambda do |_env| [200, { 'content-type' => 'text/html', 'content-length' => '13' }, body = closeable_body.new] end session = Rack::Test::Session.new(Rack::MockSession.new(app)) session.request('/') session.last_response.body.must_equal 'Hello, World!' body.closed?.must_equal true end it 'sends the input when input is given' do request '/', method: 'POST', input: 'foo' last_request.env['rack.input'].read.must_equal 'foo' end it 'does not send a multipart request when input is given' do request '/', method: 'POST', input: 'foo' last_request.env['CONTENT_TYPE'].wont_equal 'application/x-www-form-urlencoded' end it 'uses application/x-www-form-urlencoded as the CONTENT_TYPE for a POST specified with :method' do request '/', method: 'POST' last_request.env['CONTENT_TYPE'].must_equal 'application/x-www-form-urlencoded' end it 'uses application/x-www-form-urlencoded as the CONTENT_TYPE for a POST specified with REQUEST_METHOD' do request '/', 'REQUEST_METHOD' => 'POST' last_request.env['CONTENT_TYPE'].must_equal 'application/x-www-form-urlencoded' end it 'does not overwrite the CONTENT_TYPE when CONTENT_TYPE is specified in the env' do request '/', 'CONTENT_TYPE' => 'application/xml' last_request.env['CONTENT_TYPE'].must_equal 'application/xml' end it 'sets rack.url_scheme to https when the URL is https://' do request 'https://example.org/' last_request.env['rack.url_scheme'].must_equal 'https' end it 'sets SERVER_PORT to 443 when the URL is https://' do request 'https://example.org/' last_request.env['SERVER_PORT'].must_equal '443' end it 'sets HTTPS to on when the URL is https://' do request 'https://example.org/' last_request.env['HTTPS'].must_equal 'on' end it 'sends XMLHttpRequest for the X-Requested-With header if :xhr option is given' do request '/', xhr: true last_request.env['HTTP_X_REQUESTED_WITH'].must_equal 'XMLHttpRequest' last_request.must_be :xhr? end end describe 'Rack::Test::Session#header' do it 'sets a header to be sent with requests' do header 'User-Agent', 'Firefox' request '/' last_request.env['HTTP_USER_AGENT'].must_equal 'Firefox' end it 'sets a content-type to be sent with requests' do header 'content-type', 'application/json' request '/' last_request.env['CONTENT_TYPE'].must_equal 'application/json' end it 'sets a Host to be sent with requests' do header 'Host', 'www.example.ua' request '/' last_request.env['HTTP_HOST'].must_equal 'www.example.ua' end it 'persists across multiple requests' do header 'User-Agent', 'Firefox' request '/' request '/' last_request.env['HTTP_USER_AGENT'].must_equal 'Firefox' end it 'overwrites previously set headers' do header 'User-Agent', 'Firefox' header 'User-Agent', 'Safari' request '/' last_request.env['HTTP_USER_AGENT'].must_equal 'Safari' end it 'can be used to clear a header' do header 'User-Agent', 'Firefox' header 'User-Agent', nil request '/' last_request.env.wont_include 'HTTP_USER_AGENT' end it 'is overridden by headers sent during the request' do header 'User-Agent', 'Firefox' request '/', 'HTTP_USER_AGENT' => 'Safari' last_request.env['HTTP_USER_AGENT'].must_equal 'Safari' end end describe 'Rack::Test::Session#env' do it 'sets the env to be sent with requests' do env 'rack.session', csrf: 'token' request '/' last_request.env['rack.session'].must_equal csrf: 'token' end it 'persists across multiple requests' do env 'rack.session', csrf: 'token' request '/' request '/' last_request.env['rack.session'].must_equal csrf: 'token' end it 'overwrites previously set envs' do env 'rack.session', csrf: 'token' env 'rack.session', some: :thing request '/' last_request.env['rack.session'].must_equal some: :thing end it 'can be used to clear a env' do env 'rack.session', csrf: 'token' env 'rack.session', nil request '/' last_request.env.wont_include 'X_CSRF_TOKEN' end it 'is overridden by envs sent during the request' do env 'rack.session', csrf: 'token' request '/', 'rack.session' => { some: :thing } last_request.env['rack.session'].must_equal some: :thing end end describe 'Rack::Test::Session#basic_authorize' do it 'sets the HTTP_AUTHORIZATION header' do basic_authorize 'bryan', 'secret' request '/' last_request.env['HTTP_AUTHORIZATION'].must_equal 'Basic YnJ5YW46c2VjcmV0' end it 'includes the header for subsequent requests' do basic_authorize 'bryan', 'secret' request '/' request '/' last_request.env['HTTP_AUTHORIZATION'].must_equal 'Basic YnJ5YW46c2VjcmV0' end end describe 'Rack::Test::Session#follow_redirect!' do it 'follows redirects' do get '/redirect' follow_redirect! last_response.wont_be :redirect? last_response.body.must_equal "You've been redirected, session {} with options {}" last_request.env['HTTP_REFERER'].must_equal 'http://example.org/redirect' end it 'follows absolute redirects' do get '/absolute/redirect' last_response.headers['location'].must_equal 'https://www.google.com' follow_redirect! last_request.env['PATH_INFO'].must_equal '/' last_request.env['HTTP_HOST'].must_equal 'www.google.com' last_request.env['HTTPS'].must_equal 'on' end it 'follows nested redirects' do get '/nested/redirect' last_response.headers['location'].must_equal 'redirected' follow_redirect! last_response.must_be :ok? last_request.env['PATH_INFO'].must_equal '/nested/redirected' end it 'does not include params when following the redirect' do get '/redirect', 'foo' => 'bar' follow_redirect! last_request.GET.must_be_empty end it 'includes session when following the redirect' do get '/redirect', {}, 'rack.session' => { 'foo' => 'bar' } follow_redirect! last_response.body.must_include 'session {"foo"=>"bar"}' end it 'includes session options when following the redirect' do get '/redirect', {}, 'rack.session.options' => { 'foo' => 'bar' } follow_redirect! last_response.body.must_include 'session {} with options {"foo"=>"bar"}' end it 'raises an error if the last_response is not set' do proc do follow_redirect! end.must_raise(Rack::Test::Error) end it 'raises an error if the last_response is not a redirect' do get '/' proc do follow_redirect! end.must_raise(Rack::Test::Error) end it 'keeps the original method and params for HTTP 307' do post '/redirect?status=307', foo: 'bar' follow_redirect! last_response.body.must_include 'post' last_response.body.must_include 'foo' last_response.body.must_include 'bar' end end describe 'Rack::Test::Session#last_request' do it 'returns the most recent request' do request '/' last_request.env['PATH_INFO'].must_equal '/' end it 'raises an error if no requests have been issued' do proc do last_request end.must_raise(Rack::Test::Error) end end describe 'Rack::Test::Session#last_response' do it 'returns the most recent response' do request '/' last_response['content-type'].must_equal 'text/html;charset=utf-8' end it 'raises an error if no requests have been issued' do proc do last_response end.must_raise(Rack::Test::Error) end end describe 'Rack::Test::Session#after_request' do it 'runs callbacks after each request' do ran = false rack_mock_session.after_request do ran = true end get '/' ran.must_equal true end it 'runs multiple callbacks' do count = 0 2.times do rack_mock_session.after_request do count += 1 end end get '/' count.must_equal 2 end end verb_examples = Module.new do extend Minitest::Spec::DSL it 'requests the URL using VERB' do public_send(verb, '/') last_request.env['REQUEST_METHOD'].must_equal verb.to_s.upcase last_response.must_be :ok? end it 'uses the provided env' do public_send(verb, '/', {}, 'HTTP_USER_AGENT' => 'Rack::Test') last_request.env['HTTP_USER_AGENT'].must_equal 'Rack::Test' end it 'yields the response to a given block' do yielded = false public_send(verb, '/') do |response| response.must_be :ok? yielded = true end yielded.must_equal true end it 'sets the HTTP_HOST header with port' do public_send(verb, 'http://example.org:8080/uri') last_request.env['HTTP_HOST'].must_equal 'example.org:8080' end it 'sets the HTTP_HOST header without port' do public_send(verb, '/uri') last_request.env['HTTP_HOST'].must_equal 'example.org' end it 'sends XMLHttpRequest for the X-Requested-With header' do public_send(verb, '/', {}, xhr: true) last_request.env['HTTP_X_REQUESTED_WITH'].must_equal 'XMLHttpRequest' last_request.must_be :xhr? end end non_get_verb_examples = Module.new do extend Minitest::Spec::DSL it 'sets CONTENT_TYPE to application/x-www-form-urlencoded when params are not provided' do public_send(verb, '/') last_request.env['CONTENT_TYPE'].must_equal 'application/x-www-form-urlencoded' end it 'sets CONTENT_LENGTH to zero when params are not provided' do public_send(verb, '/') last_request.env['CONTENT_LENGTH'].must_equal '0' end it 'sets CONTENT_TYPE to application/x-www-form-urlencoded when params are explicitly set to nil' do public_send(verb, '/', nil) last_request.env['CONTENT_TYPE'].must_equal 'application/x-www-form-urlencoded' end it 'sets CONTENT_LENGTH to 0 when params are explicitly set to nil' do public_send(verb, '/', nil) last_request.env['CONTENT_LENGTH'].must_equal '0' end end describe 'Rack::Test::Session#get' do def verb; :get; end include verb_examples # This is not actually explicitly stated in the relevant RFCs; # https://tools.ietf.org/html/rfc7231#section-3.1.1.5 # ...but e.g. curl do not set it for GET requests. it 'does not set CONTENT_TYPE when params are not provided' do get '/' last_request.env.wont_include 'CONTENT_TYPE' end it 'sets CONTENT_LENGTH to zero or does not set it when params are not provided' do get '/' ['0', nil].must_include last_request.env['CONTENT_LENGTH'] end it 'does not set CONTENT_TYPE twhen params are explicitly set to nil' do get '/', nil last_request.env.wont_include 'CONTENT_TYPE' end it 'sets CONTENT_LENGTH to zero or does not set it when params are explicitly set to nil' do get '/', nil ['0', nil].must_include last_request.env['CONTENT_LENGTH'] end it 'uses the provided params hash' do get '/', foo: 'bar' last_request.GET.must_equal 'foo' => 'bar' end it 'sends params with parens in names' do get '/', 'foo(1i)' => 'bar' last_request.GET['foo(1i)'].must_equal 'bar' end it 'supports params with encoding sensitive names' do get '/', 'foo bar' => 'baz' last_request.GET['foo bar'].must_equal 'baz' end it 'supports params with nested encoding sensitive names' do get '/', 'boo' => { 'foo bar' => 'baz' } last_request.GET.must_equal 'boo' => { 'foo bar' => 'baz' } end it 'accepts params in the path' do get '/?foo=bar' last_request.GET.must_equal 'foo' => 'bar' end end describe 'Rack::Test::Session#head' do def verb; :head; end include verb_examples include non_get_verb_examples end describe 'Rack::Test::Session#post' do def verb; :post; end include verb_examples include non_get_verb_examples it 'uses the provided params hash' do post '/', foo: 'bar' last_request.POST.must_equal 'foo' => 'bar' end it 'supports params with encoding sensitive names' do post '/', 'foo bar' => 'baz' last_request.POST['foo bar'].must_equal 'baz' end it 'uses application/x-www-form-urlencoded as the default CONTENT_TYPE' do post '/' last_request.env['CONTENT_TYPE'].must_equal 'application/x-www-form-urlencoded' end it 'sets the CONTENT_LENGTH' do post '/', foo: 'bar' last_request.env['CONTENT_LENGTH'].must_equal '7' end it 'accepts a body' do post '/', 'Lobsterlicious!' last_request.body.read.must_equal 'Lobsterlicious!' end it 'does not overwrite the CONTENT_TYPE when CONTENT_TYPE is specified in the env' do post '/', {}, 'CONTENT_TYPE' => 'application/xml' last_request.env['CONTENT_TYPE'].must_equal 'application/xml' end end describe 'Rack::Test::Session#put' do def verb; :put; end include verb_examples include non_get_verb_examples it 'accepts a body' do put '/', 'Lobsterlicious!' last_request.body.read.must_equal 'Lobsterlicious!' end end describe 'Rack::Test::Session#patch' do def verb; :patch; end include verb_examples include non_get_verb_examples it 'accepts a body' do patch '/', 'Lobsterlicious!' last_request.body.read.must_equal 'Lobsterlicious!' end end describe 'Rack::Test::Session#delete' do def verb; :delete; end include verb_examples include non_get_verb_examples it 'accepts a body' do patch '/', 'Lobsterlicious!' last_request.body.read.must_equal 'Lobsterlicious!' end it 'uses the provided params hash' do delete '/', foo: 'bar' last_request.GET.must_equal({}) last_request.POST.must_equal 'foo' => 'bar' last_request.body.rewind last_request.body.read.must_equal 'foo=bar' end it 'accepts params in the path' do delete '/?foo=bar' last_request.GET.must_equal 'foo' => 'bar' last_request.POST.must_equal({}) last_request.body.read.must_equal '' end it 'accepts a body' do delete '/', 'Lobsterlicious!' last_request.GET.must_equal({}) last_request.body.read.must_equal 'Lobsterlicious!' end end describe 'Rack::Test::Session#options' do def verb; :options; end include verb_examples include non_get_verb_examples end describe 'Rack::Test::Session#custom_request' do it 'requests the URL using the given' do custom_request('link', '/') last_request.env['REQUEST_METHOD'].must_equal 'LINK' last_response.must_be :ok? end it 'uses the provided env' do custom_request('link', '/', {}, 'HTTP_USER_AGENT' => 'Rack::Test') last_request.env['HTTP_USER_AGENT'].must_equal 'Rack::Test' end it 'yields the response to a given block' do yielded = false custom_request('link', '/') do |response| response.must_be :ok? yielded = true end yielded.must_equal true end it 'sets the HTTP_HOST header with port' do custom_request('link', 'http://example.org:8080/uri') last_request.env['HTTP_HOST'].must_equal 'example.org:8080' end it 'sets the HTTP_HOST header without port' do custom_request('link', '/uri') last_request.env['HTTP_HOST'].must_equal 'example.org' end it 'sends XMLHttpRequest for the X-Requested-With header for an XHR' do custom_request('link', '/', {}, xhr: true) last_request.env['HTTP_X_REQUESTED_WITH'].must_equal 'XMLHttpRequest' last_request.must_be :xhr? end end describe 'Rack::Test::Session#restore_state' do it 'restores last request, last response, cookies, and hooks after block' do after_request = [] current_session.after_request{after_request << 1} get('/') request = last_request response = last_response current_session.cookie_jar['simple'].must_be_nil after_request.must_equal [1] current_session.restore_state do current_session.after_request{after_request << 2} get('/cookies/set-simple?value=foo') current_session.cookie_jar['simple'].must_equal 'foo' last_request.wont_be_same_as request last_response.wont_be_same_as response after_request.must_equal [1, 1, 2] end last_request.must_be_same_as request last_response.must_be_same_as response current_session.cookie_jar['simple'].must_be_nil get('/') after_request.must_equal [1, 1, 2, 1] end end rack-test-2.1.0/spec/spec_helper.rb000066400000000000000000000013651440412547600171730ustar00rootroot00000000000000if ENV.delete('COVERAGE') require 'simplecov' SimpleCov.start do enable_coverage :branch add_filter "/spec/" add_group('Missing'){|src| src.covered_percent < 100} add_group('Covered'){|src| src.covered_percent == 100} end end ENV['MT_NO_PLUGINS'] = '1' # Work around stupid autoloading of plugins gem 'minitest' require 'minitest/global_expectations/autorun' require_relative '../lib/rack/test' require_relative 'fixtures/fake_app' class Minitest::Spec include Rack::Test::Methods def app Rack::Test::FAKE_APP end def self.deprecated(*args, &block) it(*args) do begin verbose, $VERBOSE = $VERBOSE, nil instance_exec(&block) ensure $VERBOSE = verbose end end end end