github.com/wfusion/gofusion@v1.1.14/test/http/test.go (about)

     1  package http
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"reflect"
     8  	"sync"
     9  	_ "unsafe"
    10  
    11  	"github.com/gin-gonic/gin"
    12  	"github.com/stretchr/testify/suite"
    13  	"go.uber.org/atomic"
    14  
    15  	"github.com/wfusion/gofusion/common/utils"
    16  	"github.com/wfusion/gofusion/log"
    17  	"github.com/wfusion/gofusion/test"
    18  
    19  	fusHtp "github.com/wfusion/gofusion/http"
    20  )
    21  
    22  var (
    23  	component = "http"
    24  )
    25  
    26  type Test struct {
    27  	test.Suite
    28  
    29  	once  sync.Once
    30  	exits []func()
    31  
    32  	testName   string
    33  	testsLefts atomic.Int64
    34  }
    35  
    36  func (t *Test) SetupTest() {
    37  	t.Catch(func() {
    38  		log.Info(context.Background(), fmt.Sprintf("------------ %s test case begin ------------", component))
    39  
    40  		t.once.Do(func() {
    41  			t.exits = append(t.exits, t.Suite.Copy(t.ConfigFiles(), t.testName, 1))
    42  		})
    43  
    44  		t.exits = append(t.exits, t.Suite.Init(t.ConfigFiles(), t.testName, 1))
    45  	})
    46  }
    47  
    48  func (t *Test) TearDownTest() {
    49  	t.Catch(func() {
    50  		log.Info(context.Background(), fmt.Sprintf("------------ %s test case end ------------", component))
    51  		if t.testsLefts.Add(-1) == 0 {
    52  			for i := len(t.exits) - 1; i >= 0; i-- {
    53  				t.exits[i]()
    54  			}
    55  		}
    56  	})
    57  }
    58  
    59  func (t *Test) AppName() string {
    60  	return fmt.Sprintf("%s.%s", component, t.testName)
    61  }
    62  
    63  func (t *Test) Init(testingSuite suite.TestingSuite) {
    64  	methodFinder := reflect.TypeOf(testingSuite)
    65  	numMethod := methodFinder.NumMethod()
    66  
    67  	numTestLeft := int64(0)
    68  	for i := 0; i < numMethod; i++ {
    69  		method := methodFinder.Method(i)
    70  		ok, _ := test.MethodFilter(method.Name)
    71  		if !ok {
    72  			continue
    73  		}
    74  		numTestLeft++
    75  	}
    76  	t.testName = utils.IndirectType(methodFinder).Name()
    77  	t.testsLefts.Add(numTestLeft)
    78  }
    79  
    80  func (t *Test) ServerGiven(method, path string, hd any) (r fusHtp.IRouter) {
    81  	r = newRouter(context.Background(), gin.New(), t.AppName(), 0, -1)
    82  	switch method {
    83  	case http.MethodGet:
    84  		r.GET(path, hd)
    85  	case http.MethodPost:
    86  		r.POST(path, hd)
    87  	case http.MethodDelete:
    88  		r.DELETE(path, hd)
    89  	case http.MethodPatch:
    90  		r.PATCH(path, hd)
    91  	case http.MethodPut:
    92  		r.PUT(path, hd)
    93  	case http.MethodOptions:
    94  		r.OPTIONS(path, hd)
    95  	case http.MethodHead:
    96  		r.HEAD(path, hd)
    97  	case "Any":
    98  		r.Any(path, hd)
    99  	case "Handle":
   100  		r.Handle(path, hd)
   101  	case "File":
   102  		r.StaticFile(path, hd.(string))
   103  	}
   104  	return
   105  }
   106  
   107  //go:linkname newRouter github.com/wfusion/gofusion/http.newRouter
   108  func newRouter(context.Context, gin.IRouter, string, int, int) fusHtp.IRouter