diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-08-03 16:03:30 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-08-03 16:03:30 -0500 |
commit | 6ae0a1014c2bfc06ad4d91ea04da5c42be454079 (patch) | |
tree | 8d820e725d0f6e419a6a988c9e5c048e9bd9fce0 /lib/rack/mock.rb | |
parent | 39fec318fbe5bda17eab4aa3d35c2ed30a36583e (diff) | |
download | rack-6ae0a1014c2bfc06ad4d91ea04da5c42be454079.tar.gz |
Add common HTTP strings to Rack::Const
Diffstat (limited to 'lib/rack/mock.rb')
-rw-r--r-- | lib/rack/mock.rb | 61 |
1 files changed, 31 insertions, 30 deletions
diff --git a/lib/rack/mock.rb b/lib/rack/mock.rb index 7964c447..a4f89dd9 100644 --- a/lib/rack/mock.rb +++ b/lib/rack/mock.rb @@ -1,5 +1,6 @@ require 'uri' require 'stringio' +require 'rack/constants' require 'rack/lint' require 'rack/utils' require 'rack/response' @@ -40,24 +41,24 @@ module Rack end DEFAULT_ENV = { - "rack.version" => [1,0], - "rack.input" => StringIO.new, - "rack.errors" => StringIO.new, - "rack.multithread" => true, - "rack.multiprocess" => true, - "rack.run_once" => false, + Const::RACK_VERSION => [1,0], + Const::RACK_INPUT => StringIO.new, + Const::RACK_ERRORS => StringIO.new, + Const::RACK_MULTITHREAD => true, + Const::RACK_MULTIPROCESS => true, + Const::RACK_RUN_ONCE => false, } 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 delete(uri, opts={}) request("DELETE", uri, opts) end + def get(uri, opts={}) request(Const::GET, uri, opts) end + def post(uri, opts={}) request(Const::POST, uri, opts) end + def put(uri, opts={}) request(Const::PUT, uri, opts) end + def delete(uri, opts={}) request(Const::DELETE, uri, opts) end - def request(method="GET", uri="", opts={}) + def request(method=Const::GET, uri="", opts={}) env = self.class.env_for(uri, opts.merge(:method => method)) if opts[:lint] @@ -66,7 +67,7 @@ module Rack app = @app end - errors = env["rack.errors"] + errors = env[Const::RACK_ERRORS] MockResponse.new(*(app.call(env) + [errors])) end @@ -77,34 +78,34 @@ module Rack env = DEFAULT_ENV.dup - 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[Const::ENV_REQUEST_METHOD] = opts[:method] ? opts[:method].to_s.upcase : Const::GET + env[Const::ENV_SERVER_NAME] = uri.host || "example.org" + env[Const::ENV_SERVER_PORT] = uri.port ? uri.port.to_s : "80" + env[Const::ENV_QUERY_STRING] = uri.query.to_s + env[Const::ENV_PATH_INFO] = (!uri.path || uri.path.empty?) ? "/" : uri.path + env[Const::RACK_URL_SCHEME] = uri.scheme || "http" + env[Const::ENV_HTTPS] = env[Const::RACK_URL_SCHEME] == "https" ? "on" : "off" - env["SCRIPT_NAME"] = opts[:script_name] || "" + env[Const::ENV_SCRIPT_NAME] = opts[:script_name] || "" if opts[:fatal] - env["rack.errors"] = FatalWarner.new + env[Const::RACK_ERRORS] = FatalWarner.new else - env["rack.errors"] = StringIO.new + env[Const::RACK_ERRORS] = StringIO.new end if params = opts[:params] - if env["REQUEST_METHOD"] == "GET" + if env[Const::ENV_REQUEST_METHOD] == Const::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) + params.update(Utils.parse_nested_query(env[Const::ENV_QUERY_STRING])) + env[Const::ENV_QUERY_STRING] = Utils.build_nested_query(params) elsif !opts.has_key?(:input) - opts["CONTENT_TYPE"] = "application/x-www-form-urlencoded" + opts[Const::ENV_CONTENT_TYPE] = "application/x-www-form-urlencoded" if params.is_a?(Hash) if data = Utils::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[Const::ENV_CONTENT_LENGTH] ||= data.length.to_s + opts[Const::ENV_CONTENT_TYPE] = "multipart/form-data; boundary=#{Utils::Multipart::MULTIPART_BOUNDARY}" else opts[:input] = Utils.build_nested_query(params) end @@ -124,9 +125,9 @@ module Rack end rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding) - env['rack.input'] = rack_input + env[Const::RACK_INPUT] = rack_input - env["CONTENT_LENGTH"] ||= env["rack.input"].length.to_s + env[Const::ENV_CONTENT_LENGTH] ||= env[Const::RACK_INPUT].length.to_s opts.each { |field, value| env[field] = value if String === field |