53 lines
		
	
	
	
		
			970 B
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			53 lines
		
	
	
	
		
			970 B
		
	
	
	
		
			Go
		
	
	
	
	
	
|  | package handler | ||
|  | 
 | ||
|  | import ( | ||
|  | 	"errors" | ||
|  | 	"net/http" | ||
|  | 	"net/http/httptest" | ||
|  | 	"testing" | ||
|  | 
 | ||
|  | 	"github.com/gin-gonic/gin" | ||
|  | 	"github.com/stretchr/testify/suite" | ||
|  | ) | ||
|  | 
 | ||
|  | func TestHandler(t *testing.T) { | ||
|  | 	suite.Run(t, new(HandlerTestSuite)) | ||
|  | } | ||
|  | 
 | ||
|  | type HandlerTestSuite struct { | ||
|  | 	suite.Suite | ||
|  | 	w   *httptest.ResponseRecorder | ||
|  | 	ctx *gin.Context | ||
|  | } | ||
|  | 
 | ||
|  | func (s *HandlerTestSuite) SetupTest() { | ||
|  | 	gin.SetMode(gin.TestMode) | ||
|  | 	s.w = httptest.NewRecorder() | ||
|  | 	s.ctx, _ = gin.CreateTestContext(s.w) | ||
|  | 	s.ctx.Request = new(http.Request) | ||
|  | } | ||
|  | 
 | ||
|  | func (s *HandlerTestSuite) TearDownTest() { | ||
|  | 	s.w = nil | ||
|  | 	s.ctx = nil | ||
|  | } | ||
|  | 
 | ||
|  | func (s *HandlerTestSuite) TestHandler() { | ||
|  | 	err := errors.New("Goodbye") | ||
|  | 	HandlerWithErrorWrapper(func(c *gin.Context) error { | ||
|  | 		return err | ||
|  | 	})(s.ctx) | ||
|  | 
 | ||
|  | 	found := s.ctx.Errors.Last() | ||
|  | 	s.Assert().True(errors.Is(found, err)) | ||
|  | } | ||
|  | 
 | ||
|  | func (s *HandlerTestSuite) TestHandlerNil() { | ||
|  | 	HandlerWithErrorWrapper(func(c *gin.Context) error { | ||
|  | 		return nil | ||
|  | 	})(s.ctx) | ||
|  | 
 | ||
|  | 	found := s.ctx.Errors.Last() | ||
|  | 	s.Assert().Nil(found) | ||
|  | } |