#!/usr/bin/env bats

setup() {
  ENDPOINT="https://mocus.dynv6.net/hello"
}

@test "helloVoid exact match" {
  response=$(curl -s -X POST \
    -H "Content-Type: text/xml;charset=UTF-8" \
    -H "SOAPAction: helloVoid" \
    --data @<(cat <<EOF
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:ws="http://ws/">
  <soapenv:Body>
    <ws:helloVoid/>
  </soapenv:Body>
</soapenv:Envelope>
EOF
) "$ENDPOINT")

  returned=$(echo "$response" \
    | xmllint --xpath "string(//return)" - 2>/dev/null)

  echo "Extracted return value: $returned" >&3

  [[ "$returned" = "Hello, world!" ]]
}

@test "helloName exact match" {
  name="Bob"
  response=$(curl -s -X POST \
    -H "Content-Type: text/xml;charset=UTF-8" \
    -H "SOAPAction: helloName" \
    --data @<(cat <<EOF
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:ws="http://ws/">
  <soapenv:Body>
    <ws:helloName>
      <arg0>$name</arg0>
    </ws:helloName>
  </soapenv:Body>
</soapenv:Envelope>
EOF
) "$ENDPOINT")

  returned=$(echo "$response" \
    | xmllint --xpath "string(//return)" - 2>/dev/null)

  echo "Extracted return value: $returned" >&3

  [[ "$returned" = "Hello, $name!" ]]
}
