diff options
author | Jeremy Evans <code@jeremyevans.net> | 2020-01-28 17:23:54 -0800 |
---|---|---|
committer | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2020-01-30 12:26:19 +1300 |
commit | 8539124842df0212307287c4e381ff25f4719ca8 (patch) | |
tree | 4771c28f183d925f257c3ae02956e6b980a06832 | |
parent | 022815d3264781bd9cbd4d8d514998baabbc3edd (diff) | |
download | rack-8539124842df0212307287c4e381ff25f4719ca8.tar.gz |
More covering tests
3352 / 3378 LOC (99.23%) covered
-rw-r--r-- | lib/rack/utils.rb | 2 | ||||
-rw-r--r-- | test/spec_lint.rb | 16 | ||||
-rw-r--r-- | test/spec_mock.rb | 7 | ||||
-rw-r--r-- | test/spec_request.rb | 39 | ||||
-rw-r--r-- | test/spec_sendfile.rb | 23 | ||||
-rw-r--r-- | test/spec_server.rb | 14 | ||||
-rw-r--r-- | test/spec_utils.rb | 26 |
7 files changed, 117 insertions, 10 deletions
diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb index 782a669a..2e1e7971 100644 --- a/lib/rack/utils.rb +++ b/lib/rack/utils.rb @@ -84,9 +84,11 @@ module Rack Process.clock_gettime(Process::CLOCK_MONOTONIC) end else + # :nocov: def clock_time Time.now.to_f end + # :nocov: end def parse_query(qs, d = nil, &unescaper) diff --git a/test/spec_lint.rb b/test/spec_lint.rb index d55dfa70..7dec47cd 100644 --- a/test/spec_lint.rb +++ b/test/spec_lint.rb @@ -151,7 +151,15 @@ describe Rack::Lint do message.must_equal "rack.multipart.tempfile_factory return value must respond to #<<" lambda { - Rack::Lint.new(nil).call(env("REQUEST_METHOD" => "FUCKUP?", "rack.multipart.tempfile_factory" => lambda { |filename, content_type| String.new })) + Rack::Lint.new(lambda { |env| + env['rack.multipart.tempfile_factory'].call("testfile", "text/plain") + [] + }).call(env("rack.multipart.tempfile_factory" => lambda { |filename, content_type| String.new })) + }.must_raise(Rack::Lint::LintError). + message.must_equal "response array [] has 0 elements instead of 3" + + lambda { + Rack::Lint.new(nil).call(env("REQUEST_METHOD" => "FUCKUP?")) }.must_raise(Rack::Lint::LintError). message.must_match(/REQUEST_METHOD/) @@ -412,6 +420,7 @@ describe Rack::Lint do lambda { Rack::Lint.new(lambda { |env| + env["rack.input"].gets env["rack.input"].read(1, 2, 3) [201, { "Content-type" => "text/plain", "Content-length" => "0" }, []] }).call(env({})) @@ -610,6 +619,11 @@ describe Rack::Lint do [201, { "Content-type" => "text/plain", "Content-length" => "0" }, []] }).call(env({'rack.hijack?' => true, 'rack.hijack' => lambda { StringIO.new }, 'rack.hijack_io' => StringIO.new })). first.must_equal 201 + + Rack::Lint.new(lambda { |env| + env['rack.hijack?'] = true + [201, { "Content-type" => "text/plain", "Content-length" => "0", 'rack.hijack' => lambda { StringIO.new }, 'rack.hijack_io' => StringIO.new }, []] + }).call(env({}))[1]['rack.hijack'].call.read.must_equal '' end end diff --git a/test/spec_mock.rb b/test/spec_mock.rb index c639436d..d2311f5a 100644 --- a/test/spec_mock.rb +++ b/test/spec_mock.rb @@ -328,6 +328,9 @@ describe Rack::MockResponse do res = Rack::MockRequest.new(app).get("") res.body.must_match(/rack/) assert_match(res, /rack/) + + res.match('rack')[0].must_equal 'rack' + res.match('banana').must_be_nil end it "provide access to the Rack errors" do @@ -349,6 +352,10 @@ describe Rack::MockResponse do lambda { Rack::MockRequest.new(app).get("/?error=foo", fatal: true) }.must_raise Rack::MockRequest::FatalWarning + + lambda { + Rack::MockRequest.new(lambda { |env| env['rack.errors'].write(env['rack.errors'].string) }).get("/", fatal: true) + }.must_raise(Rack::MockRequest::FatalWarning).message.must_equal '' end end diff --git a/test/spec_request.rb b/test/spec_request.rb index 81092f75..e484a577 100644 --- a/test/spec_request.rb +++ b/test/spec_request.rb @@ -414,6 +414,7 @@ class RackRequestTest < Minitest::Spec req.content_type.must_equal 'text/plain;charset=utf-8' req.media_type.must_equal 'text/plain' req.media_type_params['charset'].must_equal 'utf-8' + req.content_charset.must_equal 'utf-8' req.POST.must_be :empty? req.params.must_equal "foo" => "quux" req.body.read.must_equal "foo=bar&quux=bla" @@ -460,11 +461,35 @@ class RackRequestTest < Minitest::Spec req.POST.must_equal "foo" => "bar", "quux" => "bla" end + it "have params only return GET if POST cannot be processed" do + obj = Object.new + def obj.read(*) raise EOFError end + def obj.set_encoding(*) end + def obj.rewind(*) end + req = make_request Rack::MockRequest.env_for("/", 'REQUEST_METHOD' => 'POST', :input => obj) + req.params.must_equal req.GET + req.params.wont_be_same_as req.GET + end + it "get value by key from params with #[]" do req = make_request \ Rack::MockRequest.env_for("?foo=quux") req['foo'].must_equal 'quux' req[:foo].must_equal 'quux' + + next if self.class == TestProxyRequest + verbose = $VERBOSE + warn_arg = nil + req.define_singleton_method(:warn) do |arg| + warn_arg = arg + end + begin + $VERBOSE = true + req['foo'].must_equal 'quux' + warn_arg.must_equal "Request#[] is deprecated and will be removed in a future version of Rack. Please use request.params[] instead" + ensure + $VERBOSE = verbose + end end it "set value to key on params with #[]=" do @@ -487,6 +512,20 @@ class RackRequestTest < Minitest::Spec req.params.must_equal 'foo' => 'jaz' req['foo'].must_equal 'jaz' req[:foo].must_equal 'jaz' + + verbose = $VERBOSE + warn_arg = nil + req.define_singleton_method(:warn) do |arg| + warn_arg = arg + end + begin + $VERBOSE = true + req['foo'] = 'quux' + warn_arg.must_equal "Request#[]= is deprecated and will be removed in a future version of Rack. Please use request.params[]= instead" + req.params['foo'].must_equal 'quux' + ensure + $VERBOSE = verbose + end end it "return values for the keys in the order given from values_at" do diff --git a/test/spec_sendfile.rb b/test/spec_sendfile.rb index a20aacf5..09e810e9 100644 --- a/test/spec_sendfile.rb +++ b/test/spec_sendfile.rb @@ -40,6 +40,18 @@ describe Rack::Sendfile do end end + it "does nothing and logs to rack.errors when incorrect X-Sendfile-Type header present" do + io = StringIO.new + request 'HTTP_X_SENDFILE_TYPE' => 'X-Banana', 'rack.errors' => io do |response| + response.must_be :ok? + response.body.must_equal 'Hello World' + response.headers.wont_include 'X-Sendfile' + + io.rewind + io.read.must_equal "Unknown x-sendfile variation: 'X-Banana'.\n" + end + end + it "sets X-Sendfile response header and discards body" do request 'HTTP_X_SENDFILE_TYPE' => 'X-Sendfile' do |response| response.must_be :ok? @@ -139,6 +151,7 @@ describe Rack::Sendfile do begin dir1 = Dir.mktmpdir dir2 = Dir.mktmpdir + dir3 = Dir.mktmpdir first_body = open_file(File.join(dir1, 'rack_sendfile')) first_body.puts 'hello world' @@ -146,6 +159,9 @@ describe Rack::Sendfile do second_body = open_file(File.join(dir2, 'rack_sendfile')) second_body.puts 'goodbye world' + third_body = open_file(File.join(dir3, 'rack_sendfile')) + third_body.puts 'hello again world' + headers = { 'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect', 'HTTP_X_ACCEL_MAPPING' => "#{dir1}/=/foo/bar/, #{dir2}/=/wibble/" @@ -164,6 +180,13 @@ describe Rack::Sendfile do response.headers['Content-Length'].must_equal '0' response.headers['X-Accel-Redirect'].must_equal '/wibble/rack_sendfile' end + + request(headers, third_body) do |response| + response.must_be :ok? + response.body.must_be :empty? + response.headers['Content-Length'].must_equal '0' + response.headers['X-Accel-Redirect'].must_equal "#{dir3}/rack_sendfile" + end ensure FileUtils.remove_entry_secure dir1 FileUtils.remove_entry_secure dir2 diff --git a/test/spec_server.rb b/test/spec_server.rb index dab51199..dc9e7337 100644 --- a/test/spec_server.rb +++ b/test/spec_server.rb @@ -174,17 +174,13 @@ describe Rack::Server do end it "support -b option to specify inline rackup config" do - SPEC_ARGV[0..-1] = ['-scgi', '-E', 'production', '-b', 'use Rack::ContentLength; run ->(env){[200, {}, []]}'] + SPEC_ARGV[0..-1] = ['-scgi', '-E', 'development', '-b', 'use Rack::ContentLength; run ->(env){[200, {}, []]}'] server = Rack::Server.new def (server.server).run(app, **) app end - s, h, b = server.start.call({}) - s.must_equal 200 - h.must_equal 'Content-Length' => '0' - body = String.new - b.each do |s| - body << s - end - body.must_equal '' + s, h, b = server.start.call('rack.errors' => StringIO.new) + s.must_equal 500 + h['Content-Type'].must_equal 'text/plain' + b.join.must_include 'Rack::Lint::LintError' end it "support -e option to evaluate ruby code" do diff --git a/test/spec_utils.rb b/test/spec_utils.rb index 88ad64f5..5064be43 100644 --- a/test/spec_utils.rb +++ b/test/spec_utils.rb @@ -546,6 +546,30 @@ describe Rack::Utils, "cookies" do Rack::Utils.add_cookie_to_header(Object.new, 'name', 'value') }.must_raise ArgumentError end + + it "sets and deletes cookies in header hash" do + header = {'Set-Cookie'=>''} + Rack::Utils.set_cookie_header!(header, 'name', 'value').must_be_nil + header['Set-Cookie'].must_equal 'name=value' + Rack::Utils.set_cookie_header!(header, 'name2', 'value2').must_be_nil + header['Set-Cookie'].must_equal "name=value\nname2=value2" + Rack::Utils.set_cookie_header!(header, 'name2', 'value3').must_be_nil + header['Set-Cookie'].must_equal "name=value\nname2=value2\nname2=value3" + + Rack::Utils.delete_cookie_header!(header, 'name2').must_be_nil + header['Set-Cookie'].must_equal "name=value\nname2=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT" + Rack::Utils.delete_cookie_header!(header, 'name').must_be_nil + header['Set-Cookie'].must_equal "name2=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT\nname=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT" + + header = {'Set-Cookie'=>nil} + Rack::Utils.delete_cookie_header!(header, 'name').must_be_nil + header['Set-Cookie'].must_equal "name=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT" + + header = {'Set-Cookie'=>[]} + Rack::Utils.delete_cookie_header!(header, 'name').must_be_nil + header['Set-Cookie'].must_equal "name=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT" + end + end describe Rack::Utils, "byte_range" do @@ -745,6 +769,8 @@ describe Rack::Utils::Context do r2.must_equal 4 r3 = c3.call(:misc_symbol) r3.must_be_nil + r3 = c2.context(:misc_symbol, test_target3) + r3.must_be_nil r4 = Rack::MockRequest.new(a4).get('/') r4.status.must_equal 200 r5 = Rack::MockRequest.new(a5).get('/') |