Skip to content

Commit

Permalink
Make default MockRequest input body nil since it is now optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jan 13, 2023
1 parent ba399ff commit 10aa25a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
4 changes: 0 additions & 4 deletions lib/rack/mock_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ def self.env_for(uri = "", opts = {})
end
end

unless opts.key?(:input)
opts[:input] = String.new
end

if String === opts[:input]
rack_input = StringIO.new(opts[:input])
else
Expand Down
8 changes: 6 additions & 2 deletions test/spec_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
[200, { "content-type" => "test/plain", "content-length" => "3" }, ["foo"]]
end

def env(*args)
Rack::MockRequest.env_for("/", *args)
def env(options = {})
unless options.key?(:input)
options[:input] = String.new
end

Rack::MockRequest.env_for("/", options)
end

it "pass valid request" do
Expand Down
11 changes: 7 additions & 4 deletions test/spec_mock_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
app = Rack::Lint.new(lambda { |env|
req = Rack::Request.new(env)

env["mock.postdata"] = env["rack.input"].read
if input = env["rack.input"]
env["mock.postdata"] = input.read
end

if req.GET["error"]
env["rack.errors"].puts req.GET["error"]
env["rack.errors"].flush
Expand Down Expand Up @@ -71,7 +74,7 @@
env["PATH_INFO"].must_equal "/"
env["SCRIPT_NAME"].must_equal ""
env["rack.url_scheme"].must_equal "http"
env["mock.postdata"].must_be :empty?
env["mock.postdata"].must_be_nil
end

it "allow GET/POST/PUT/DELETE/HEAD" do
Expand Down Expand Up @@ -194,7 +197,7 @@
env["QUERY_STRING"].must_include "baz=2"
env["QUERY_STRING"].must_include "foo%5Bbar%5D=1"
env["PATH_INFO"].must_equal "/foo"
env["mock.postdata"].must_equal ""
env["mock.postdata"].must_be_nil
end

it "accept raw input in params for GET requests" do
Expand All @@ -204,7 +207,7 @@
env["QUERY_STRING"].must_include "baz=2"
env["QUERY_STRING"].must_include "foo%5Bbar%5D=1"
env["PATH_INFO"].must_equal "/foo"
env["mock.postdata"].must_equal ""
env["mock.postdata"].must_be_nil
end

it "accept params and build url encoded params for POST requests" do
Expand Down
5 changes: 4 additions & 1 deletion test/spec_mock_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
app = Rack::Lint.new(lambda { |env|
req = Rack::Request.new(env)

env["mock.postdata"] = env["rack.input"].read
if input = env["rack.input"]
env["mock.postdata"] = input.read
end

if req.GET["error"]
env["rack.errors"].puts req.GET["error"]
env["rack.errors"].flush
Expand Down
3 changes: 1 addition & 2 deletions test/spec_multipart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def multipart_file(name)
end

it "return nil if content type is not multipart" do
env = Rack::MockRequest.env_for("/",
"CONTENT_TYPE" => 'application/x-www-form-urlencoded')
env = Rack::MockRequest.env_for("/", "CONTENT_TYPE" => 'application/x-www-form-urlencoded', :input => "")
Rack::Multipart.parse_multipart(env).must_be_nil
end

Expand Down
4 changes: 2 additions & 2 deletions test/spec_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class RackRequestTest < Minitest::Spec
it "wrap the rack variables" do
req = make_request(Rack::MockRequest.env_for("http://example.com:8080/"))

req.body.must_respond_to :gets
req.body.must_be_nil
req.scheme.must_equal "http"
req.request_method.must_equal "GET"

Expand All @@ -131,7 +131,7 @@ class RackRequestTest < Minitest::Spec
req.host.must_equal "example.com"
req.port.must_equal 8080

req.content_length.must_equal "0"
req.content_length.must_be_nil
req.content_type.must_be_nil
end

Expand Down

0 comments on commit 10aa25a

Please sign in to comment.