Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diversario committed Nov 30, 2023
1 parent 8d33460 commit ea011fc
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prometheus

import (
"encoding/base64"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -33,6 +34,56 @@ var _ = Describe("Tests for Prometheus", func() {
Expect(count).To(BeEquivalentTo(0))
Expect(err).To(BeNil())
})

It("Test2 bearer header is used when token is provided", func() {
url := "https://example.com/api"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
_, err = bat.RoundTrip(req)
Expect(req.Header.Get("Authorization")).To(Equal("Bearer someRandomToken"))
//Asserting no of times mocks are called
Expect(count).To(BeEquivalentTo(0))
Expect(err).To(BeNil())
})

It("Test3 basic auth header is used when no token is provided", func() {
bat.token = ""
url := "https://example.com/api"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
_, err = bat.RoundTrip(req)

encodedAuthHeader := base64.StdEncoding.EncodeToString([]byte("someRandomUsername:someRandomPassword"))

Expect(req.Header.Get("Authorization")).To(Equal("Basic " + encodedAuthHeader))
//Asserting no of times mocks are called
Expect(count).To(BeEquivalentTo(0))
Expect(err).To(BeNil())
})

It("Test4 no auth header set when auth details are omitted", func() {
bat.token = ""
bat.username = ""
bat.password = ""
url := "https://example.com/api"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
_, err = bat.RoundTrip(req)

Expect(req.Header.Get("Authorization")).To(Equal(""))
//Asserting no of times mocks are called
Expect(count).To(BeEquivalentTo(0))
Expect(err).To(BeNil())
})
})

Context("Tests for NewClient()", func() {
Expand Down

0 comments on commit ea011fc

Please sign in to comment.