!
! This will test the timeout qualifier on HttpResource.
! This is the server portion of the test case for Bug 1275 & Bug 1810.
!
! @param httpPort  Port for HTTP Server to listen on.
!                  Since NeXtMidas 2.7.0, use 0 to listen on any available port.
! @param delayTime Time in seconds to delay before sending a response back to client
! @param portLabel Results label to save HTTP Server's port in parent's results table
!                  if not blank (e.g. "")
! @param testResultsLabel Results label to save that HTTP Server got post request
!                         in parent's results table if not blank (e.g. "")
! @param expectedTestData This the expected test data that HTTP Server should receive
!                         from a HTTP POST request from client.
!
! @bug 1275 HttpResource: Need to be able to set HTTP timeout via a qualifier
! @bug 1810 CONVERT: default behavior of conver with tbl2xml (output to HttpResource)
! @author  Jim Roush - original version
! @author  Neon Ngo - cleanup and mods to listen on any available port, test for Bug 1810
! @version $Id: test_http_resource_server.mm,v 1.4 2010/09/10 23:15:27 ntn Exp $
!
startmacro L:httpPort[0] L:delayTime[3] U:portLabel[""] U:testResultsLabel[""] S:expectedTestData
  switch DEBUG dbg GET DEF=false SDEF=true

  global/parent ^{portLabel} ^{testResultsLabel}
  invoke anyPortSupported nxm.sys.lib.StringUtil.compareVersions("^env.nmversion","2.7.0")
  if httpPort EQ 0 AND anyPortSupported LT 0 then
    set L:httpPort 9080
    info "To use 0 to listen on any available port, you need NeXtMidas 2.7.0 or later, setting to port ^{httpPort}"
  endif

  pipe on
    ! added /AUXLIST=AUX.READ to that we can test Bug 2253 and Bug 2254
    httpserv/msgid=main/mname="httpresource" port=^httpPort /auxlist=AUX.READ
    if portLabel neqs "" then
      if anyPortSupported GE 0 then
        result ^{portLabel} ^{reg.httpserv.port}
      else
        result ^{portLabel} ^{httpPort}
      endif
    endif
  pipe off

endmacro

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

  if msg.name eqs "STOP_TEST" then
    pipe stop

  elseif msg.name eqs "HTTPGET" then
    pause ^{delaytime}
    set socket msg.data.socket
    set params msg.data.parameters
    invoke,,socket.openSocket()
    invoke,,socket.writeln("<XML><ROW>temp=1</ROW></XML>")
    invoke,,socket.closeSocket()

  elseif msg.name eqs "HTTPPOST" then
    ! test case for Bug 1810 to make sure get GOT POST request
    set socket msg.data.socket
    set params msg.data.parameters

    if testResultsLabel neqs "" then
      result ^{testResultsLabel}.got_post true
    endif

    ! res filedata socket.getPostData("filedata").getDataString()  ! <-- NTN TODO: this should work but does NOT (hence the two line version)
    invoke filedata socket.getPostData("filedata")
    invoke filedata filedata.getDataString()
    assert/text="^reg.main.name: HTTP server expected POST data" expectedTestData EQS filedata

    ! need to doing below so that we don't hang HTTPSERV or client
    invoke,,socket.openSocket()
    invoke,,socket.closeSocket()
  endif
return
