Skip to content

Commit

Permalink
improved http scheme test
Browse files Browse the repository at this point in the history
  • Loading branch information
gunishmatta committed Nov 7, 2022
1 parent 7cbd2eb commit e0e3dd1
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions controllers/event_handling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = &notifyv1.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)

Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -299,4 +360,5 @@ func TestEventHandler(t *testing.T) {
req = nil
})
}

}

0 comments on commit e0e3dd1

Please sign in to comment.