lib/rack/mock.rb in rack-2.1.4.4 vs lib/rack/mock.rb in rack-2.2.0

- old
+ new

@@ -1,13 +1,10 @@ # frozen_string_literal: true require 'uri' require 'stringio' -require 'rack' -require 'rack/lint' -require 'rack/utils' -require 'rack/response' +require_relative '../rack' require 'cgi/cookie' module Rack # Rack::MockRequest helps testing your Rack application without # actually using HTTP. @@ -54,18 +51,28 @@ def initialize(app) @app = app end + # Make a GET request and return a MockResponse. See #request. def get(uri, opts = {}) request(GET, uri, opts) end + # Make a POST request and return a MockResponse. See #request. def post(uri, opts = {}) request(POST, uri, opts) end + # Make a PUT request and return a MockResponse. See #request. def put(uri, opts = {}) request(PUT, uri, opts) end + # Make a PATCH request and return a MockResponse. See #request. def patch(uri, opts = {}) request(PATCH, uri, opts) end + # Make a DELETE request and return a MockResponse. See #request. def delete(uri, opts = {}) request(DELETE, uri, opts) end + # Make a HEAD request and return a MockResponse. See #request. def head(uri, opts = {}) request(HEAD, uri, opts) end + # Make an OPTIONS request and return a MockResponse. See #request. def options(uri, opts = {}) request(OPTIONS, uri, opts) end + # Make a request using the given request method for the given + # uri to the rack application and return a MockResponse. + # Options given are passed to MockRequest.env_for. def request(method = GET, uri = "", opts = {}) env = self.class.env_for(uri, opts.merge(method: method)) if opts[:lint] app = Rack::Lint.new(@app) @@ -86,10 +93,17 @@ @parser ||= URI::Parser.new @parser.parse(uri) end # Return the Rack environment used for a request to +uri+. + # All options that are strings are added to the returned environment. + # Options: + # :fatal :: Whether to raise an exception if request outputs to rack.errors + # :input :: The rack.input to set + # :method :: The HTTP request method to use + # :params :: The params to use + # :script_name :: The SCRIPT_NAME to set def self.env_for(uri = "", opts = {}) uri = parse_uri_rfc2396(uri) uri.path = "/#{uri.path}" unless uri.path[0] == ?/ env = DEFAULT_ENV.dup @@ -155,9 +169,13 @@ # Rack::MockResponse provides useful helpers for testing your apps. # Usually, you don't create the MockResponse on your own, but use # MockRequest. class MockResponse < Rack::Response + class << self + alias [] new + end + # Headers attr_reader :original_headers, :cookies # Errors attr_accessor :errors