-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
requestid_test.go
71 lines (60 loc) · 1.84 KB
/
requestid_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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())
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())
changeID.Get("/", h)
}
const expectedClientSentID = "client sent id"
clientSentID := app.Party("/client_id")
{
clientSentID.Use(requestid.New())
clientSentID.Get("/", h)
}
e := httptest.New(t, app)
e.GET("/default").Expect().Status(httptest.StatusOK).Body().NotEmpty()
e.GET("/custom").Expect().Status(httptest.StatusOK).Body().IsEqual(expectedCustomID)
e.GET("/custom_err").Expect().Status(httptest.StatusUnauthorized).Body().IsEqual(expectedErrMsg)
e.GET("/custom_change_id").Expect().Status(httptest.StatusOK).Body().IsEqual(expectedCustomIDFromOtherMiddleware)
e.GET("/client_id").WithHeader("X-Request-Id", expectedClientSentID).Expect().Header("X-Request-Id").IsEqual(expectedClientSentID)
}