require File.join(File.dirname(__FILE__), '..', 'CONFIG.rb') require 'test/unit' require 'nitro/cgi/request' class TC_Request < Test::Unit::TestCase # :nodoc: all class DummyRequest include Nitro::Request def initialize @headers = {} @params = {} end end def test_all req = DummyRequest.new req.headers['HTTP_HOST'] = 'www.nitroproject.org' assert_equal 'nitroproject.org', req.domain assert_equal 'www', req.subdomains.first req.headers['HTTP_HOST'] = 'www.nitroproject.co.uk' assert_equal 'nitroproject.co.uk', req.domain(2) req.headers['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' assert req.xhr? req.headers['REQUEST_METHOD'] = 'POST' req.headers['CONTENT_TYPE'] = 'application/x-yaml' assert req.yaml_post? req.instance_variable_set '@post_format', nil req.headers['CONTENT_TYPE'] = 'text/xml' assert req.xml_post? req['test'] = 'hello' assert req['test'] assert_equal 'hello', req['test'] assert req.has?('test') assert !req.has?('phantom') req['bug'] = nil assert !req.has?('bug') end def test_local_net? req = DummyRequest.new local_ips = ["192.168.1.1", "192.168.20.1", "192.168.99.1", "192.168.200.1", "192.168.21.245", "192.168.90.34", "192.168.254.254", "172.16.1.1", "172.18.1.1", "172.31.254.1", "172.21.1.254", "10.16.1.1", "10.0.1.1", "10.254.1.254", "10.192.1.192", "10.254.254.254", "10.10.10.10"] not_local_ips = ["191.168.1.1", "192.169.20.1", "193.168.254.254", "172.15.1.1", "171.18.1.1", "172.32.0.0", "173.21.1.254", "11.16.1.1", "66.249.93.104", "72.14.221.104", "66.102.9.104", "17.254.3.183", "207.46.250.119", "207.46.130.108", "207.68.160.190", "65.54.240.126", "213.199.144.151", "65.55.238.126"] assert_equal true, local_ips.map { |ip| req.local_net?(ip) }.all? assert_equal true, not_local_ips.map { |ip| req.local_net?(ip) }.all?{|v|!v} end end