!******************************************************************************!
! Tests for running CGI scripts on a web server
!
! Version: $Id: test_cgi.mm,v 1.12 2011/06/01 14:11:21 ntn Exp $
!******************************************************************************!
startmacro S:cgipath["nmcgi"] S:hostname["localhost"]
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  result L:outStatus 1

if env.ostype EQS "UNIX" then

  ! Test a CGI file without KEYWORDS
  call test_cgi_no_keywords

  ! Test a file with KEYWORDS
  if /KNOWNDR isTrue then
    call test_cgi_keywords
  endif
elseif env.ostype EQS "DOS" then
  call test_cgi_dos
endif

if outStatus LT 0 then
  info "^reg.main.name: Please verify the following server"
  info "information before running this test again."
  info "    NeXtMidas Host Name: ^{hostname}"
  info "    NeXtMidas CGI Path:  ^{cgipath}"
endif

endmacro

!******************************************************************************!
procedure test_cgi_no_keywords

  result testName "^reg.main.name: cat type 1999 data, no keywords"
  if interAct then
    status http://^{hostname}/^{cgipath}/test_script_cat_world.sh{FG=txt}
    call getResponse "Do you see a text file status above?" response
    assert/stat=tempStatus/text="^reg.main.name: ^testName, FG=TXT" &
          "^response" EQS "Y"
    if "^tempStatus" NEQS "PASS" res outStatus -1
  endif
  remove filetype
  status http://^{hostname}/^{cgipath}/test_script_cat_world.sh{FG=dat} &
         type=filetype
  assert/stat=tempStatus/text="^reg.main.name: ^testName, FG=DAT" &
          ^filetype EQ 1999
  if "^tempStatus" NEQS "PASS" res outStatus -1

return

!******************************************************************************!
procedure test_cgi_keywords
  if interAct then
    result testName "^reg.main.name: cat type 5000 file w/ keys, text listing"
    status http://^{hostname}/^{cgipath}/test_script_cat_cone.sh{FG=txt}
    call getResponse "Do you see a text file status above?" response
    assert/stat=tempStatus/text="^reg.main.name: ^testName, FG=TXT" &
          "^response" EQS "Y"
    if "^tempStatus" NEQS "PASS" res outStatus -1

  endif
  result testName "^reg.main.name: cat 5000 file w/ keys, type"
  remove filetype
  status http://^{hostname}/^{cgipath}/test_script_cat_cone.sh{FG=dat} &
         type=filetype
  assert/stat=tempStatus/text="^reg.main.name: ^testName, FG=DAT" &
          ^filetype EQ 5000
  if "^tempStatus" NEQS "PASS" res outStatus -1

  result testName "^reg.main.name: cat 5000 file w/ keys, keywords"
  remove col thickness
  noop http://^{hostname}/^{cgipath}/test_script_cat_cone.sh{FG=dat} &
       "test_cgi_cone.tmp"

  key "test_cgi_cone.tmp" GET color L:col
  key "test_cgi_cone.tmp" GET thick L:thickness
  assert/stat=tempStatus/text="^reg.main.name: ^testName, FG=DAT" &
          col EQ 2 AND thickness EQ 1
  if "^tempStatus" NEQS "PASS" res outStatus -1

return

!******************************************************************************!
procedure test_cgi_dos

  result testName "^reg.main.name: test of DOS text file status"

  if interAct then
    status http://^{hostname}/^{cgipath}/test_script_cat_world.bat{FG=txt}
    call getResponse "Do you see a text file status above?" response
    assert/stat=tempStatus/text="^reg.main.name: ^testName, FG=TXT" &
          "^response" EQS "Y"
    if "^tempStatus" NEQS "PASS" res outStatus -1
  endif
  remove filetype

return

!******************************************************************************!
procedure getResponse S:prompt U:OutResponse
  remove response
  if outStatus GT 0 then
    ask response "^{prompt} [Y]"
  else
    res response "N"
  endif

  if ^response EQS "Y" or ^response EQS "y" then
    result respose "Y"
  else
    result reponse "N"
  endif
  result ^OutResponse ^response
return

!******************************************************************************!
procedure processException
  warn "Caught Exception"
  res outStatus -1
return
