request.go 2.7 KB

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