rack-test-0.7.0/0000755000175000017500000000000013144300104013235 5ustar uwabamiuwabamirack-test-0.7.0/lib/0000755000175000017500000000000013144300104014003 5ustar uwabamiuwabamirack-test-0.7.0/lib/rack/0000755000175000017500000000000013144300104014723 5ustar uwabamiuwabamirack-test-0.7.0/lib/rack/mock_session.rb0000644000175000017500000000331113144300104017742 0ustar uwabamiuwabamimodule Rack class MockSession # :nodoc: attr_writer :cookie_jar attr_reader :default_host def initialize(app, default_host = Rack::Test::DEFAULT_HOST) @app = app @after_request = [] @default_host = default_host @last_request = nil @last_response = nil end def after_request(&block) @after_request << block end def clear_cookies @cookie_jar = Rack::Test::CookieJar.new([], @default_host) end def set_cookie(cookie, uri = nil) cookie_jar.merge(cookie, uri) end def request(uri, env) env["HTTP_COOKIE"] ||= cookie_jar.for(uri) @last_request = Rack::Request.new(env) status, headers, body = @app.call(@last_request.env) @last_response = MockResponse.new(status, headers, body, env["rack.errors"].flush) body.close if body.respond_to?(:close) cookie_jar.merge(last_response.headers["Set-Cookie"], uri) @after_request.each { |hook| hook.call } if @last_response.respond_to?(:finish) @last_response.finish else @last_response end end # Return the last request issued in the session. Raises an error if no # requests have been sent yet. def last_request raise Rack::Test::Error.new("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 Rack::Test::Error.new("No response yet. Request a page first.") unless @last_response @last_response end def cookie_jar @cookie_jar ||= Rack::Test::CookieJar.new([], @default_host) end end end rack-test-0.7.0/lib/rack/test/0000755000175000017500000000000013144300104015702 5ustar uwabamiuwabamirack-test-0.7.0/lib/rack/test/utils.rb0000644000175000017500000001040213144300104017364 0ustar uwabamiuwabamimodule Rack module Test module Utils # :nodoc: include Rack::Utils extend Rack::Utils def build_nested_query(value, prefix = nil) case value when Array if value.empty? "#{prefix}[]=" else value.map do |v| unless unescape(prefix) =~ /\[\]$/ prefix = "#{prefix}[]" end build_nested_query(v, "#{prefix}") 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 module_function :build_nested_query def build_multipart(params, first = true, multipart = false) if first unless params.is_a?(Hash) raise ArgumentError, "value must be a Hash" end 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 flattened_params = Hash.new 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 = {} build_multipart(v, false).each { |subkey, subvalue| nested_params[subkey] = subvalue } flattened_params["#{k}[]"] ||= [] flattened_params["#{k}[]"] << nested_params else flattened_params["#{k}[]"] = value end end when Hash build_multipart(value, false).each { |subkey, subvalue| flattened_params[k + subkey] = subvalue } else flattened_params[k] = value end end if first build_parts(flattened_params) else flattened_params end end module_function :build_multipart private def build_parts(parameters) get_parts(parameters).join + "--#{MULTIPART_BOUNDARY}--\r" end module_function :build_parts def get_parts(parameters) parameters.map { |name, value| if name =~ /\[\]\Z/ && value.is_a?(Array) && value.all? {|v| v.is_a?(Hash)} value.map { |hash| new_value = {} hash.each { |k, v| new_value[name+k] = v } get_parts(new_value).join }.join else if value.respond_to?(:original_filename) build_file_part(name, value) elsif value.is_a?(Array) and value.all? { |v| v.respond_to?(:original_filename) } value.map do |v| build_file_part(name, v) end.join else primitive_part = build_primitive_part(name, value) Rack::Test.encoding_aware_strings? ? primitive_part.force_encoding('BINARY') : primitive_part end end } end module_function :get_parts def build_primitive_part(parameter_name, value) unless value.is_a? Array value = [value] end value.map do |v| <<-EOF --#{MULTIPART_BOUNDARY}\r Content-Disposition: form-data; name="#{parameter_name}"\r \r #{v}\r EOF end.join end module_function :build_primitive_part def build_file_part(parameter_name, uploaded_file) ::File.open(uploaded_file.path, "rb") do |physical_file| physical_file.set_encoding(Encoding::BINARY) if physical_file.respond_to?(:set_encoding) <<-EOF --#{MULTIPART_BOUNDARY}\r Content-Disposition: form-data; name="#{parameter_name}"; filename="#{escape(uploaded_file.original_filename)}"\r Content-Type: #{uploaded_file.content_type}\r Content-Length: #{::File.stat(uploaded_file.path).size}\r \r #{physical_file.read}\r EOF end end module_function :build_file_part end end end rack-test-0.7.0/lib/rack/test/version.rb0000644000175000017500000000010113144300104017704 0ustar uwabamiuwabamimodule Rack module Test VERSION = '0.7.0'.freeze end end rack-test-0.7.0/lib/rack/test/uploaded_file.rb0000644000175000017500000000327713144300104021034 0ustar uwabamiuwabamirequire "tempfile" require "fileutils" 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 def initialize(path, content_type = "text/plain", binary = false) raise "#{path} file does not exist" unless ::File.exist?(path) @content_type = content_type @original_filename = ::File.basename(path) @tempfile = Tempfile.new([@original_filename, ::File.extname(path)]) @tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding) @tempfile.binmode if binary ObjectSpace.define_finalizer(self, self.class.finalize(@tempfile)) FileUtils.copy_file(path, @tempfile.path) end def path @tempfile.path end alias_method :local_path, :path def method_missing(method_name, *args, &block) #:nodoc: @tempfile.__send__(method_name, *args, &block) end def respond_to?(method_name, include_private = false) #:nodoc: @tempfile.respond_to?(method_name, include_private) || super end def self.finalize(file) proc { actually_finalize file } end def self.actually_finalize(file) file.close file.unlink end end end end rack-test-0.7.0/lib/rack/test/cookie_jar.rb0000644000175000017500000001101113144300104020326 0ustar uwabamiuwabamirequire "uri" require "time" module Rack module Test class Cookie # :nodoc: include Rack::Utils # :api: private attr_reader :name, :value # :api: private 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 @name_value_raw, options = raw.split(/[;,] */n, 2) @name, @value = parse_query(@name_value_raw, ';').to_a.first @options = parse_query(options, ';') @options["domain"] ||= (uri.host || default_host) @options["path"] ||= uri.path.sub(/\/[^\/]*\Z/, "") end def replaces?(other) [name.downcase, domain, path] == [other.name.downcase, other.domain, other.path] end # :api: private def raw @name_value_raw end # :api: private def empty? @value.nil? || @value.empty? end # :api: private def domain @options["domain"] end def secure? @options.has_key?("secure") end def http_only? @options.has_key?('HttpOnly') end # :api: private def path @options["path"].strip || "/" end # :api: private def expires Time.parse(@options["expires"]) if @options["expires"] end # :api: private def expired? expires && expires < Time.now end # :api: private def valid?(uri) uri ||= default_uri if uri.host.nil? uri.host = @default_host end real_domain = domain =~ /^\./ ? domain[1..-1] : domain (!secure? || (secure? && uri.scheme == "https")) && uri.host =~ Regexp.new("#{Regexp.escape(real_domain)}$", Regexp::IGNORECASE) && uri.path =~ Regexp.new("^#{Regexp.escape(path)}") end # :api: private def matches?(uri) ! expired? && valid?(uri) end # :api: private def <=>(other) # Orders the cookies from least specific to most [name, path, domain.reverse] <=> [other.name, other.path, other.domain.reverse] end def to_h @options.merge( 'value' => @value, 'HttpOnly' => http_only?, 'secure' => secure?, ) end alias_method :to_hash, :to_h protected def default_uri URI.parse("//" + @default_host + "/") end end class CookieJar # :nodoc: # :api: private def initialize(cookies = [], default_host = DEFAULT_HOST) @default_host = default_host @cookies = cookies @cookies.sort! end def [](name) cookies = hash_for(nil) # TODO: Should be case insensitive cookies[name.to_s] && cookies[name.to_s].value end def []=(name, value) merge("#{name}=#{Rack::Utils.escape(value)}") end def get_cookie(name) hash_for(nil).fetch(name,nil) end def delete(name) @cookies.reject! do |cookie| cookie.name == name end end 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!{|c| c.empty? } end raw_cookies.each do |raw_cookie| cookie = Cookie.new(raw_cookie, uri, @default_host) self << cookie if cookie.valid?(uri) end end def <<(new_cookie) @cookies.reject! do |existing_cookie| new_cookie.replaces?(existing_cookie) end @cookies << new_cookie @cookies.sort! end # :api: private def for(uri) hash_for(uri).values.map { |c| c.raw }.join(';') end def to_hash cookies = {} hash_for(nil).each do |name, cookie| cookies[name] = cookie.value end return cookies end protected def hash_for(uri = nil) cookies = {} # 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. @cookies.each do |cookie| cookies[cookie.name] = cookie if !uri || cookie.matches?(uri) end return cookies end end end end rack-test-0.7.0/lib/rack/test/methods.rb0000644000175000017500000000374713144300104017705 0ustar uwabamiuwabamirequire "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). # # Example: # # class HomepageTest < Test::Unit::TestCase # include Rack::Test::Methods # # def app # MyApp.new # end # end module Methods extend Forwardable def rack_mock_session(name = :default) # :nodoc: return build_rack_mock_session unless name @_rack_mock_sessions ||= {} @_rack_mock_sessions[name] ||= build_rack_mock_session end def build_rack_mock_session # :nodoc: Rack::MockSession.new(app) end 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 def build_rack_test_session(name) # :nodoc: Rack::Test::Session.new(rack_mock_session(name)) end def current_session # :nodoc: rack_test_session(_current_session_names.last) end def with_session(name) # :nodoc: _current_session_names.push(name) yield rack_test_session(name) _current_session_names.pop end def _current_session_names # :nodoc: @_current_session_names ||= [:default] end METHODS = [ :request, :get, :post, :put, :patch, :delete, :options, :head, :follow_redirect!, :header, :env, :set_cookie, :clear_cookies, :authorize, :basic_authorize, :digest_authorize, :last_response, :last_request ] def_delegators :current_session, *METHODS end end end rack-test-0.7.0/lib/rack/test/mock_digest_request.rb0000644000175000017500000000070313144300104022267 0ustar uwabamiuwabamimodule Rack module Test class MockDigestRequest # :nodoc: def initialize(params) @params = params end def method_missing(sym) if @params.has_key? k = sym.to_s return @params[k] end super end def method @params['method'] end def response(password) Rack::Auth::Digest::MD5.new(nil).send :digest, self, password end end end end rack-test-0.7.0/lib/rack/test.rb0000644000175000017500000002435113144300104016234 0ustar uwabamiuwabamirequire "uri" require "rack" require "rack/mock_session" require "rack/test/cookie_jar" require "rack/test/mock_digest_request" require "rack/test/utils" require "rack/test/methods" require "rack/test/uploaded_file" require "rack/test/version" module Rack module Test DEFAULT_HOST = "example.org" MULTIPART_BOUNDARY = "----------XnJLe9ZIbbGUYtzPQJ16u1" # The common base class for exceptions raised by Rack::Test class Error < StandardError; end # This class represents a series of requests issued to a Rack app, sharing # a single cookie jar # # 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_delegators :@rack_mock_session, :clear_cookies, :set_cookie, :last_response, :last_request # Creates a Rack::Test::Session for a given Rack app or Rack::MockSession. # # 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) def initialize(mock_session) @headers = {} @env = {} @digest_username = nil @digest_password = nil if mock_session.is_a?(MockSession) @rack_mock_session = mock_session else @rack_mock_session = MockSession.new(mock_session) end @default_host = @rack_mock_session.default_host end # Issue a GET request for the given URI with the given params and Rack # environment. Stores the issues request object in #last_request and # the app's response in #last_response. Yield #last_response to a block # if given. # # Example: # get "/" def get(uri, params = {}, env = {}, &block) env = env_for(uri, env.merge(:method => "GET", :params => params)) process_request(uri, env, &block) end # Issue a POST request for the given URI. See #get # # Example: # post "/signup", "name" => "Bryan" def post(uri, params = {}, env = {}, &block) env = env_for(uri, env.merge(:method => "POST", :params => params)) process_request(uri, env, &block) end # Issue a PUT request for the given URI. See #get # # Example: # put "/" def put(uri, params = {}, env = {}, &block) env = env_for(uri, env.merge(:method => "PUT", :params => params)) process_request(uri, env, &block) end # Issue a PATCH request for the given URI. See #get # # Example: # patch "/" def patch(uri, params = {}, env = {}, &block) env = env_for(uri, env.merge(:method => "PATCH", :params => params)) process_request(uri, env, &block) end # Issue a DELETE request for the given URI. See #get # # Example: # delete "/" def delete(uri, params = {}, env = {}, &block) env = env_for(uri, env.merge(:method => "DELETE", :params => params)) process_request(uri, env, &block) end # Issue an OPTIONS request for the given URI. See #get # # Example: # options "/" def options(uri, params = {}, env = {}, &block) env = env_for(uri, env.merge(:method => "OPTIONS", :params => params)) process_request(uri, env, &block) end # Issue a HEAD request for the given URI. See #get # # Example: # head "/" def head(uri, params = {}, env = {}, &block) env = env_for(uri, env.merge(:method => "HEAD", :params => params)) process_request(uri, env, &block) end # Issue a request to the Rack app for the given URI and optional Rack # environment. Stores the issues request object in #last_request and # the app's response in #last_response. Yield #last_response to a block # if given. # # Example: # request "/" def request(uri, env = {}, &block) env = env_for(uri, env) 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) if value.nil? @headers.delete(name) else @headers[name] = value end end # Set an env var to be included on all subsequent requests through the # session. Use a value of nil to remove a previously configured env. # # 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("m*") header('Authorization', "Basic #{encoded_login}") end alias_method :authorize, :basic_authorize # Set the username and password for HTTP Digest authorization, to be # included in subsequent requests in the HTTP_AUTHORIZATION header. # # Example: # digest_authorize "bryan", "secret" def digest_authorize(username, password) @digest_username = username @digest_password = password end # 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.new("Last response was not a redirect. Cannot follow_redirect!") end if last_response.status == 307 send(last_request.request_method.downcase.to_sym, last_response["Location"], last_request.params, { "HTTP_REFERER" => last_request.url }) else get(last_response["Location"], {}, { "HTTP_REFERER" => last_request.url }) end end private def env_for(path, env) uri = URI.parse(path) uri.path = "/#{uri.path}" unless uri.path[0] == ?/ uri.host ||= @default_host env = default_env.merge(env) env["HTTP_HOST"] ||= [uri.host, (uri.port if uri.port != uri.default_port)].compact.join(":") env.update("HTTPS" => "on") if URI::HTTPS === uri env["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" if env[:xhr] # TODO: Remove this after Rack 1.1 has been released. # Stringifying and upcasing methods has be commit upstream env["REQUEST_METHOD"] ||= env[:method] ? env[:method].to_s.upcase : "GET" if ["GET", "DELETE"].include?(env["REQUEST_METHOD"]) # merge :params with the query string if params = env[:params] params = parse_nested_query(params) if params.is_a?(String) uri.query = [uri.query, build_nested_query(params)].compact.reject { |v| v == '' }.join("&") end elsif !env.has_key?(:input) env["CONTENT_TYPE"] ||= "application/x-www-form-urlencoded" if env[:params].is_a?(Hash) if data = build_multipart(env[:params]) env[:input] = data env["CONTENT_LENGTH"] ||= data.length.to_s env["CONTENT_TYPE"] = "multipart/form-data; boundary=#{MULTIPART_BOUNDARY}" else env[:input] = params_to_string(env[:params]) end else env[:input] = env[:params] end end env.delete(:params) if env.has_key?(:cookie) set_cookie(env.delete(:cookie), uri) end Rack::MockRequest.env_for(uri.to_s, env) end def process_request(uri, env) uri = URI.parse(uri) uri.host ||= @default_host uri.scheme ||= "https" if env["HTTPS"] == "on" @rack_mock_session.request(uri, env) if retry_with_digest_auth?(env) auth_env = env.merge({ "HTTP_AUTHORIZATION" => digest_auth_header, "rack-test.digest_auth_retry" => true }) auth_env.delete('rack.request') process_request(uri.path, auth_env) else yield last_response if block_given? last_response end end def digest_auth_header challenge = last_response["WWW-Authenticate"].split(" ", 2).last params = Rack::Auth::Digest::Params.parse(challenge) params.merge!({ "username" => @digest_username, "nc" => "00000001", "cnonce" => "nonsensenonce", "uri" => last_request.fullpath, "method" => last_request.env["REQUEST_METHOD"], }) params["response"] = MockDigestRequest.new(params).response(@digest_password) "Digest #{params}" end def retry_with_digest_auth?(env) last_response.status == 401 && digest_auth_configured? && !env["rack-test.digest_auth_retry"] end def digest_auth_configured? @digest_username end def default_env { "rack.test" => true, "REMOTE_ADDR" => "127.0.0.1" }.merge(@env).merge(headers_for_env) end def headers_for_env converted_headers = {} @headers.each do |name, value| env_key = name.upcase.gsub("-", "_") env_key = "HTTP_" + env_key unless "CONTENT_TYPE" == env_key converted_headers[env_key] = value end converted_headers end def params_to_string(params) case params when Hash then build_nested_query(params) when nil then "" else params end end end def self.encoding_aware_strings? defined?(Encoding) && "".respond_to?(:encode) end end end rack-test-0.7.0/MIT-LICENSE.txt0000644000175000017500000000207113144300104015507 0ustar uwabamiuwabamiCopyright (c) 2008-2009 Bryan Helmkamp, Engine Yard Inc. 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-0.7.0/History.md0000644000175000017500000001645013144300104015226 0ustar uwabamiuwabami## 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-0.7.0/rack-test.gemspec0000644000175000017500000000526713144300104016511 0ustar uwabamiuwabami######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- Gem::Specification.new do |s| s.name = "rack-test" s.version = "0.7.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Bryan Helmkamp"] s.date = "2017-07-10" s.description = "Rack::Test is a small, simple testing API for Rack apps. It can be used on its\nown or as a reusable starting point for Web frameworks and testing libraries\nto build on. Most of its initial functionality is an extraction of Merb 1.0's\nrequest helpers feature." s.email = "bryan@brynary.com" s.files = ["History.md", "MIT-LICENSE.txt", "README.md", "lib/rack/mock_session.rb", "lib/rack/test.rb", "lib/rack/test/cookie_jar.rb", "lib/rack/test/methods.rb", "lib/rack/test/mock_digest_request.rb", "lib/rack/test/uploaded_file.rb", "lib/rack/test/utils.rb", "lib/rack/test/version.rb"] s.homepage = "http://github.com/rack-test/rack-test" s.licenses = ["MIT"] s.require_paths = ["lib"] s.rubygems_version = "1.8.23" s.summary = "Simple testing API built on Rack" if s.respond_to? :specification_version then s.specification_version = 4 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_development_dependency(%q, ["~> 0.6"]) s.add_runtime_dependency(%q, ["< 3", ">= 1.0"]) s.add_development_dependency(%q, ["~> 12.0"]) s.add_development_dependency(%q, ["~> 5.1"]) s.add_development_dependency(%q, ["~> 3.6"]) s.add_development_dependency(%q, ["< 0.50", ">= 0.49"]) s.add_development_dependency(%q, ["< 3", ">= 1.0"]) s.add_development_dependency(%q, ["~> 0.19"]) else s.add_dependency(%q, ["~> 0.6"]) s.add_dependency(%q, ["< 3", ">= 1.0"]) s.add_dependency(%q, ["~> 12.0"]) s.add_dependency(%q, ["~> 5.1"]) s.add_dependency(%q, ["~> 3.6"]) s.add_dependency(%q, ["< 0.50", ">= 0.49"]) s.add_dependency(%q, ["< 3", ">= 1.0"]) s.add_dependency(%q, ["~> 0.19"]) end else s.add_dependency(%q, ["~> 0.6"]) s.add_dependency(%q, ["< 3", ">= 1.0"]) s.add_dependency(%q, ["~> 12.0"]) s.add_dependency(%q, ["~> 5.1"]) s.add_dependency(%q, ["~> 3.6"]) s.add_dependency(%q, ["< 0.50", ">= 0.49"]) s.add_dependency(%q, ["< 3", ">= 1.0"]) s.add_dependency(%q, ["~> 0.19"]) end end rack-test-0.7.0/README.md0000644000175000017500000000534413144300104014522 0ustar uwabamiuwabami# Rack::Test [](https://travis-ci.org/rack-test/rack-test) [](https://codeclimate.com/github/rack-test/rack-test) [](https://codeclimate.com/github/rack-test/rack-test) Code: https://github.com/rack-test/rack-test ## Description Rack::Test is a small, simple testing API for Rack apps. It can be used on its own or as a reusable starting point for Web frameworks and testing libraries to build on. ## Features * Maintains a cookie jar across requests * Easily follow redirects when desired * Set request headers to be used by all subsequent requests * Small footprint. Approximately 200 LOC ## Examples ```ruby require "test/unit" require "rack/test" class HomepageTest < Test::Unit::TestCase include Rack::Test::Methods def app app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['All responses are OK']] } builder = Rack::Builder.new builder.run app end def test_response_is_ok get "/" assert last_response.ok? assert_equal last_response.body, "All responses are OK" end def set_request_headers headers 'Accept-Charset', 'utf-8' get "/" assert last_response.ok? assert_equal last_response.body, "All responses are OK" end def test_response_is_ok_for_other_paths get "/other_paths" assert last_response.ok? assert_equal last_response.body, "All responses are OK" end end ``` If you want to test one app in isolation, you just return that app as shown above. But if you want to test the entire app stack, including middlewares, cascades etc. you need to parse the app defined in config.ru. ```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 via Bundler: `gem "rack-test", require: "rack/test"` ## Authors - Contributions from Bryan Helmkamp, Simon Rozet, Pat Nakajima and others - Much of the original code was extracted from Merb 1.0's request helper ## License `rack-test` is released under the [MIT License](MIT-LICENSE.txt). ## 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.txt is updated ## Releasing * Ensure History.txt is up-to-date * Bump VERSION in lib/rack/test/version.rb * bundle exec thor :release