#!../../src/xotclsh
# $Id: s.xocomm.test 1.10 01/04/18 21:24:34+02:00 neumann@mohegan.wu-wien.ac.at $
@ @File {
  description {
    This is a webclient used as a regression test. 
    When it is started it launches an xotcl-Web server as partner process.
    It tests currently the basic functionality of:
    <ul>
    <li>GET and PUT requests
    <li>Basic Access Control
    </ul>
  }
}
array set opt {-startServer 1}
array set opt $argv

set startCmd "../../src/xotclsh ../../apps/comm/webserver.xotcl"

#set auto_path "../../packages ../../src $auto_path"
if {$opt(-startServer)} {
  set PIPE [open "| $startCmd"]
} else {
  puts $startCmd
}

#package require package; package verbose 1
package require xotcl::comm::httpAccess
package require xotcl::comm::ftp
package require xotcl::trace

set hostport localhost:8086
set protectedhostport localhost:9096

set slowURL "http://quote.yahoo.com/q?s=^DJI&d=1d"
set ftpURL "ftp://mohegan.wi-inf.uni-essen.de/welcome.msg"


proc printError msg {puts stderr !!!$msg!!!}

Object userPwd
userPwd proc user {u} {
  [self] set user $u
}
userPwd proc show {realm userVar pwVar} {
  upvar $userVar u $pwVar pw
  set u [[self] set user]
  set pw test
  return 1
}       

Class Test -parameter {msg request test failureTest}
Test proc new args {
   eval Test create [[self] autoname t%03d] $args 
}
Test proc run {} {
  foreach t [lsort [[self] info instances]] {$t run }
  exit
}
Test instproc report {test} {
  #r0 showVars
  #[r0 set token] showVars
  if {[r0 getContentLength]>0} {
    #puts stderr ------------------ct=[[r0 set token] set contentType]
    if {[regexp text [[r0 set token] set contentType]]} {
      #puts stderr c='[r0 getContent]'
    }
  }
  if {[eval $test]} {
    puts "\tsuceeded! $test"
  } else {
    puts "\tfailure! $test"
    puts "\t<[r0::sink set contentLength]>"
  }
}
Test instproc run {} {
  puts "[self] [[self] set msg]"
  if {[catch {eval SimpleRequest r0 -url [[self] set request]} errMsg]} {
    puts "Error catched: '$errMsg'"
  } elseif {[r0 success]} {
    [self] report [[self] set test]
  } elseif {[[self] exists failureTest]} {
    [self] report [[self] set failureTest]
  } else {
    puts "retrieval of [r0 set url] failed"
  }
}


Test new -msg "Trying to load image logo-100.jpg ... " \
    -test {expr {[r0::sink set contentLength] == 1706}} \
    -request http://$hostport/logo-100.jpg

Test new -msg "Trying to PUT a file on web-server ... " \
    -test {expr [[r0 set token] set responseCode] == 200} \
    -request [list http://$hostport/junk.junk -method PUT \
		  -data "this is a test\n" \
		  -contentType plain/text]

Test new -msg "And download it again ... " \
    -test {expr [r0 getContentLength] == 15} \
    -request http://$hostport/junk.junk

Test new -msg "Get protected resource ... " \
    -request http://$protectedhostport/ \
    -test {expr [r0 getContentLength] > 500}

Test new -msg "Try an FTP request $ftpURL ... " \
    -request $ftpURL \
    -test {expr [r0 getContentLength] > 100}

#Test new -msg "Try timeout with slow URL $slowURL ... " \
    -failureTest {expr {[[r0 set token] set errormsg] == {timeout exceeded}}} \
    -request [list $slowURL -timeout 100]

Test new -msg terminate -request http://$protectedhostport/exit \
    -test {set x 1}

after 1000 {
  Test run
}
catch {vwait forever}

