frp/test/e2e/framework/request.go

91 lines
1.9 KiB
Go
Raw Normal View History

2020-06-02 22:48:55 +08:00
package framework
import (
2021-03-31 16:57:39 +08:00
"github.com/fatedier/frp/test/e2e/framework/consts"
2020-06-02 22:48:55 +08:00
"github.com/fatedier/frp/test/e2e/pkg/request"
)
2021-03-31 16:57:39 +08:00
func SetRequestProtocol(protocol string) func(*request.Request) {
return func(r *request.Request) {
r.Protocol(protocol)
2020-09-07 14:57:23 +08:00
}
}
2021-03-31 16:57:39 +08:00
func SetRequestPort(port int) func(*request.Request) {
return func(r *request.Request) {
r.Port(port)
2020-09-07 14:57:23 +08:00
}
}
2021-03-31 16:57:39 +08:00
// 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 ExpectResponse(req *request.Request, expectResp []byte, explain ...interface{}) {
ret, err := req.Do()
2020-09-07 14:57:23 +08:00
ExpectNoError(err, explain...)
2021-03-31 16:57:39 +08:00
ExpectEqualValues(expectResp, ret, explain...)
2020-09-07 14:57:23 +08:00
}
2021-03-31 16:57:39 +08:00
func ExpectResponseError(req *request.Request, explain ...interface{}) {
_, err := req.Do()
2020-09-07 14:57:23 +08:00
ExpectError(err, explain...)
}
2021-03-31 16:57:39 +08:00
type RequestExpect struct {
req *request.Request
f *Framework
expectResp []byte
expectError bool
explain []interface{}
2020-09-07 14:57:23 +08:00
}
2021-03-31 16:57:39 +08:00
func NewRequestExpect(f *Framework) *RequestExpect {
return &RequestExpect{
req: NewRequest(),
f: f,
expectResp: []byte(consts.TestString),
expectError: false,
explain: make([]interface{}, 0),
}
}
func (e *RequestExpect) RequestModify(f func(r *request.Request)) *RequestExpect {
2021-03-31 16:57:39 +08:00
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) ExpectResp(resp []byte) *RequestExpect {
e.expectResp = resp
return e
}
2021-03-31 16:57:39 +08:00
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...)
}
2020-06-02 22:48:55 +08:00
}