#!/usr/bin/env ruby
# $Id: tc_server.rb,v 1.20 2004/04/23 17:05:47 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_Server < Test::Unit::TestCase
  def setup
    @config = Qwik::Config.new(true)
    @config.test = true
    @dir = @config.data_dir.path+"test"
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
    @dir.mkdir if !@dir.directory?
    @memory = Qwik::ServerMemory.new(@config)
    @server = Qwik::Server.new(@config)
    trap(:TERM) { @server.shutdown; }
    trap(:INT)  { @server.shutdown; }
  end

  def test_session
    @webrick_request = Test::WEBrickRequest.new(@server.config)
    uri = URI.parse('http://test.qwik.jp/')
    @webrick_request.request_uri = uri
    @webrick_request.path = '/FrontPage.html'
    @webrick_request.peeraddr = [nil, nil, nil, '127.0.0.1']

    @webrick_response = Test::WEBrickResponse.new(@server.config)
    @webrick_response.set_config

    req = Qwik::Request.new(@config, @webrick_request)
    res = Qwik::Response.new(@config, @webrick_response)
    session = Qwik::Session.new(@config, @memory, req, res)
    farm = Qwik::Farm.new(@config)
    site = farm.get_site(session.req.sitename)

    session.run
    res.setback(@webrick_response)
    assert_equal('text/html; charset=Shift_JIS', @webrick_response['content-type'])
    assert_match(%r|^<|, @webrick_response.body)

    @webrick_request.path = '/theme/base.css'
    req = Qwik::Request.new(@config, @webrick_request)
    res = Qwik::Response.new(@config, @webrick_response)
    session = Qwik::Session.new(@config, @memory, req, res)
    assert_equal('css', session.req.ext)
    session.run
    res.setback(@webrick_response)
    assert_equal('text/css', @webrick_response['content-type'])
    assert_instance_of(File, @webrick_response.body)
    assert_match(%r|^/*|, @webrick_response.body.read)

    # test_head_session
    @webrick_request.path = '/FrontPage.html'
    webrick_request = @webrick_request.dup
    webrick_request.request_method = 'HEAD'
    req = Qwik::Request.new(@config, webrick_request)
    res = Qwik::Response.new(@config, @webrick_response)
    session = Qwik::Session.new(@config, @memory, req, res)
    farm = Qwik::Farm.new(@config)
    site = farm.get_site(session.req.sitename)

    session.run
    res.setback(@webrick_response)
    assert_equal('text/html; charset=Shift_JIS', @webrick_response['content-type'])
    assert_match(%r|^<|, @webrick_response.body)

    webrick_request.path = '/theme/base.css'
    req = Qwik::Request.new(@config, webrick_request)
    res = Qwik::Response.new(@config, @webrick_response)
    session = Qwik::Session.new(@config, @memory, req, res)
    assert_equal('css', session.req.ext)
    session.run
    res.setback(@webrick_response)
    assert_equal('text/css', @webrick_response['content-type'])
    assert_instance_of(File, @webrick_response.body)
    assert_match(%r|^/*|, @webrick_response.body.read)
  end

  def teardown()
    @server.shutdown if @server
    @dir.rmtree if @dir.directory?
    @dir.rmdir  if @dir.directory?
  end
end
