package framework

import (
	"time"

	"github.com/fatedier/frp/test/e2e/pkg/request"
)

func ExpectRequest(protocol string, port int, in, out []byte, timeout time.Duration, explain ...interface{}) {
	switch protocol {
	case "tcp":
		ExpectTCPRequest(port, in, out, timeout, explain...)
	case "udp":
		ExpectUDPRequest(port, in, out, timeout, explain...)
	default:
		Failf("ExpectRequest not support protocol: %s", protocol)
	}
}

func ExpectRequestError(protocol string, port int, in []byte, timeout time.Duration, explain ...interface{}) {
	switch protocol {
	case "tcp":
		ExpectTCPRequestError(port, in, timeout, explain...)
	case "udp":
		ExpectUDPRequestError(port, in, timeout, explain...)
	default:
		Failf("ExpectRequestError not support protocol: %s", protocol)
	}
}

func ExpectTCPRequest(port int, in, out []byte, timeout time.Duration, explain ...interface{}) {
	res, err := request.SendTCPRequest(port, in, timeout)
	ExpectNoError(err, explain...)
	ExpectEqual(string(out), res, explain...)
}

func ExpectTCPRequestError(port int, in []byte, timeout time.Duration, explain ...interface{}) {
	_, err := request.SendTCPRequest(port, in, timeout)
	ExpectError(err, explain...)
}

func ExpectUDPRequest(port int, in, out []byte, timeout time.Duration, explain ...interface{}) {
	res, err := request.SendUDPRequest(port, in, timeout)
	ExpectNoError(err, explain...)
	ExpectEqual(string(out), res, explain...)
}

func ExpectUDPRequestError(port int, in []byte, timeout time.Duration, explain ...interface{}) {
	_, err := request.SendUDPRequest(port, in, timeout)
	ExpectError(err, explain...)
}