lib/rack/mock.rb in rack-1.6.13 vs lib/rack/mock.rb in rack-2.0.0.alpha
- old
+ new
@@ -39,131 +39,114 @@
""
end
end
DEFAULT_ENV = {
- "rack.version" => Rack::VERSION,
- "rack.input" => StringIO.new,
- "rack.errors" => StringIO.new,
- "rack.multithread" => true,
- "rack.multiprocess" => true,
- "rack.run_once" => false,
- }
+ RACK_VERSION => Rack::VERSION,
+ RACK_INPUT => StringIO.new,
+ RACK_ERRORS => StringIO.new,
+ RACK_MULTITHREAD => true,
+ RACK_MULTIPROCESS => true,
+ RACK_RUNONCE => false,
+ }.freeze
def initialize(app)
@app = app
end
- def get(uri, opts={}) request("GET", uri, opts) end
- def post(uri, opts={}) request("POST", uri, opts) end
- def put(uri, opts={}) request("PUT", uri, opts) end
- def patch(uri, opts={}) request("PATCH", uri, opts) end
- def delete(uri, opts={}) request("DELETE", uri, opts) end
- def head(uri, opts={}) request("HEAD", uri, opts) end
- def options(uri, opts={}) request("OPTIONS", uri, opts) end
+ def get(uri, opts={}) request(GET, uri, opts) end
+ def post(uri, opts={}) request(POST, uri, opts) end
+ def put(uri, opts={}) request(PUT, uri, opts) end
+ def patch(uri, opts={}) request(PATCH, uri, opts) end
+ def delete(uri, opts={}) request(DELETE, uri, opts) end
+ def head(uri, opts={}) request(HEAD, uri, opts) end
+ def options(uri, opts={}) request(OPTIONS, uri, opts) end
- def request(method="GET", uri="", opts={})
+ def request(method=GET, uri="", opts={})
env = self.class.env_for(uri, opts.merge(:method => method))
if opts[:lint]
app = Rack::Lint.new(@app)
else
app = @app
end
- errors = env["rack.errors"]
+ errors = env[RACK_ERRORS]
status, headers, body = app.call(env)
MockResponse.new(status, headers, body, errors)
ensure
body.close if body.respond_to?(:close)
end
- # For historical reasons, we're pinning to RFC 2396. It's easier for users
- # and we get support from ruby 1.8 to 2.2 using this method.
+ # For historical reasons, we're pinning to RFC 2396.
+ # URI::Parser = URI::RFC2396_Parser
def self.parse_uri_rfc2396(uri)
- @parser ||= defined?(URI::RFC2396_Parser) ? URI::RFC2396_Parser.new : URI
+ @parser ||= URI::Parser.new
@parser.parse(uri)
end
# Return the Rack environment used for a request to +uri+.
def self.env_for(uri="", opts={})
uri = parse_uri_rfc2396(uri)
uri.path = "/#{uri.path}" unless uri.path[0] == ?/
env = DEFAULT_ENV.dup
- env_with_encoding(env, opts, uri)
+ env[REQUEST_METHOD] = opts[:method] ? opts[:method].to_s.upcase : GET
+ env[SERVER_NAME] = uri.host || "example.org"
+ env[SERVER_PORT] = uri.port ? uri.port.to_s : "80"
+ env[QUERY_STRING] = uri.query.to_s
+ env[PATH_INFO] = (!uri.path || uri.path.empty?) ? "/" : uri.path
+ env[RACK_URL_SCHEME] = uri.scheme || "http"
+ env[HTTPS] = env[RACK_URL_SCHEME] == "https" ? "on" : "off"
env[SCRIPT_NAME] = opts[:script_name] || ""
if opts[:fatal]
- env["rack.errors"] = FatalWarner.new
+ env[RACK_ERRORS] = FatalWarner.new
else
- env["rack.errors"] = StringIO.new
+ env[RACK_ERRORS] = StringIO.new
end
if params = opts[:params]
- if env[REQUEST_METHOD] == "GET"
+ if env[REQUEST_METHOD] == GET
params = Utils.parse_nested_query(params) if params.is_a?(String)
params.update(Utils.parse_nested_query(env[QUERY_STRING]))
env[QUERY_STRING] = Utils.build_nested_query(params)
elsif !opts.has_key?(:input)
opts["CONTENT_TYPE"] = "application/x-www-form-urlencoded"
if params.is_a?(Hash)
- if data = Utils::Multipart.build_multipart(params)
+ if data = Rack::Multipart.build_multipart(params)
opts[:input] = data
opts["CONTENT_LENGTH"] ||= data.length.to_s
- opts["CONTENT_TYPE"] = "multipart/form-data; boundary=#{Utils::Multipart::MULTIPART_BOUNDARY}"
+ opts["CONTENT_TYPE"] = "multipart/form-data; boundary=#{Rack::Multipart::MULTIPART_BOUNDARY}"
else
opts[:input] = Utils.build_nested_query(params)
end
else
opts[:input] = params
end
end
end
- empty_str = ""
- empty_str.force_encoding("ASCII-8BIT") if empty_str.respond_to? :force_encoding
+ empty_str = ''.force_encoding(Encoding::ASCII_8BIT)
opts[:input] ||= empty_str
if String === opts[:input]
rack_input = StringIO.new(opts[:input])
else
rack_input = opts[:input]
end
- rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding)
- env['rack.input'] = rack_input
+ rack_input.set_encoding(Encoding::BINARY)
+ env[RACK_INPUT] = rack_input
- env["CONTENT_LENGTH"] ||= env["rack.input"].length.to_s
+ env["CONTENT_LENGTH"] ||= env[RACK_INPUT].length.to_s
opts.each { |field, value|
env[field] = value if String === field
}
env
- end
-
- if "<3".respond_to? :b
- def self.env_with_encoding(env, opts, uri)
- env[REQUEST_METHOD] = (opts[:method] ? opts[:method].to_s.upcase : "GET").b
- env["SERVER_NAME"] = (uri.host || "example.org").b
- env["SERVER_PORT"] = (uri.port ? uri.port.to_s : "80").b
- env[QUERY_STRING] = (uri.query.to_s).b
- env[PATH_INFO] = ((!uri.path || uri.path.empty?) ? "/" : uri.path).b
- env["rack.url_scheme"] = (uri.scheme || "http").b
- env["HTTPS"] = (env["rack.url_scheme"] == "https" ? "on" : "off").b
- end
- else
- def self.env_with_encoding(env, opts, uri)
- env[REQUEST_METHOD] = opts[:method] ? opts[:method].to_s.upcase : "GET"
- env["SERVER_NAME"] = uri.host || "example.org"
- env["SERVER_PORT"] = uri.port ? uri.port.to_s : "80"
- env[QUERY_STRING] = uri.query.to_s
- env[PATH_INFO] = (!uri.path || uri.path.empty?) ? "/" : uri.path
- env["rack.url_scheme"] = uri.scheme || "http"
- env["HTTPS"] = env["rack.url_scheme"] == "https" ? "on" : "off"
- end
end
end
# Rack::MockResponse provides useful helpers for testing your apps.
# Usually, you don't create the MockResponse on your own, but use