Skip to content

Commit

Permalink
Refactor HTTP client variable names and usage in eventgenerator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bonzofenix committed Jul 29, 2024
1 parent 080ece1 commit 45d9256
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ var (
regPath = regexp.MustCompile(`^/v1/apps/.*/scale$`)
configFile *os.File
conf config.Config
egPort int

httpClientForPublicApi *http.Client

healthHttpClient *http.Client
mockLogCache *testhelpers.MockLogCache
mockScalingEngine *ghttp.Server
breachDurationSecs = 10
Expand All @@ -66,11 +62,9 @@ var _ = SynchronizedBeforeSuite(func() []byte {
initDB()
return []byte(eg)
}, func(pathByte []byte) {
healthHttpClient = &http.Client{}
egPath = string(pathByte)
initHttpEndPoints()
initConfig()
httpClientForPublicApi = testhelpers.NewPublicApiClient()
})

var _ = SynchronizedAfterSuite(func() {
Expand Down Expand Up @@ -240,7 +234,7 @@ func initHttpEndPoints() {
func initConfig() {
testCertDir := testhelpers.TestCertFolder()

egPort = 7000 + GinkgoParallelProcess()
egPort := 7000 + GinkgoParallelProcess()
dbUrl := testhelpers.GetDbUrl()
conf = config.Config{
Logging: helpers.LoggingConfig{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import (

var _ = Describe("Eventgenerator", func() {
var (
runner *EventGeneratorRunner
httpsClient *http.Client
serverURL *url.URL
err error
runner *EventGeneratorRunner
httpClientForEventGenerator *http.Client
serverURL *url.URL
err error
)

BeforeEach(func() {
runner = NewEventGeneratorRunner()
httpsClient = testhelpers.NewEventGeneratorClient()
httpClientForEventGenerator = testhelpers.NewEventGeneratorClient()

serverURL, err = url.Parse("http://127.0.0.1:" + strconv.Itoa(conf.Server.Port))
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -122,7 +122,7 @@ var _ = Describe("Eventgenerator", func() {
})
})

Context("when an interrupt is sent", func() {
When("an interrupt is sent", func() {
BeforeEach(func() {
runner.Start()
})
Expand All @@ -134,15 +134,15 @@ var _ = Describe("Eventgenerator", func() {
})

Describe("EventGenerator REST API", func() {
Context("when a request for aggregated metrics history comes", func() {
When("a request for aggregated metrics history comes", func() {
BeforeEach(func() {
serverURL.User = url.UserPassword(conf.Server.BasicAuth.Username, conf.Server.BasicAuth.Password)
serverURL.Path = "/v1/apps/an-app-id/aggregated_metric_histories/a-metric-type"
runner.Start()
})

It("returns with a 200", func() {
rsp, err := httpClientForPublicApi.Get(serverURL.String())
rsp, err := httpClientForEventGenerator.Get(serverURL.String())
Expect(err).NotTo(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusOK))
rsp.Body.Close()
Expand All @@ -157,7 +157,7 @@ var _ = Describe("Eventgenerator", func() {
serverURL.Path = "/health"
})

Describe("when Health server is ready to serve RESTful API", func() {
When("Health server is ready to serve RESTful API", func() {
BeforeEach(func() {
basicAuthConfig := conf
basicAuthConfig.Health.BasicAuth.Username = ""
Expand All @@ -170,7 +170,7 @@ var _ = Describe("Eventgenerator", func() {

When("a request to query health comes", func() {
It("returns with a 200", func() {
rsp, err := httpsClient.Get(fmt.Sprintf("%s/health", serverURL))
rsp, err := httpClientForEventGenerator.Get(fmt.Sprintf("%s/health", serverURL))
Expect(err).NotTo(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusOK))

Expand All @@ -188,51 +188,51 @@ var _ = Describe("Eventgenerator", func() {
})
})

Describe("when Health server is ready to serve RESTful API with basic Auth", func() {
When("Health server is ready to serve RESTful API with basic Auth", func() {
BeforeEach(func() {
runner.Start()
})

When("when username and password are incorrect for basic authentication during health check", func() {
When("username and password are incorrect for basic authentication during health check", func() {
It("should return 401", func() {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/health", serverURL), nil)
Expect(err).NotTo(HaveOccurred())

req.SetBasicAuth("wrongusername", "wrongpassword")

rsp, err := httpsClient.Do(req)
rsp, err := httpClientForEventGenerator.Do(req)
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusUnauthorized))
})
})

When("when username and password are correct for basic authentication during health check", func() {
When("username and password are correct for basic authentication during health check", func() {
It("should return 200", func() {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/health", serverURL), nil)
Expect(err).NotTo(HaveOccurred())

req.SetBasicAuth(conf.Health.BasicAuth.Username, conf.Health.BasicAuth.Password)

rsp, err := httpsClient.Do(req)
rsp, err := httpClientForEventGenerator.Do(req)
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusOK))
})
})
})

Describe("when Health server is ready to serve RESTful API with basic Auth", func() {
When("Health server is ready to serve RESTful API with basic Auth", func() {
BeforeEach(func() {
runner.Start()
})

When("when username and password are incorrect for basic authentication during health check", func() {
When("username and password are incorrect for basic authentication during health check", func() {
It("should return 401", func() {
req, err := http.NewRequest(http.MethodGet, serverURL.String(), nil)
Expect(err).NotTo(HaveOccurred())

req.SetBasicAuth("wrongusername", "wrongpassword")

rsp, err := httpsClient.Do(req)
rsp, err := httpClientForEventGenerator.Do(req)
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusUnauthorized))
})
Expand All @@ -245,7 +245,7 @@ var _ = Describe("Eventgenerator", func() {

req.SetBasicAuth(conf.Health.BasicAuth.Username, conf.Health.BasicAuth.Password)

rsp, err := httpsClient.Do(req)
rsp, err := httpClientForEventGenerator.Do(req)
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusOK))
})
Expand Down

0 comments on commit 45d9256

Please sign in to comment.