require File.join(File.dirname(__FILE__), 'CONFIG.rb') require 'stringio' require 'tempfile' require 'socket' require 'ostruct' require 'test/unit' require 'facet/kernel/assign_with' require 'nitro/cgi' require 'nitro/cgi/cookie' class String class << self def random(size) s = String.new size.times {|i| s << (Kernel.rand(26)+25) } # ascii range s end end end class TestAdapterCgi < Test::Unit::TestCase # :nodoc: all include Nitro class User attr_accessor :name attr_accessor :password end def test_parse_query_parameters qs = 'name=tml;id=12354' params = Cgi.parse_query_string(qs) assert_equal 2, params.size assert_equal 'tml', params['name'] qs = '' params = Cgi.parse_query_string(qs) assert_equal 0, params.size qs = nil params = Cgi.parse_query_string(qs) assert_equal 0, params.size qs = 'name=tml;arr[]=1;arr[]=2;arr[]=3' params = Cgi.parse_query_string(qs) assert_equal 2, params.size arr = params['arr'] assert_equal Array, arr.class assert_equal 3, arr.size qs = 'other=1;user.name=gmosx;user.password=hello' params = Cgi.parse_query_string(qs) assert_equal 2, params.size u = params['user'] assert_equal Hash, u.class assert_equal 'gmosx', u['name'] assert_equal 'hello', u['password'] user = User.new user.assign_with(params['user']) assert_equal 'gmosx', user.name assert_equal 'hello', user.password # Ruby/Scriptaculous compatibility. qs = 'other=1;user[name]=gmosx;user[password]=hello' params = Cgi.parse_query_string(qs) assert_equal 2, params.size u = params['user'] assert_equal Hash, u.class assert_equal 'gmosx', u['name'] assert_equal 'hello', u['password'] end def test_parse_cookies context = OpenStruct.new context.env = {} context.env['HTTP_COOKIE'] = 'nsid=123; nauth=gmosx:passwd' Cgi.parse_cookies(context) assert_equal 2, context.cookies.size assert_equal '123', context.cookies['nsid'] context.env = {} context.env['HTTP_COOKIE'] = 'nsid=123; nsid=23123' cookies = Cgi.parse_cookies(context) assert_equal 1, context.cookies.size assert_equal "123" + "\0" + "23123", context.cookies['nsid'] end def test_response_headers ctx = OpenStruct.new ctx.status = 200 ctx.response_cookies = { 'nsid' => Cookie.new('nsid', '1233'), 'nauth' => Cookie.new('nauth', 'gmosx') } ctx.response_headers = { 'Content-Type' => 'text/html' } res = "Status: 200 OK\r\nContent-Type: text/html\r\nSet-Cookie: nauthnauth=gmosx; Path=/\r\nSet-Cookie: nsidnsid=1233; Path=/\r\n\r\n" assert_equal res, Cgi.response_headers(ctx) end =begin def test_parse_multipart context = OpenStruct.new context.in = File.open(File.join(File.dirname(__FILE__), 'raw_post1.bin')) context.headers = { 'CONTENT_LENGTH' => 11963 } boundary = '---------------------------277124474474241471962886717' CgiUtils.parse_multipart(context, boundary) end =end def test_parse_multipart # MIME block close falls on read boundary, also contains an empty field. boundary = "---------------------------15773515131648678014689318540" closure = "#{Cgi::EOL}--#{boundary}--#{Cgi::EOL}" block_size = Cgi.buffer_size input = StringIO.new input << "--#{boundary}#{Cgi::EOL}" input << "Content-Disposition: form-data; name=\"test_file_a\"; filename=\"test_case.txt\"#{Cgi::EOL}Content-Type: application/octet-stream#{Cgi::EOL}#{Cgi::EOL}" fake_file_a = String.random(block_size - input.size - (closure.size/2)) input << fake_file_a input_after_fake_file = input.size input << "#{Cgi::EOL}--#{boundary}#{Cgi::EOL}" #Boundary fell inbetween two reads? assert_equal((block_size > input_after_fake_file) && (block_size < input.size),true) input << "Content-Disposition: form-data; name=\"test_file_b\"; filename=\"test_case.txt\"#{Cgi::EOL}Content-Type: application/octet-stream#{Cgi::EOL}#{Cgi::EOL}" fake_file_b = Tempfile.new("TCCGI") i = 0 file_size = 1024*1024*10 # 10MB i += fake_file_b.write String.random(1024*10) while i < file_size fake_file_b.rewind input << fake_file_b.read fake_file_b.rewind input << "#{Cgi::EOL}--#{boundary}#{Cgi::EOL}" input << "Content-Disposition: form-data; name=\"empty_field\"#{Cgi::EOL}#{Cgi::EOL}" input << closure input.rewind start = Time.now params = Nitro::Cgi.parse_multipart(make_context(input), boundary) duration = Time.now - start # puts "\nparse_multipart took: #{duration} seconds\n" assert_equal(fake_file_a,params['test_file_a'].to_s) assert_equal(fake_file_b.read,params['test_file_b'].to_s) assert_equal(String.new,params['empty_field'].to_s) end def make_context(input) context = OpenStruct.new context.in = input context.content_length = input.size context.env = { 'HTTP_USER_AGENT' => 'TestCase' } context.session = Hash.new class << context.session def sync true end end context end end # Tempfile.new("CGI")