Skip to content

Commit e0e3dd1

Browse files
committed
improved http scheme test
1 parent 7cbd2eb commit e0e3dd1

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

controllers/event_handling_test.go

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,68 @@ func TestEventHandler(t *testing.T) {
5353
t.Fatalf("failed to create memory storage")
5454
}
5555

56+
httpScheme := "http"
57+
58+
eventServerTests := []struct {
59+
name string
60+
isHttpEnabled bool
61+
url string
62+
}{
63+
{
64+
name: "http scheme is enabled",
65+
isHttpEnabled: true,
66+
}, {
67+
name: "http scheme is disabled",
68+
isHttpEnabled: false,
69+
},
70+
}
71+
for _, eventServerTest := range eventServerTests {
72+
73+
t.Run(eventServerTest.name, func(t *testing.T) {
74+
75+
eventServer := server.NewEventServer("127.0.0.1:56789", logf.Log, k8sClient, true, eventServerTest.isHttpEnabled)
76+
77+
stopCh := make(chan struct{})
78+
go eventServer.ListenAndServe(stopCh, eventMdlw, store)
79+
80+
rcvServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
81+
req = r
82+
w.WriteHeader(200)
83+
}))
84+
defer rcvServer.Close()
85+
defer close(stopCh)
86+
87+
providerKey := types.NamespacedName{
88+
Name: fmt.Sprintf("provider-%s", randStringRunes(5)),
89+
Namespace: namespace,
90+
}
91+
provider = &notifyv1.Provider{
92+
ObjectMeta: metav1.ObjectMeta{
93+
Name: providerKey.Name,
94+
Namespace: providerKey.Namespace,
95+
},
96+
Spec: notifyv1.ProviderSpec{
97+
Type: "generic",
98+
Address: rcvServer.URL,
99+
},
100+
}
101+
102+
webhook_url, err := url.Parse(provider.Spec.Address)
103+
if err != nil {
104+
105+
if eventServerTest.isHttpEnabled {
106+
g.Expect(webhook_url.Scheme).To(Equal(httpScheme))
107+
} else {
108+
g.Expect(webhook_url.Scheme).ToNot(Equal(httpScheme))
109+
}
110+
111+
}
112+
113+
})
114+
}
115+
56116
eventServer := server.NewEventServer("127.0.0.1:56789", logf.Log, k8sClient, true, true)
117+
57118
stopCh := make(chan struct{})
58119
go eventServer.ListenAndServe(stopCh, eventMdlw, store)
59120

@@ -79,9 +140,8 @@ func TestEventHandler(t *testing.T) {
79140
},
80141
}
81142

82-
webhook_url, err := url.Parse(provider.Spec.Address)
83143
g.Expect(err).ToNot(HaveOccurred())
84-
g.Expect(webhook_url.Scheme).To(Equal("http"))
144+
85145
g.Expect(k8sClient.Create(context.Background(), provider)).To(Succeed())
86146
g.Eventually(func() bool {
87147
var obj notifyv1.Provider
@@ -178,6 +238,7 @@ func TestEventHandler(t *testing.T) {
178238
res, err := http.Post("http://localhost:56789/", "application/json", buf)
179239
g.Expect(err).ToNot(HaveOccurred())
180240
g.Expect(res.StatusCode).To(Equal(202)) // event_server responds with 202 Accepted
241+
181242
}
182243

183244
testForwarded := func() {
@@ -299,4 +360,5 @@ func TestEventHandler(t *testing.T) {
299360
req = nil
300361
})
301362
}
363+
302364
}

0 commit comments

Comments
 (0)