mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 20:07:04 +00:00
add a new simple, builtin requestid middleware (makes use of the Context.SetID/GetID methods too)
Former-commit-id: d46bce7c1964adada01934aa95daf389c141defc
This commit is contained in:
63
middleware/requestid/requestid_test.go
Normal file
63
middleware/requestid/requestid_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package requestid_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/v12/httptest"
|
||||
"github.com/kataras/iris/v12/middleware/requestid"
|
||||
)
|
||||
|
||||
func TestRequestID(t *testing.T) {
|
||||
app := iris.New()
|
||||
h := func(ctx iris.Context) {
|
||||
ctx.WriteString(requestid.Get(ctx))
|
||||
}
|
||||
|
||||
def := app.Party("/default")
|
||||
{
|
||||
def.Use(requestid.New(nil))
|
||||
def.Get("/", h)
|
||||
}
|
||||
|
||||
const expectedCustomID = "my_id"
|
||||
custom := app.Party("/custom")
|
||||
{
|
||||
customGen := func(ctx context.Context) string {
|
||||
return expectedCustomID
|
||||
}
|
||||
|
||||
custom.Use(requestid.New(customGen))
|
||||
custom.Get("/", h)
|
||||
}
|
||||
|
||||
const expectedErrMsg = "no id"
|
||||
customWithErr := app.Party("/custom_err")
|
||||
{
|
||||
customGen := func(ctx context.Context) string {
|
||||
ctx.StopWithText(iris.StatusUnauthorized, expectedErrMsg)
|
||||
return ""
|
||||
}
|
||||
|
||||
customWithErr.Use(requestid.New(customGen))
|
||||
customWithErr.Get("/", h)
|
||||
}
|
||||
|
||||
const expectedCustomIDFromOtherMiddleware = "my custom id"
|
||||
changeID := app.Party("/custom_change_id")
|
||||
{
|
||||
changeID.Use(func(ctx iris.Context) {
|
||||
ctx.SetID(expectedCustomIDFromOtherMiddleware)
|
||||
ctx.Next()
|
||||
})
|
||||
changeID.Use(requestid.New(nil))
|
||||
changeID.Get("/", h)
|
||||
}
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/default").Expect().Status(httptest.StatusOK).Body().NotEmpty()
|
||||
e.GET("/custom").Expect().Status(httptest.StatusOK).Body().Equal(expectedCustomID)
|
||||
e.GET("/custom_err").Expect().Status(httptest.StatusUnauthorized).Body().Equal(expectedErrMsg)
|
||||
e.GET("/custom_change_id").Expect().Status(httptest.StatusOK).Body().Equal(expectedCustomIDFromOtherMiddleware)
|
||||
}
|
||||
Reference in New Issue
Block a user