# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")

module Qwik
  class Action
    def c_relative_to_absolute(url)
      return url if /\A\// =~ url
      "/"+@site.urlpath+url
    end

    def c_relative_to_full(url)
      if /\Ahttp:\/\// =~ url ||
	  /\Ahttps:\/\// =~ url
	return url # already full
      end

      # check siteurl
      siteurl = @site.siteconfig["siteurl"]
      if siteurl != ""
	url = siteurl+url
	return url
      end

      # make full url
      path = c_relative_to_absolute(url)
      port = ""
      port = ":"+@config.public_port if @config.public_port.to_i != 80
      host = @config.public_host
      url = "http://"+host+port+path
      url
    end
  end
end

if $0 == __FILE__
  require "t/test"
  $debug = true
end

if $debug
  class TC_ActUrl < Test::Unit::TestCase
    include TestSession

    def test_all
      session

      # test_c_relative_to_absolute
      assert_equal("/test/t.html",
		   @action.c_relative_to_absolute("t.html"))
      assert_equal("/test/1.files",
		   @action.c_relative_to_absolute("1.files"))

      # test_c_relative_to_full with test site
      assert_match(/\Ahttp:/,
		   @action.c_relative_to_full("FrontPage.html"))

      # test_c_relative_to_full
      assert_equal("http://e.com/",
		   @action.c_relative_to_full("http://e.com/"))

      # Check when siteurl is changed.
      page = @site["_SiteConfig"]
      page.store(":siteurl:http://e.com/")
      assert_equal("http://e.com/FrontPage.html",
		   @action.c_relative_to_full("FrontPage.html"))
      page.delete

      # test default site
      @action.instance_eval {
	@site = @memory.farm.get_default_site
      }
      assert_equal("/t.html",
		   @action.c_relative_to_absolute("t.html"))
      assert_equal("/1.files",
		   @action.c_relative_to_absolute("1.files"))
      assert_equal("http://127.0.0.1:9190/t.html",
		   @action.c_relative_to_full("t.html"))
    end
  end
end

