-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware_test.go
136 lines (108 loc) · 3.03 KB
/
middleware_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package raiden_test
import (
"context"
"testing"
"github.com/sev-2/raiden"
"github.com/sev-2/raiden/pkg/mock"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
"go.opentelemetry.io/otel/trace"
)
func TestNewChain(t *testing.T) {
c := raiden.NewChain(m1, m2)
assert.NotNil(t, c)
}
func TestChain_Append(t *testing.T) {
c := raiden.NewChain(m1, m2)
assert.NotNil(t, c.Append(m3, m4))
}
func TestChain_Prepend(t *testing.T) {
c := raiden.NewChain(m3, m4)
assert.NotNil(t, c.Prepend(m1, m2))
}
func TestChain_Then(t *testing.T) {
// Create a new chain with two middlewares
c := raiden.NewChain(m1, m2)
// setup required data
controller := &HelloWorldController{}
// Call Then
fn := c.Then("GET", raiden.RouteTypeCustom, controller)
// Test the returned function
assert.NotNil(t, fn)
}
func Test_Tracer(t *testing.T) {
a := raiden.NewChain(m1, m2)
breakerMiddleware := raiden.BreakerMiddleware("/some-path")
a.Append(breakerMiddleware)
controller := &HelloWorldController{}
fn := a.Then("GET", raiden.RouteTypeCustom, controller)
tracedChain := raiden.TraceMiddleware(fn)
assert.NotNil(t, tracedChain)
mockCtx := &mock.MockContext{
CtxFn: func() context.Context {
return context.Background()
},
SetCtxFn: func(ctx context.Context) {},
SetSpanFn: func(span trace.Span) {},
SendErrorWithCodeFn: func(statusCode int, err error) error {
return nil
},
TracerFn: func() trace.Tracer {
noopProvider := trace.NewNoopTracerProvider()
tracer := noopProvider.Tracer("test")
return tracer
},
ConfigFn: func() *raiden.Config {
return &raiden.Config{
DeploymentTarget: raiden.DeploymentTargetCloud,
ProjectId: "test-project-id",
ProjectName: "My Great Project",
SupabaseApiBasePath: "/v1",
SupabaseApiUrl: "http://supabase.cloud.com",
SupabasePublicUrl: "http://supabase.cloud.com",
CorsAllowedOrigins: "*",
CorsAllowedMethods: "GET, POST, PUT, DELETE, OPTIONS",
CorsAllowedHeaders: "X-Requested-With, Content-Type, Authorization",
}
},
RequestContextFn: func() *fasthttp.RequestCtx {
return &fasthttp.RequestCtx{}
},
SendJsonFn: func(data any) error {
return nil
},
}
breakerFn := breakerMiddleware(fn)
breakerRes := breakerFn(mockCtx)
assert.Nil(t, breakerRes)
initRes := fn(mockCtx)
assert.Nil(t, initRes)
res := tracedChain(mockCtx)
assert.Nil(t, res)
corsFn := raiden.CorsMiddleware(mockCtx.Config())
corsFn(&fasthttp.RequestCtx{
Request: fasthttp.Request{
Header: fasthttp.RequestHeader{},
},
})
}
func m1(next raiden.RouteHandlerFn) raiden.RouteHandlerFn {
return func(ctx raiden.Context) error {
return next(ctx)
}
}
func m2(next raiden.RouteHandlerFn) raiden.RouteHandlerFn {
return func(ctx raiden.Context) error {
return next(ctx)
}
}
func m3(next raiden.RouteHandlerFn) raiden.RouteHandlerFn {
return func(ctx raiden.Context) error {
return next(ctx)
}
}
func m4(next raiden.RouteHandlerFn) raiden.RouteHandlerFn {
return func(ctx raiden.Context) error {
return next(ctx)
}
}