This repository was archived by the owner on Jun 24, 2026. It is now read-only.
forked from katanomi/pkg
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp_test.go
More file actions
158 lines (130 loc) · 4.28 KB
/
Copy pathapp_test.go
File metadata and controls
158 lines (130 loc) · 4.28 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright 2021 The AlaudaDevops Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sharedmain
import (
"context"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/AlaudaDevops/pkg/fieldindexer"
"github.com/emicklei/go-restful/v3"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var _ = Describe("AppBuilder", func() {
var (
appBuilder *AppBuilder
)
BeforeEach(func() {
appBuilder = &AppBuilder{}
})
Context("BuiltInFilters", func() {
It("should return filter that disables pretty print", func() {
filters := appBuilder.BuiltInFilters()
Expect(filters).To(HaveLen(1))
req := restful.NewRequest(httptest.NewRequest(http.MethodGet, "/", nil))
recorder := httptest.NewRecorder()
resp := restful.NewResponse(recorder)
chain := &restful.FilterChain{
Filters: []restful.FilterFunction{},
Target: func(req *restful.Request, resp *restful.Response) {
resp.ResponseWriter.Write([]byte("test"))
},
}
filters[0](req, resp, chain)
respValue := reflect.ValueOf(resp).Elem()
prettyPrintField := respValue.FieldByName("prettyPrint")
Expect(prettyPrintField.Bool()).To(BeFalse())
Expect(recorder.Body.String()).To(Equal("test"))
})
})
})
func TestAppWithFieldIndexer(t *testing.T) {
t.Run("append one field indexer ", func(t *testing.T) {
g := NewGomegaWithT(t)
a := App("test").WithFieldIndexer(fieldindexer.FieldIndexer{
Obj: &corev1.ConfigMap{},
Field: "data.key",
ExtractValue: func(object client.Object) []string {
return []string{object.(*corev1.ConfigMap).Data["key"]}
},
})
g.Expect(a.fieldIndexeres).Should(HaveLen(1))
})
t.Run("append more than one field indexer", func(t *testing.T) {
g := NewGomegaWithT(t)
a := App("test").WithFieldIndexer(fieldindexer.FieldIndexer{
Obj: &corev1.ConfigMap{},
Field: "data.key",
ExtractValue: func(object client.Object) []string {
return []string{object.(*corev1.ConfigMap).Data["key"]}
},
}).WithFieldIndexer(fieldindexer.FieldIndexer{
Obj: &corev1.ConfigMap{},
Field: "data.name",
ExtractValue: func(object client.Object) []string {
return []string{object.(*corev1.ConfigMap).Data["name"]}
},
})
g.Expect(a.fieldIndexeres).Should(HaveLen(2))
})
}
func TestAppContextOpts(t *testing.T) {
t.Run("empty function list should return directly", func(t *testing.T) {
g := NewGomegaWithT(t)
a := &AppBuilder{}
result := a.ContextOpts()
g.Expect(result).Should(BeIdenticalTo(a))
g.Expect(a.Context).Should(BeNil())
})
t.Run("single context modification function", func(t *testing.T) {
g := NewGomegaWithT(t)
a := &AppBuilder{
Context: context.Background(),
}
type testKey struct{}
testValue := "test-value"
result := a.ContextOpts(func(ctx context.Context) context.Context {
return context.WithValue(ctx, testKey{}, testValue)
})
g.Expect(result).Should(BeIdenticalTo(a))
g.Expect(a.Context.Value(testKey{})).Should(Equal(testValue))
})
t.Run("multiple context modification functions applied in order", func(t *testing.T) {
g := NewGomegaWithT(t)
a := &AppBuilder{
Context: context.Background(),
}
type key1 struct{}
type key2 struct{}
type key3 struct{}
result := a.ContextOpts(
func(ctx context.Context) context.Context {
return context.WithValue(ctx, key1{}, "value1")
},
func(ctx context.Context) context.Context {
return context.WithValue(ctx, key2{}, "value2")
},
func(ctx context.Context) context.Context {
return context.WithValue(ctx, key3{}, "value3")
},
)
g.Expect(result).Should(BeIdenticalTo(a))
g.Expect(a.Context.Value(key1{})).Should(Equal("value1"))
g.Expect(a.Context.Value(key2{})).Should(Equal("value2"))
g.Expect(a.Context.Value(key3{})).Should(Equal("value3"))
})
}