github.com/wangkui503/aero@v1.0.0/Middleware_test.go (about)

     1  package aero_test
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"github.com/aerogo/aero"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestApplicationMiddleware(t *testing.T) {
    12  	app := aero.New()
    13  
    14  	// Register route
    15  	app.Get("/", func(ctx *aero.Context) string {
    16  		return ctx.Text(helloWorld)
    17  	})
    18  
    19  	// Register middleware
    20  	app.Use(func(ctx *aero.Context, next func()) {
    21  		ctx.StatusCode = http.StatusPermanentRedirect
    22  		next()
    23  	})
    24  
    25  	// Get response
    26  	response := getResponse(app, "/")
    27  
    28  	// Verify response
    29  	assert.Equal(t, http.StatusPermanentRedirect, response.Code)
    30  	assert.Equal(t, helloWorld, response.Body.String())
    31  }
    32  
    33  func TestApplicationMiddlewareSkipNext(t *testing.T) {
    34  	app := aero.New()
    35  
    36  	// Register route
    37  	app.Get("/", func(ctx *aero.Context) string {
    38  		return ctx.Text(helloWorld)
    39  	})
    40  
    41  	// Register middleware
    42  	app.Use(func(ctx *aero.Context, next func()) {
    43  		// Not calling next() will stop the response chain
    44  	})
    45  
    46  	// Get response
    47  	response := getResponse(app, "/")
    48  
    49  	// Verify response
    50  	assert.Equal(t, "", response.Body.String())
    51  }