! This will test the functionality of RMIF /HTTP switch by verifying that it returns back things
! without exceptions when browsing to certain URLs available with the /HTTP switch
!
! @author  Neon Ngo
! @since NeXtMidas 2.5.0
!
! <port>  The server's port number (defaults to -1 for semi-random port between 9900-9999)
!
startmacro L:port[-1]
  switch INTERACTIVE interAct     GET DEF=TRUE  SDEF=TRUE
  switch DEBUG       debug        GET DEF=false SDEF=true
  switch VERBOSE     verbose      GET DEF=false SDEF=true
  switch STATEM      statemachine GET DEF=false SDEF=true

  set inNeXtOpt TEST("^{reg.shell.class}",eqs,"class nxm.sys.libxm.XmShell")

  if port == -1 then
    calc L:port random 100 * 9900 +   ! semi-random port in range of [9900-9999]
  endif

  ! Automatic test for the /HTTP switch on RMIF
  pipe on
    rmif/STATEM=^statemachine/VERBOSE=^verbose/http ^port
!    rmif/http ^port
    set url "http://localhost:^{reg.rmif.port}"

    assert/text="RMIF/HTTP: browse / (root web page)" "^{url}/" FEXISTS

    file copy/f "^{url}/header" ram:test_rmif_header.txt
    assert/text="RMIF/HTTP: browse /header" ram:test_rmif_header.txt{FG=txt} FEXISTS

    assert/text="RMIF/HTTP: browse /nmroot/" ^{url}/nmroot/ FEXISTS

    file copy/f "^{url}/Files/filesPage.html?Aux1" ram:test_rmif_existing_aux.txt
    assert/text="RMIF/HTTP: browse existing AUX=1 under /Files" &
      ram:test_rmif_existing_aux.txt{FG=txt} FEXISTS
    if debug then datalist ram:test_rmif_existing_aux.txt

  ! 2010-06-07 NTN: this test case cannot run under NeXtOpt (since X-Midas current does not support named AUXes)
  if inNeXtOpt isFalse then
    ! test BUG 909 - should not see any exceptions in console
    file copy/f "^{url}/Files/filesPage.html?Auxtest99" ram:test_rmif_nonexisting_aux.txt
    assert/text="RMIF/HTTP: browse non-existent AUX=test99 under /Files" &
      ram:test_rmif_nonexisting_aux.txt{FG=txt} FEXISTS
    if debug then datalist ram:test_rmif_nonexisting_aux.txt
  endif

    set this.timer(0) 2.0
  pipe off

  if interAct then
    ask userResult "Did you see any Exceptions or Errors in the console output above? [NO] "
    say ""
    assert/text="RMIF/HTTP: User reported Exception/Error" ^userResult ISFALSE
  endif

  if debug then say "last command in macro"
endmacro

!******************************************************************************!
procedure processMessage m:msg
  if debug then
    say "^reg.main.name: Got message name=^msg.name fid=^msg.fid info=^msg.info data=^msg.data"
  endif

  if msg.name eqs "TIMER" pipe stop
return

!****************************************************************************!
procedure processException m:emsg
  !warning "^reg.main.name: Got Exception: ^emsg.name = ^emsg.data from = ^emsg.fid"
  if debug then results emsg

  ! FAIL on unknown exception
  assert/text="^reg.main.name: Got unexpected Exception: ^emsg.data" "FALSE"
return
