summary refs log tree commit
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2017-05-12 14:57:06 -0400
committereileencodes <eileencodes@gmail.com>2017-05-12 14:57:06 -0400
commitbd9bf52fa431c412c0ccc4e824e739c0d49b3514 (patch)
treec27e1cd7b3de9e1f7d34102f26a3cd8f68b76426
parenta0557f70d44f07ec2c651945d66102cf973c916f (diff)
downloadrack-bd9bf52fa431c412c0ccc4e824e739c0d49b3514.tar.gz
Ensure env values are ASCII 8BIT encoded
When web servers read data from the socket it's encoded as ASCII_8BIT
because we don't know the encoding. This change makes the env closer to
what a real web server will return in production.
-rw-r--r--lib/rack/mock.rb14
-rw-r--r--test/spec_mock.rb17
2 files changed, 24 insertions, 7 deletions
diff --git a/lib/rack/mock.rb b/lib/rack/mock.rb
index f3321772..10070f11 100644
--- a/lib/rack/mock.rb
+++ b/lib/rack/mock.rb
@@ -91,13 +91,13 @@ 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[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
 
       env[SCRIPT_NAME] = opts[:script_name] || ""
 
diff --git a/test/spec_mock.rb b/test/spec_mock.rb
index a4d4e5a5..e383f203 100644
--- a/test/spec_mock.rb
+++ b/test/spec_mock.rb
@@ -211,6 +211,23 @@ describe Rack::MockRequest do
     Rack::MockRequest.new(capp).get('/', :lint => true)
     called.must_equal true
   end
+
+  it "defaults encoding to ASCII 8BIT" do
+    req = Rack::MockRequest.env_for("/foo")
+
+    keys = [
+        Rack::REQUEST_METHOD,
+        Rack::SERVER_NAME,
+        Rack::SERVER_PORT,
+        Rack::QUERY_STRING,
+        Rack::PATH_INFO,
+        Rack::HTTPS,
+        Rack::RACK_URL_SCHEME
+    ]
+    keys.each do |k|
+      assert_equal Encoding::ASCII_8BIT, req[k].encoding
+    end
+  end
 end
 
 describe Rack::MockResponse do