From e0e3dd17ccc3eb1c3d98319aa599c40cc1702bf1 Mon Sep 17 00:00:00 2001 From: Gunish Matta <33680363+gunishmatta@users.noreply.github.com> Date: Mon, 7 Nov 2022 12:44:11 +0000 Subject: [PATCH] improved http scheme test --- controllers/event_handling_test.go | 66 +++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/controllers/event_handling_test.go b/controllers/event_handling_test.go index b9dd20165..f0ea2cbc5 100644 --- a/controllers/event_handling_test.go +++ b/controllers/event_handling_test.go @@ -53,7 +53,68 @@ func TestEventHandler(t *testing.T) { t.Fatalf("failed to create memory storage") } + httpScheme := "http" + + eventServerTests := []struct { + name string + isHttpEnabled bool + url string + }{ + { + name: "http scheme is enabled", + isHttpEnabled: true, + }, { + name: "http scheme is disabled", + isHttpEnabled: false, + }, + } + for _, eventServerTest := range eventServerTests { + + t.Run(eventServerTest.name, func(t *testing.T) { + + eventServer := server.NewEventServer("127.0.0.1:56789", logf.Log, k8sClient, true, eventServerTest.isHttpEnabled) + + stopCh := make(chan struct{}) + go eventServer.ListenAndServe(stopCh, eventMdlw, store) + + rcvServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + req = r + w.WriteHeader(200) + })) + defer rcvServer.Close() + defer close(stopCh) + + providerKey := types.NamespacedName{ + Name: fmt.Sprintf("provider-%s", randStringRunes(5)), + Namespace: namespace, + } + provider = ¬ifyv1.Provider{ + ObjectMeta: metav1.ObjectMeta{ + Name: providerKey.Name, + Namespace: providerKey.Namespace, + }, + Spec: notifyv1.ProviderSpec{ + Type: "generic", + Address: rcvServer.URL, + }, + } + + webhook_url, err := url.Parse(provider.Spec.Address) + if err != nil { + + if eventServerTest.isHttpEnabled { + g.Expect(webhook_url.Scheme).To(Equal(httpScheme)) + } else { + g.Expect(webhook_url.Scheme).ToNot(Equal(httpScheme)) + } + + } + + }) + } + eventServer := server.NewEventServer("127.0.0.1:56789", logf.Log, k8sClient, true, true) + stopCh := make(chan struct{}) go eventServer.ListenAndServe(stopCh, eventMdlw, store) @@ -79,9 +140,8 @@ func TestEventHandler(t *testing.T) { }, } - webhook_url, err := url.Parse(provider.Spec.Address) g.Expect(err).ToNot(HaveOccurred()) - g.Expect(webhook_url.Scheme).To(Equal("http")) + g.Expect(k8sClient.Create(context.Background(), provider)).To(Succeed()) g.Eventually(func() bool { var obj notifyv1.Provider @@ -178,6 +238,7 @@ func TestEventHandler(t *testing.T) { res, err := http.Post("http://localhost:56789/", "application/json", buf) g.Expect(err).ToNot(HaveOccurred()) g.Expect(res.StatusCode).To(Equal(202)) // event_server responds with 202 Accepted + } testForwarded := func() { @@ -299,4 +360,5 @@ func TestEventHandler(t *testing.T) { req = nil }) } + }