| 9 | "github.com/fatedier/frp/test/e2e/pkg/request" |
| 10 | ) |
| 11 |
| 12 | func SpecifiedHTTPBodyHandler(body []byte) http.HandlerFunc { |
| 13 | return func(w http.ResponseWriter, req *http.Request) { |
| 14 | _, _ = w.Write(body) |
| 15 | } |
| 16 | } |
| 17 |
| 18 | func ExpectResponseCode(code int) EnsureFunc { |
| 19 | return func(resp *request.Response) bool { |
| 20 | if resp.Code == code { |
| 21 | return true |
| 22 | } |
| 23 | flog.Warnf("expect code %d, but got %d", code, resp.Code) |
| 24 | return false |
| 25 | } |
| 26 | } |
| 27 |
| 28 | // NewRequest return a default request with default timeout and content. |
| 29 | func NewRequest() *request.Request { |
| 30 | return request.New(). |
| 31 | Timeout(consts.DefaultTimeout). |
| 32 | Body([]byte(consts.TestString)) |
| 33 | } |
| 34 |
| 35 | func NewHTTPRequest() *request.Request { |
| 36 | return request.New().HTTP().HTTPParams("GET", "", "/", nil) |
| 37 | } |
| 38 |
| 39 | type RequestExpect struct { |
| 40 | req *request.Request |
| 41 |
| 42 | f *Framework |
| 43 | expectResp []byte |
| 44 | expectError bool |
| 45 | explain []any |
| 46 | } |
| 47 |
| 48 | func NewRequestExpect(f *Framework) *RequestExpect { |
| 49 | return &RequestExpect{ |
| 50 | req: NewRequest(), |
| 51 | f: f, |
| 52 | expectResp: []byte(consts.TestString), |
| 53 | expectError: false, |
| 54 | explain: make([]any, 0), |
| 55 | } |
| 56 | } |
| 57 |
| 58 | func (e *RequestExpect) Request(req *request.Request) *RequestExpect { |
| 59 | e.req = req |
| 60 | return e |
| 61 | } |
| 62 |
| 63 | func (e *RequestExpect) RequestModify(f func(r *request.Request)) *RequestExpect { |
| 64 | f(e.req) |
| 65 | return e |
| 66 | } |
| 67 |
| 68 | func (e *RequestExpect) Protocol(protocol string) *RequestExpect { |
| 69 | e.req.Protocol(protocol) |
| 70 | return e |
| 71 | } |
| 72 |
| 73 | func (e *RequestExpect) PortName(name string) *RequestExpect { |
| 74 | if e.f != nil { |
| 75 | e.req.Port(e.f.PortByName(name)) |
| 76 | } |
| 77 | return e |
| 78 | } |
| 79 |
| 80 | func (e *RequestExpect) Port(port int) *RequestExpect { |
| 81 | if e.f != nil { |
| 82 | e.req.Port(port) |
| 83 | } |
| 84 | return e |
| 85 | } |
| 86 |
| 87 | func (e *RequestExpect) ExpectResp(resp []byte) *RequestExpect { |
| 88 | e.expectResp = resp |
| 89 | return e |
| 90 | } |
| 91 |
| 92 | func (e *RequestExpect) ExpectError(expectErr bool) *RequestExpect { |
| 93 | e.expectError = expectErr |
| 94 | return e |
| 95 | } |
| 96 |
| 97 | func (e *RequestExpect) Explain(explain ...any) *RequestExpect { |
| 98 | e.explain = explain |
| 99 | return e |
| 100 | } |
| 101 |
| 102 | type EnsureFunc func(*request.Response) bool |
| 103 |
| 104 | func (e *RequestExpect) Ensure(fns ...EnsureFunc) { |
| 105 | ret, err := e.req.Do() |
| 106 | if e.expectError { |
| 107 | ExpectErrorWithOffset(1, err, e.explain...) |
| 108 | return |
| 109 | } |
| 110 | ExpectNoErrorWithOffset(1, err, e.explain...) |
| 111 |
| 112 | if len(fns) == 0 { |
| 113 | if !bytes.Equal(e.expectResp, ret.Content) { |
| 114 | flog.Tracef("response info: %+v", ret) |
| 115 | } |
| 116 | ExpectEqualValuesWithOffset(1, string(ret.Content), string(e.expectResp), e.explain...) |
| 117 | } else { |
| 118 | for _, fn := range fns { |
| 119 | ok := fn(ret) |
| 120 | if !ok { |
| 121 | flog.Tracef("response info: %+v", ret) |
| 122 | } |
| 123 | ExpectTrueWithOffset(1, ok, e.explain...) |
| 124 | } |
| 125 | } |
| 126 | } |