request.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package framework
  2. import (
  3. "github.com/fatedier/frp/test/e2e/framework/consts"
  4. "github.com/fatedier/frp/test/e2e/pkg/request"
  5. )
  6. func SetRequestProtocol(protocol string) func(*request.Request) {
  7. return func(r *request.Request) {
  8. r.Protocol(protocol)
  9. }
  10. }
  11. func SetRequestPort(port int) func(*request.Request) {
  12. return func(r *request.Request) {
  13. r.Port(port)
  14. }
  15. }
  16. // NewRequest return a default TCP request with default timeout and content.
  17. func NewRequest() *request.Request {
  18. return request.New().
  19. Timeout(consts.DefaultTimeout).
  20. Body([]byte(consts.TestString))
  21. }
  22. func ExpectResponse(req *request.Request, expectResp []byte, explain ...interface{}) {
  23. ret, err := req.Do()
  24. ExpectNoError(err, explain...)
  25. ExpectEqualValues(expectResp, ret, explain...)
  26. }
  27. func ExpectResponseError(req *request.Request, explain ...interface{}) {
  28. _, err := req.Do()
  29. ExpectError(err, explain...)
  30. }
  31. type RequestExpect struct {
  32. req *request.Request
  33. f *Framework
  34. expectResp []byte
  35. expectError bool
  36. explain []interface{}
  37. }
  38. func NewRequestExpect(f *Framework) *RequestExpect {
  39. return &RequestExpect{
  40. req: NewRequest(),
  41. f: f,
  42. expectResp: []byte(consts.TestString),
  43. expectError: false,
  44. explain: make([]interface{}, 0),
  45. }
  46. }
  47. func (e *RequestExpect) RequestModify(f func(r *request.Request)) *RequestExpect {
  48. f(e.req)
  49. return e
  50. }
  51. func (e *RequestExpect) PortName(name string) *RequestExpect {
  52. if e.f != nil {
  53. e.req.Port(e.f.PortByName(name))
  54. }
  55. return e
  56. }
  57. func (e *RequestExpect) ExpectResp(resp []byte) *RequestExpect {
  58. e.expectResp = resp
  59. return e
  60. }
  61. func (e *RequestExpect) ExpectError(expectErr bool) *RequestExpect {
  62. e.expectError = expectErr
  63. return e
  64. }
  65. func (e *RequestExpect) Explain(explain ...interface{}) *RequestExpect {
  66. e.explain = explain
  67. return e
  68. }
  69. func (e *RequestExpect) Ensure() {
  70. if e.expectError {
  71. ExpectResponseError(e.req, e.explain...)
  72. } else {
  73. ExpectResponse(e.req, e.expectResp, e.explain...)
  74. }
  75. }