test/spec_utils.rb in rack-1.2.8 vs test/spec_utils.rb in rack-1.3.0.beta

- old
+ new

@@ -1,17 +1,11 @@ +# -*- encoding: utf-8 -*- + require 'rack/utils' require 'rack/mock' describe Rack::Utils do - def kcodeu - one8 = RUBY_VERSION.to_f < 1.9 - default_kcode, $KCODE = $KCODE, 'U' if one8 - yield - ensure - $KCODE = default_kcode if one8 - end - should "escape correctly" do Rack::Utils.escape("fo<o>bar").should.equal "fo%3Co%3Ebar" Rack::Utils.escape("a space").should.equal "a+space" Rack::Utils.escape("q1!2\"'w$5&7/z8)?\\"). should.equal "q1%212%22%27w%245%267%2Fz8%29%3F%5C" @@ -24,42 +18,20 @@ matz_name_sep = "\xE3\x81\xBE\xE3\x81\xA4 \xE3\x82\x82\xE3\x81\xA8".unpack("a*")[0] # Matsu moto matz_name_sep.force_encoding("UTF-8") if matz_name_sep.respond_to? :force_encoding Rack::Utils.escape(matz_name_sep).should.equal '%E3%81%BE%E3%81%A4+%E3%82%82%E3%81%A8' end - if RUBY_VERSION[/^\d+\.\d+/] == '1.8' - should "escape correctly for multibyte characters if $KCODE is set to 'U'" do - kcodeu do - matz_name = "\xE3\x81\xBE\xE3\x81\xA4\xE3\x82\x82\xE3\x81\xA8".unpack("a*")[0] # Matsumoto - matz_name.force_encoding("UTF-8") if matz_name.respond_to? :force_encoding - Rack::Utils.escape(matz_name).should.equal '%E3%81%BE%E3%81%A4%E3%82%82%E3%81%A8' - matz_name_sep = "\xE3\x81\xBE\xE3\x81\xA4 \xE3\x82\x82\xE3\x81\xA8".unpack("a*")[0] # Matsu moto - matz_name_sep.force_encoding("UTF-8") if matz_name_sep.respond_to? :force_encoding - Rack::Utils.escape(matz_name_sep).should.equal '%E3%81%BE%E3%81%A4+%E3%82%82%E3%81%A8' - end - end - - should "unescape multibyte characters correctly if $KCODE is set to 'U'" do - kcodeu do - Rack::Utils.unescape('%E3%81%BE%E3%81%A4+%E3%82%82%E3%81%A8').should.equal( - "\xE3\x81\xBE\xE3\x81\xA4 \xE3\x82\x82\xE3\x81\xA8".unpack("a*")[0]) - end - end - end - - should "escape objects that responds to to_s" do - kcodeu do - Rack::Utils.escape(:id).should.equal "id" - end - end - if "".respond_to?(:encode) should "escape non-UTF8 strings" do Rack::Utils.escape("ø".encode("ISO-8859-1")).should.equal "%F8" end end + should "escape path spaces with %20" do + Rack::Utils.escape_path("foo bar").should.equal "foo%20bar" + end + should "unescape correctly" do Rack::Utils.unescape("fo%3Co%3Ebar").should.equal "fo<o>bar" Rack::Utils.unescape("a+space").should.equal "a space" Rack::Utils.unescape("a%20space").should.equal "a space" Rack::Utils.unescape("q1%212%22%27w%245%267%2Fz8%29%3F%5C"). @@ -102,10 +74,13 @@ should.equal "foo" => nil, "bar" => "" Rack::Utils.parse_nested_query("foo=bar&baz="). should.equal "foo" => "bar", "baz" => "" Rack::Utils.parse_nested_query("my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F"). should.equal "my weird field" => "q1!2\"'w$5&7/z8)?" + + Rack::Utils.parse_nested_query("a=b&pid%3D1234=1023"). + should.equal "pid=1234" => "1023", "a" => "b" Rack::Utils.parse_nested_query("foo[]"). should.equal "foo" => [nil] Rack::Utils.parse_nested_query("foo[]="). should.equal "foo" => [""] @@ -213,45 +188,24 @@ lambda { Rack::Utils.build_nested_query("foo=bar") }. should.raise(ArgumentError). message.should.equal "value must be a Hash" end - should "escape html entities [&><'\"/]" do + should "should escape html entities [&><'\"/]" do Rack::Utils.escape_html("foo").should.equal "foo" Rack::Utils.escape_html("f&o").should.equal "f&amp;o" Rack::Utils.escape_html("f<o").should.equal "f&lt;o" Rack::Utils.escape_html("f>o").should.equal "f&gt;o" - Rack::Utils.escape_html("f'o").should.equal "f&#39;o" + Rack::Utils.escape_html("f'o").should.equal "f&#x27;o" Rack::Utils.escape_html('f"o').should.equal "f&quot;o" - Rack::Utils.escape_html("f/o").should.equal "f&#47;o" - Rack::Utils.escape_html("<foo></foo>").should.equal "&lt;foo&gt;&lt;&#47;foo&gt;" + Rack::Utils.escape_html("f/o").should.equal "f&#x2F;o" + Rack::Utils.escape_html("<foo></foo>").should.equal "&lt;foo&gt;&lt;&#x2F;foo&gt;" end - should "escape html entities even on MRI when it's bugged" do - test_escape = lambda do - kcodeu do - Rack::Utils.escape_html("\300<").should.equal "\300&lt;" - end - end - - if RUBY_VERSION.to_f < 1.9 - test_escape.call - else - test_escape.should.raise(ArgumentError) - end - end - - if "".respond_to?(:encode) - should "escape html entities in unicode strings" do - # the following will cause warnings if the regex is poorly encoded: - Rack::Utils.escape_html("☃").should.equal "☃" - end - end - should "figure out which encodings are acceptable" do helper = lambda do |a, b| - request = Rack::Request.new(Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => a)) + Rack::Request.new(Rack::MockRequest.env_for("", "HTTP_ACCEPT_ENCODING" => a)) Rack::Utils.select_best_encoding(a, b) end helper.call(%w(), [["x", 1]]).should.equal(nil) helper.call(%w(identity), [["identity", 0.0]]).should.equal(nil) @@ -272,16 +226,11 @@ should "return the bytesize of String" do Rack::Utils.bytesize("FOO\xE2\x82\xAC").should.equal 6 end - should "should perform constant time string comparison" do - Rack::Utils.secure_compare('a', 'a').should.equal true - Rack::Utils.secure_compare('a', 'b').should.equal false - end - - should "should return status code for integer" do + should "return status code for integer" do Rack::Utils.status_code(200).should.equal 200 end should "return status code for string" do Rack::Utils.status_code("200").should.equal 200 @@ -290,10 +239,53 @@ should "return status code for symbol" do Rack::Utils.status_code(:ok).should.equal 200 end end +describe Rack::Utils, "byte_range" do + should "ignore missing or syntactically invalid byte ranges" do + Rack::Utils.byte_ranges({},500).should.equal nil + Rack::Utils.byte_ranges({"HTTP_RANGE" => "foobar"},500).should.equal nil + Rack::Utils.byte_ranges({"HTTP_RANGE" => "furlongs=123-456"},500).should.equal nil + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes="},500).should.equal nil + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=-"},500).should.equal nil + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=123,456"},500).should.equal nil + # A range of non-positive length is syntactically invalid and ignored: + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=456-123"},500).should.equal nil + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=456-455"},500).should.equal nil + end + + should "parse simple byte ranges" do + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=123-456"},500).should.equal [(123..456)] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=123-"},500).should.equal [(123..499)] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=-100"},500).should.equal [(400..499)] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=0-0"},500).should.equal [(0..0)] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=499-499"},500).should.equal [(499..499)] + end + + should "truncate byte ranges" do + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=123-999"},500).should.equal [(123..499)] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=600-999"},500).should.equal [] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=-999"},500).should.equal [(0..499)] + end + + should "ignore unsatisfiable byte ranges" do + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=500-501"},500).should.equal [] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=500-"},500).should.equal [] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=999-"},500).should.equal [] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=-0"},500).should.equal [] + end + + should "handle byte ranges of empty files" do + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=123-456"},0).should.equal [] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=0-"},0).should.equal [] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=-100"},0).should.equal [] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=0-0"},0).should.equal [] + Rack::Utils.byte_ranges({"HTTP_RANGE" => "bytes=-0"},0).should.equal [] + end +end + describe Rack::Utils::HeaderHash do should "retain header case" do h = Rack::Utils::HeaderHash.new("Content-MD5" => "d5ff4e2a0 ...") h['ETag'] = 'Boo!' h.to_hash.should.equal "Content-MD5" => "d5ff4e2a0 ...", "ETag" => 'Boo!' @@ -373,10 +365,16 @@ k.should.equal("foo") v.should.equal("bar\nbaz") end end + should "not create headers out of thin air" do + h = Rack::Utils::HeaderHash.new + h['foo'] + h['foo'].should.be.nil + h.should.not.include 'foo' + end end describe Rack::Utils::Context do class ContextTest attr_reader :app @@ -427,290 +425,6 @@ r4.status.should.equal 200 r5 = Rack::MockRequest.new(a5).get('/') r5.status.should.equal 200 r4.body.should.equal r5.body end -end - -describe Rack::Utils::Multipart do - def multipart_fixture(name) - file = multipart_file(name) - data = File.open(file, 'rb') { |io| io.read } - - type = "multipart/form-data; boundary=AaB03x" - length = data.respond_to?(:bytesize) ? data.bytesize : data.size - - { "CONTENT_TYPE" => type, - "CONTENT_LENGTH" => length.to_s, - :input => StringIO.new(data) } - end - - def multipart_file(name) - File.join(File.dirname(__FILE__), "multipart", name.to_s) - end - - should "return nil if content type is not multipart" do - env = Rack::MockRequest.env_for("/", - "CONTENT_TYPE" => 'application/x-www-form-urlencoded') - Rack::Utils::Multipart.parse_multipart(env).should.equal nil - end - - should "parse multipart upload with text file" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:text)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["submit-name"].should.equal "Larry" - params["files"][:type].should.equal "text/plain" - params["files"][:filename].should.equal "file1.txt" - params["files"][:head].should.equal "Content-Disposition: form-data; " + - "name=\"files\"; filename=\"file1.txt\"\r\n" + - "Content-Type: text/plain\r\n" - params["files"][:name].should.equal "files" - params["files"][:tempfile].read.should.equal "contents" - end - - should "parse multipart upload with nested parameters" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:nested)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["foo"]["submit-name"].should.equal "Larry" - params["foo"]["files"][:type].should.equal "text/plain" - params["foo"]["files"][:filename].should.equal "file1.txt" - params["foo"]["files"][:head].should.equal "Content-Disposition: form-data; " + - "name=\"foo[files]\"; filename=\"file1.txt\"\r\n" + - "Content-Type: text/plain\r\n" - params["foo"]["files"][:name].should.equal "foo[files]" - params["foo"]["files"][:tempfile].read.should.equal "contents" - end - - should "parse multipart upload with binary file" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:binary)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["submit-name"].should.equal "Larry" - params["files"][:type].should.equal "image/png" - params["files"][:filename].should.equal "rack-logo.png" - params["files"][:head].should.equal "Content-Disposition: form-data; " + - "name=\"files\"; filename=\"rack-logo.png\"\r\n" + - "Content-Type: image/png\r\n" - params["files"][:name].should.equal "files" - params["files"][:tempfile].read.length.should.equal 26473 - end - - should "parse multipart upload with empty file" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:empty)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["submit-name"].should.equal "Larry" - params["files"][:type].should.equal "text/plain" - params["files"][:filename].should.equal "file1.txt" - params["files"][:head].should.equal "Content-Disposition: form-data; " + - "name=\"files\"; filename=\"file1.txt\"\r\n" + - "Content-Type: text/plain\r\n" - params["files"][:name].should.equal "files" - params["files"][:tempfile].read.should.equal "" - end - - should "parse multipart upload with filename with semicolons" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:semicolon)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["files"][:type].should.equal "text/plain" - params["files"][:filename].should.equal "fi;le1.txt" - params["files"][:head].should.equal "Content-Disposition: form-data; " + - "name=\"files\"; filename=\"fi;le1.txt\"\r\n" + - "Content-Type: text/plain\r\n" - params["files"][:name].should.equal "files" - params["files"][:tempfile].read.should.equal "contents" - end - - should "not include file params if no file was selected" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:none)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["submit-name"].should.equal "Larry" - params["files"].should.equal nil - params.keys.should.not.include "files" - end - - should "parse IE multipart upload and clean up filename" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:ie)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["files"][:type].should.equal "text/plain" - params["files"][:filename].should.equal "file1.txt" - params["files"][:head].should.equal "Content-Disposition: form-data; " + - "name=\"files\"; " + - 'filename="C:\Documents and Settings\Administrator\Desktop\file1.txt"' + - "\r\nContent-Type: text/plain\r\n" - params["files"][:name].should.equal "files" - params["files"][:tempfile].read.should.equal "contents" - end - - should "parse filename and modification param" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_and_modification_param)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["files"][:type].should.equal "image/jpeg" - params["files"][:filename].should.equal "genome.jpeg" - params["files"][:head].should.equal "Content-Type: image/jpeg\r\n" + - "Content-Disposition: attachment; " + - "name=\"files\"; " + - "filename=genome.jpeg; " + - "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";\r\n" + - "Content-Description: a complete map of the human genome\r\n" - params["files"][:name].should.equal "files" - params["files"][:tempfile].read.should.equal "contents" - end - - should "parse filename with escaped quotes" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_escaped_quotes)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["files"][:type].should.equal "application/octet-stream" - params["files"][:filename].should.equal "escape \"quotes" - params["files"][:head].should.equal "Content-Disposition: form-data; " + - "name=\"files\"; " + - "filename=\"escape \\\"quotes\"\r\n" + - "Content-Type: application/octet-stream\r\n" - params["files"][:name].should.equal "files" - params["files"][:tempfile].read.should.equal "contents" - end - - should "parse filename with percent escaped quotes" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_percent_escaped_quotes)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["files"][:type].should.equal "application/octet-stream" - params["files"][:filename].should.equal "escape \"quotes" - params["files"][:head].should.equal "Content-Disposition: form-data; " + - "name=\"files\"; " + - "filename=\"escape %22quotes\"\r\n" + - "Content-Type: application/octet-stream\r\n" - params["files"][:name].should.equal "files" - params["files"][:tempfile].read.should.equal "contents" - end - - should "parse filename with unescaped quotes" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_quotes)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["files"][:type].should.equal "application/octet-stream" - params["files"][:filename].should.equal "escape \"quotes" - params["files"][:head].should.equal "Content-Disposition: form-data; " + - "name=\"files\"; " + - "filename=\"escape \"quotes\"\r\n" + - "Content-Type: application/octet-stream\r\n" - params["files"][:name].should.equal "files" - params["files"][:tempfile].read.should.equal "contents" - end - - should "parse filename with escaped quotes and modification param" do - env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_escaped_quotes_and_modification_param)) - params = Rack::Utils::Multipart.parse_multipart(env) - params["files"][:type].should.equal "image/jpeg" - params["files"][:filename].should.equal "\"human\" genome.jpeg" - params["files"][:head].should.equal "Content-Type: image/jpeg\r\n" + - "Content-Disposition: attachment; " + - "name=\"files\"; " + - "filename=\"\"human\" genome.jpeg\"; " + - "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";\r\n" + - "Content-Description: a complete map of the human genome\r\n" - params["files"][:name].should.equal "files" - params["files"][:tempfile].read.should.equal "contents" - end - - - it "should parse very long unquoted multipart file names" do - data = <<-EOF ---AaB03x\r -Content-Type: text/plain\r -Content-Disposition: attachment; name=file; filename=#{'long' * 100}\r -\r -contents\r ---AaB03x--\r - EOF - - options = { - "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x", - "CONTENT_LENGTH" => data.length.to_s, - :input => StringIO.new(data) - } - env = Rack::MockRequest.env_for("/", options) - params = Rack::Utils::Multipart.parse_multipart(env) - - params["file"][:filename].should.equal('long' * 100) - end - - it "rewinds input after parsing upload" do - options = multipart_fixture(:text) - input = options[:input] - env = Rack::MockRequest.env_for("/", options) - params = Rack::Utils::Multipart.parse_multipart(env) - params["submit-name"].should.equal "Larry" - params["files"][:filename].should.equal "file1.txt" - input.read.length.should.equal 197 - end - - it "builds multipart body" do - files = Rack::Utils::Multipart::UploadedFile.new(multipart_file("file1.txt")) - data = Rack::Utils::Multipart.build_multipart("submit-name" => "Larry", "files" => files) - - options = { - "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x", - "CONTENT_LENGTH" => data.length.to_s, - :input => StringIO.new(data) - } - env = Rack::MockRequest.env_for("/", options) - params = Rack::Utils::Multipart.parse_multipart(env) - params["submit-name"].should.equal "Larry" - params["files"][:filename].should.equal "file1.txt" - params["files"][:tempfile].read.should.equal "contents" - end - - it "builds nested multipart body" do - files = Rack::Utils::Multipart::UploadedFile.new(multipart_file("file1.txt")) - data = Rack::Utils::Multipart.build_multipart("people" => [{"submit-name" => "Larry", "files" => files}]) - - options = { - "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x", - "CONTENT_LENGTH" => data.length.to_s, - :input => StringIO.new(data) - } - env = Rack::MockRequest.env_for("/", options) - params = Rack::Utils::Multipart.parse_multipart(env) - params["people"][0]["submit-name"].should.equal "Larry" - params["people"][0]["files"][:filename].should.equal "file1.txt" - params["people"][0]["files"][:tempfile].read.should.equal "contents" - end - - it "can parse fields that end at the end of the buffer" do - input = File.read(multipart_file("bad_robots")) - - req = Rack::Request.new Rack::MockRequest.env_for("/", - "CONTENT_TYPE" => "multipart/form-data, boundary=1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon", - "CONTENT_LENGTH" => input.size, - :input => input) - - req.POST['file.path'].should.equal "/var/tmp/uploads/4/0001728414" - req.POST['addresses'].should.not.equal nil - end - - it "builds complete params with the chunk size of 16384 slicing exactly on boundary" do - data = File.open(multipart_file("fail_16384_nofile")) { |f| f.read }.gsub(/\n/, "\r\n") - options = { - "CONTENT_TYPE" => "multipart/form-data; boundary=----WebKitFormBoundaryWsY0GnpbI5U7ztzo", - "CONTENT_LENGTH" => data.length.to_s, - :input => StringIO.new(data) - } - env = Rack::MockRequest.env_for("/", options) - params = Rack::Utils::Multipart.parse_multipart(env) - - params.should.not.equal nil - params.keys.should.include "AAAAAAAAAAAAAAAAAAA" - params["AAAAAAAAAAAAAAAAAAA"].keys.should.include "PLAPLAPLA_MEMMEMMEMM_ATTRATTRER" - params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"].keys.should.include "new" - params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"].keys.should.include "-2" - params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"]["-2"].keys.should.include "ba_unit_id" - params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"]["-2"]["ba_unit_id"].should.equal "1017" - end - - should "return nil if no UploadedFiles were used" do - data = Rack::Utils::Multipart.build_multipart("people" => [{"submit-name" => "Larry", "files" => "contents"}]) - data.should.equal nil - end - - should "raise ArgumentError if params is not a Hash" do - lambda { Rack::Utils::Multipart.build_multipart("foo=bar") }. - should.raise(ArgumentError). - message.should.equal "value must be a Hash" - end -end +end \ No newline at end of file