#
# testing how the environment variables resolution 
# is handled by mod_rivet
#

set rvttestfilename env.rvt

::tcltest::test env-1.1 {Environment variable} {
    set page [ ::http::geturl "${urlbase}$rvttestfilename?load_env=true" ]
    regexp -line "^env\\(DOCUMENT_NAME\\) = $rvttestfilename\$" [::http::data $page] match
    ::http::cleanup $page
    set match
} "env(DOCUMENT_NAME) = $rvttestfilename"

::tcltest::test env-1.2 {Environment variable} {
    set page [ ::http::geturl "${urlbase}$rvttestfilename?load_env=false" ]
    regexp -line "^env\\(DOCUMENT_NAME\\) = $rvttestfilename\$" [::http::data $page] match
    ::http::cleanup $page
    set match
} "env(DOCUMENT_NAME) = $rvttestfilename"

::tcltest::test env-1.2.1 {CGI Environment variable double translation} {
    set page1 [ ::http::geturl "${urlbase}$rvttestfilename?protocol=1"]
    set match1 [string trim [::http::data $page1]]
    set page2 [ ::http::geturl "${urlbase}$rvttestfilename?protocol=1"]
    set match2 [string trim [::http::data $page2]]
    #puts "proto: $match1 $match2"
    ::http::cleanup $page1
    ::http::cleanup $page2
    expr [string match $match1 $match2]
} "1"

set envmethod env_methods.tcl
set env_vars [list "DATE_LOCAL" "DATE_GMT" "LAST_MODIFIED" "DOCUMENT_URI" "DOCUMENT_PATH_INFO" "DOCUMENT_NAME"  \
                   "QUERY_STRING_UNESCAPED" "RIVET_CACHE_FREE" "RIVET_CACHE_SIZE" "HTTP_ACCEPT" "HTTP_HOST" \
                   "QUERY_STRING" "SCRIPT_FILENAME" "SERVER_NAME" "SERVER_PORT" "SERVER_PROTOCOL"]

puts "comparing ::rivet::env and ::rivet::load_env for variable resolution"
foreach v $env_vars {
    puts -nonewline "$v "
    ::tcltest::test env-1.3 {Environment variable methods} {
        set page [::http::geturl "${urlbase}$envmethod?envvar=$v"]
        ::http::data $page
    } "$v: OK"
}
puts ""


