mirror of
https://github.com/fatedier/frp.git
synced 2026-01-11 22:23:12 +00:00
add e2e tests (#2334)
This commit is contained in:
@@ -1,51 +1,85 @@
|
||||
package framework
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/fatedier/frp/test/e2e/framework/consts"
|
||||
"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 SetRequestProtocol(protocol string) func(*request.Request) {
|
||||
return func(r *request.Request) {
|
||||
r.Protocol(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 SetRequestPort(port int) func(*request.Request) {
|
||||
return func(r *request.Request) {
|
||||
r.Port(port)
|
||||
}
|
||||
}
|
||||
|
||||
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...)
|
||||
// NewRequest return a default TCP request with default timeout and content.
|
||||
func NewRequest() *request.Request {
|
||||
return request.New().
|
||||
Timeout(consts.DefaultTimeout).
|
||||
Body([]byte(consts.TestString))
|
||||
}
|
||||
|
||||
func ExpectTCPRequestError(port int, in []byte, timeout time.Duration, explain ...interface{}) {
|
||||
_, err := request.SendTCPRequest(port, in, timeout)
|
||||
func ExpectResponse(req *request.Request, expectResp []byte, explain ...interface{}) {
|
||||
ret, err := req.Do()
|
||||
ExpectNoError(err, explain...)
|
||||
ExpectEqualValues(expectResp, ret, explain...)
|
||||
}
|
||||
|
||||
func ExpectResponseError(req *request.Request, explain ...interface{}) {
|
||||
_, err := req.Do()
|
||||
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...)
|
||||
type RequestExpect struct {
|
||||
req *request.Request
|
||||
|
||||
f *Framework
|
||||
expectResp []byte
|
||||
expectError bool
|
||||
explain []interface{}
|
||||
}
|
||||
|
||||
func ExpectUDPRequestError(port int, in []byte, timeout time.Duration, explain ...interface{}) {
|
||||
_, err := request.SendUDPRequest(port, in, timeout)
|
||||
ExpectError(err, explain...)
|
||||
func NewRequestExpect(f *Framework) *RequestExpect {
|
||||
return &RequestExpect{
|
||||
req: NewRequest(),
|
||||
f: f,
|
||||
expectResp: []byte(consts.TestString),
|
||||
expectError: false,
|
||||
explain: make([]interface{}, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (e *RequestExpect) Request(f func(r *request.Request)) *RequestExpect {
|
||||
f(e.req)
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *RequestExpect) PortName(name string) *RequestExpect {
|
||||
if e.f != nil {
|
||||
e.req.Port(e.f.PortByName(name))
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *RequestExpect) ExpectError(expectErr bool) *RequestExpect {
|
||||
e.expectError = expectErr
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *RequestExpect) Explain(explain ...interface{}) *RequestExpect {
|
||||
e.explain = explain
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *RequestExpect) Ensure() {
|
||||
if e.expectError {
|
||||
ExpectResponseError(e.req, e.explain...)
|
||||
} else {
|
||||
ExpectResponse(e.req, e.expectResp, e.explain...)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user