#!/usr/bin/env ruby
# $Id: tc_session.rb,v 1.5 2004/04/28 13:17:58 eto Exp $
# Copyright (C) 2003-2004 Kouichirou Eto, All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
# You can redistribute it and/or modify it under the terms of the GNU GPL2.

$LOAD_PATH.unshift('../../lib')
$LOAD_PATH.unshift('../../compat')
require 'test/unit'
require 'qwik/server'

module Test
  class WEBrickRequest < WEBrick::HTTPRequest
    attr_writer :request_uri, :path, :peeraddr
    attr_writer :request_method, :cookies, :header, :query
    attr_writer :user
  end
  class WEBrickResponse < WEBrick::HTTPResponse
    def set_config
      @config.update(WEBrick::Config::HTTP)
    end
  end
end

class TC_Request < Test::Unit::TestCase
  def setup
    @config = Qwik::Config.new(true)
    @req = Qwik::Request.new(@config, nil)
  end

  def test_request
    # test_parse_host
    assert_equal('qwik.jp',     @req.parse_host('qwik.jp'))
    assert_equal('qwik.jp',     @req.parse_host('test'))
    assert_equal('qwik.jp',     @req.parse_host('127.0.0.1'))

    # test_parse_path
    assert_equal(['/', 'FrontPage.html'],@req.parse_path('/'))
    assert_equal(['/', 'FrontPage.html'],@req.parse_path('/FrontPage.html'))
    assert_equal(['/0', 't.jpg'],	@req.parse_path('/0/t.jpg'))
    assert_equal(['/t', 'FrontPage.html'],@req.parse_path('/t/'))
    assert_equal(['/t', 'FrontPage.html'],@req.parse_path('/t/FrontPage.html'))

    # test_parse_req
    assert_equal(['qwik.jp', 'www', '/', 'FrontPage.html'], @req.parse_req('qwik.jp', '/'))
    assert_equal(['qwik.jp', 't', '/t', 'FrontPage.html'], @req.parse_req('qwik.jp', '/t/'))
    assert_equal(['qwik.jp', 't', '/t', 'FrontPage.html'], @req.parse_req('qwik.jp', '/t/FrontPage.html'))
    assert_equal(['qwik.jp', 's.t', '/s.t', 'FrontPage.html'], @req.parse_req('qwik.jp', '/s.t/'))
  end
end

class TC_WEBrickRequest < Test::Unit::TestCase
  def setup
    @config = Qwik::Config.new(true)
    @config.test = true
    @server = Qwik::Server.new(@config)
    trap(:TERM) { @server.shutdown; }
    trap(:INT)  { @server.shutdown; }
  end

  def test_parse_webrick
    @webrick_request = Test::WEBrickRequest.new(@server.config)

    @webrick_request.request_uri = URI.parse('http://qwik.jp/')
    @webrick_request.path = '/'
    @req = Qwik::Request.new(@config, @webrick_request)
    assert_equal('qwik.jp', @req.domain)
    assert_equal('www', @req.sitename)
    assert_equal('/', @req.path)
    assert_equal('FrontPage', @req.base)
    assert_equal('html', @req.ext)

    @webrick_request.request_uri = URI.parse('http://test.qwik.jp/0.html')
    @webrick_request.path = '/0.html'
    @req = Qwik::Request.new(@config, @webrick_request)
    assert_equal('test.qwik.jp', @req.domain)
    assert_equal('www', @req.sitename)
    assert_equal('/', @req.path)
    assert_equal('0', @req.base)
    assert_equal('html', @req.ext)

    @webrick_request.request_uri = URI.parse('http://qwik.jp/test/0.html')
    @webrick_request.path = '/test/0.html'
    @req = Qwik::Request.new(@config, @webrick_request)
    assert_equal('qwik.jp', @req.domain)
    assert_equal('test', @req.sitename)
    assert_equal('/test', @req.path)
    assert_equal('0', @req.base)
    assert_equal('html', @req.ext)

    # test_parse_webrick_env
    @webrick_request = Test::WEBrickRequest.new(@server.config)
    @webrick_request.path = '/'

    @webrick_request.peeraddr = [nil, nil, nil, '192.168.0.1']
    @req = Qwik::Request.new(@config, @webrick_request)

    @webrick_request.request_method = 'POST'
    @req = Qwik::Request.new(@config, @webrick_request)
    assert_equal('POST', @req.request_method)

    @webrick_request.request_method = 'HEAD'
    @req = Qwik::Request.new(@config, @webrick_request)
    assert_equal('HEAD', @req.request_method)

    @webrick_request.request_method = 'GET'
    @req = Qwik::Request.new(@config, @webrick_request)
    assert_equal('GET', @req.request_method)

    @webrick_request.header = {'x-test' => ['t']}
    @req = Qwik::Request.new(@config, @webrick_request)
    assert_equal('t', @req['X-Test'])

    @webrick_request.cookies = []
    @webrick_request.cookies << WEBrick::Cookie.new('s', 't')
    @webrick_request.cookies << WEBrick::Cookie.new('k', 'v')
    @req = Qwik::Request.new(@config, @webrick_request)
    assert_equal('t', @req.cookies['s'])
    assert_equal('v', @req.cookies['k'])

    @webrick_request.query = {'k' => 'v'}
    @req = Qwik::Request.new(@config, @webrick_request)
    assert_equal('v', @req.query['k'])

    @req = Qwik::Request.new(@config, @webrick_request)

    @webrick_request.query = {'c' => 'edit'}
    @req = Qwik::Request.new(@config, @webrick_request)
  end

  def teardown()
    @server.shutdown if @server
  end
end
