- test: Test js input
  cleanup:
    -   bn remove returnJSInput
  steps:
    -   in: export BASE_ARGS="-H X-Binaris-Api-Key:${BINARIS_API_KEY}"
    -   in: export TEST_FUNCTION="https://${BINARIS_INVOKE_ENDPOINT}/v2/run/${BINARIS_ACCOUNT_ID}/returnJSInput"
    -   in: bn create node8 returnJSInput
    -   in: 'echo "exports.handler = (input) => ({ inputType: typeof input, input});" >function.js'
    -   in: bn deploy returnJSInput
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"inputType":"object","input":{}}'
    -   in: curl -s --data '{"' ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"inputType":"undefined"}'
    -   in: curl -s --data '{}' ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"inputType":"object","input":{}}'
    -   in: curl -s --data '{"foo":"bar","yet":123}' ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"inputType":"object","input":{"foo":"bar","yet":123}}'
    -   in: curl -s --data '["foo","bar"]' ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"inputType":"object","input":["foo","bar"]}'

- test: Test js method 
  cleanup:
    -   bn remove returnJSMethod
  steps:
    -   in: export BASE_ARGS="-H X-Binaris-Api-Key:${BINARIS_API_KEY}"
    -   in: export TEST_FUNCTION="https://${BINARIS_INVOKE_ENDPOINT}/v2/run/${BINARIS_ACCOUNT_ID}/returnJSMethod"
    -   in: bn create node8 returnJSMethod
    -   in: 'echo "exports.handler = (_, ctx) => ctx.request.method;" >function.js'
    -   in: bn deploy returnJSMethod
    -   in: curl -s -X GET ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '"GET"'
    -   in: curl -s -X POST ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '"POST"'
    -   in: curl -s -X PUT ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '"PUT"'
    -   in: curl -s -X DELETE ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '"DELETE"'
    -   in: curl -s -X OPTIONS ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '"OPTIONS"'
    -   in: curl -s -X PATCH ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '"PATCH"'

- test: Test js data input
  cleanup:
    -   bn remove returnJSReqBody
  steps:
    -   in: export BASE_ARGS="-H X-Binaris-Api-Key:${BINARIS_API_KEY}"
    -   in: export TEST_FUNCTION="https://${BINARIS_INVOKE_ENDPOINT}/v2/run/${BINARIS_ACCOUNT_ID}/returnJSReqBody"
    -   in: bn create node8 returnJSReqBody
    -   in: 'echo "exports.handler = (_, ctx) => ctx.request.body;" >function.js'
    -   in: bn deploy returnJSReqBody
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"type":"Buffer","data":[]}'
    -   in: curl -s --data '{' ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"type":"Buffer","data":[123]}'
    -   in: curl -s --data '{}' ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"type":"Buffer","data":[123,125]}'
    -   in: curl -s --data ' ' ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"type":"Buffer","data":[32]}'
    -   in: curl -s --data-urlencode ' ' ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"type":"Buffer","data":[37,50,48]}'
    -   in: echo | curl -s --data-binary @- ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"type":"Buffer","data":[10]}'
    -   in: echo | gzip -n | curl -s --data-binary @- ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{"type":"Buffer","data":[31,139,8,0,0,0,0,0,0,3,227,2,0,147,6,215,50,1,0,0,0]}'

- test: Test js headers input
  cleanup:
    -   bn remove returnJSReqTestHeader
  steps:
    -   in: export BASE_ARGS="-H X-Binaris-Api-Key:${BINARIS_API_KEY}"
    -   in: export TEST_FUNCTION="https://${BINARIS_INVOKE_ENDPOINT}/v2/run/${BINARIS_ACCOUNT_ID}/returnJSReqTestHeader"
    -   in: bn create node8 returnJSReqTestHeader
    -   in: 'echo "exports.handler = (_, ctx) => ctx.request.headers.test;" >function.js'
    -   in: bn deploy returnJSReqTestHeader
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}"
        out: ''
    -   in: 'curl -s -H "Test;" ${BASE_ARGS} "${TEST_FUNCTION}"'
        out: '""'
    -   in: 'curl -s -H "Test: foo" ${BASE_ARGS} "${TEST_FUNCTION}"'
        out: '"foo"'
    -   in: 'curl -s -H "Test: 123" ${BASE_ARGS} "${TEST_FUNCTION}"'
        out: '"123"'
    -   in: 'curl -s -H "Test: foo" -H "Test: bar" ${BASE_ARGS} "${TEST_FUNCTION}"'
        out: '"foo, bar"'

- test: Test js query input
  cleanup:
    -   bn remove returnJSReqQuery
  steps:
    -   in: export BASE_ARGS="-H X-Binaris-Api-Key:${BINARIS_API_KEY}"
    -   in: export TEST_FUNCTION="https://${BINARIS_INVOKE_ENDPOINT}/v2/run/${BINARIS_ACCOUNT_ID}/returnJSReqQuery"
    -   in: bn create node8 returnJSReqQuery
    -   in: 'echo "exports.handler = (_, ctx) => ctx.request.query;" >function.js'
    -   in: bn deploy returnJSReqQuery
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '{}'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}?foo=123"
        out: '{"foo":"123"}'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}?foo=123&bar=this"
        out: '{"foo":"123","bar":"this"}'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}?foo="
        out: '{"foo":""}'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}/some/where?foo=123"
        out: '{"foo":"123"}'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}/some/where/?foo=123&bar=this"
        out: '{"foo":"123","bar":"this"}'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}?foo="
        out: '{"foo":""}'

- test: Test js path input
  cleanup:
    -   bn remove returnJSReqPath
  steps:
    -   in: export BASE_ARGS="-H X-Binaris-Api-Key:${BINARIS_API_KEY}"
    -   in: export TEST_FUNCTION="https://${BINARIS_INVOKE_ENDPOINT}/v2/run/${BINARIS_ACCOUNT_ID}/returnJSReqPath"
    -   in: bn create node8 returnJSReqPath
    -   in: 'echo "exports.handler = (_, ctx) => ctx.request.path;" >function.js'
    -   in: bn deploy returnJSReqPath
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}"
        out: '"/"'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}/////"
        out: '"/"'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}/foo"
        out: '"/foo"'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}/foo?qs"
        out: '"/foo"'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}/foo/"
        out: '"/foo/"'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}/foo/?qs=1"
        out: '"/foo/"'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}/foo///"
        out: '"/foo/"'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}/foo/bar"
        out: '"/foo/bar"'
    -   in: curl -s ${BASE_ARGS} "${TEST_FUNCTION}/foo/bar/"
        out: '"/foo/bar/"'

- test: Test js custom response
  cleanup:
    -   bn remove customJSResponse
  steps:
    -   in: bn create node8 customJSResponse
    -   in: |-
           cat >function.js << EOL
           exports.handler = async (_, ctx) => {
             return new ctx.Response({
               statusCode: 231,
               headers: {
                 foo: 123,
                 'bar': 'vaz',
                 'but': '',
               },
               body: Buffer.from('no quotes'),
             });
           }
           EOL
    -   in: bn deploy customJSResponse
    -   in: curl -H X-Binaris-Api-Key:${BINARIS_API_KEY} --silent --include "https://${BINARIS_INVOKE_ENDPOINT}/v2/run/${BINARIS_ACCOUNT_ID}/customJSResponse" | grep -e foo -e bar -e but -e HTTP -e quotes
        out: |-
             HTTP/1.1 231 unknown
             foo: 123
             bar: vaz
             but: 
             no quotes
