mirror of
https://github.com/fatedier/frp.git
synced 2025-07-27 07:35:07 +00:00
more e2e tests (#1845)
This commit is contained in:
@@ -10,6 +10,7 @@ type Process struct {
|
||||
cmd *exec.Cmd
|
||||
cancel context.CancelFunc
|
||||
errorOutput *bytes.Buffer
|
||||
stdOutput *bytes.Buffer
|
||||
|
||||
beforeStopHandler func()
|
||||
}
|
||||
@@ -22,7 +23,9 @@ func New(path string, params []string) *Process {
|
||||
cancel: cancel,
|
||||
}
|
||||
p.errorOutput = bytes.NewBufferString("")
|
||||
p.stdOutput = bytes.NewBufferString("")
|
||||
cmd.Stderr = p.errorOutput
|
||||
cmd.Stdout = p.stdOutput
|
||||
return p
|
||||
}
|
||||
|
||||
@@ -42,6 +45,10 @@ func (p *Process) ErrorOutput() string {
|
||||
return p.errorOutput.String()
|
||||
}
|
||||
|
||||
func (p *Process) StdOutput() string {
|
||||
return p.stdOutput.String()
|
||||
}
|
||||
|
||||
func (p *Process) SetBeforeStopHandler(fn func()) {
|
||||
p.beforeStopHandler = fn
|
||||
}
|
||||
|
@@ -6,26 +6,35 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func SendTCPRequest(port int, content []byte, timeout time.Duration) (res string, err error) {
|
||||
func SendTCPRequest(port int, content []byte, timeout time.Duration) (string, error) {
|
||||
c, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
||||
if err != nil {
|
||||
err = fmt.Errorf("connect to tcp server error: %v", err)
|
||||
return
|
||||
return "", fmt.Errorf("connect to tcp server error: %v", err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
c.SetDeadline(time.Now().Add(timeout))
|
||||
return sendTCPRequestByConn(c, content)
|
||||
return sendRequestByConn(c, content)
|
||||
}
|
||||
|
||||
func sendTCPRequestByConn(c net.Conn, content []byte) (res string, err error) {
|
||||
func SendUDPRequest(port int, content []byte, timeout time.Duration) (string, error) {
|
||||
c, err := net.Dial("udp", fmt.Sprintf("127.0.0.1:%d", port))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("connect to udp server error: %v", err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
c.SetDeadline(time.Now().Add(timeout))
|
||||
return sendRequestByConn(c, content)
|
||||
}
|
||||
|
||||
func sendRequestByConn(c net.Conn, content []byte) (string, error) {
|
||||
c.Write(content)
|
||||
|
||||
buf := make([]byte, 2048)
|
||||
n, errRet := c.Read(buf)
|
||||
if errRet != nil {
|
||||
err = fmt.Errorf("read from tcp error: %v", errRet)
|
||||
return
|
||||
n, err := c.Read(buf)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read error: %v", err)
|
||||
}
|
||||
return string(buf[:n]), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user