Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
saurav-malani committed May 22, 2023
1 parent 9fa51ee commit 5d00fc4
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,17 @@ var _ = Describe("Gateway", func() {
var (
gateway *HandleT
statsStore *memstats.Store
whServer *httptest.Server
server *httptest.Server
url *url.URL
)

BeforeEach(func() {
whServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, "OK")
}))
url, err := url.Parse(whServer.URL)
var err error
url, err = url.Parse(server.URL)
Expect(err).To(BeNil())
config.Set("Warehouse.webPort", url.Port())

Expand All @@ -364,7 +366,7 @@ var _ = Describe("Gateway", func() {
AfterEach(func() {
err := gateway.Shutdown()
Expect(err).To(BeNil())
whServer.Close()
server.Close()
})

createValidBody := func(customProperty, customValue string) []byte {
Expand All @@ -378,9 +380,8 @@ var _ = Describe("Gateway", func() {
}
verifyEndpoint := func(endpoints []string, method string) {
client := &http.Client{}
baseURL := "http://localhost:8080"
for _, ep := range endpoints {
url := baseURL + ep
url := url.String() + ep
var req *http.Request
var err error
if ep == "/beacon/v1/batch" {
Expand Down Expand Up @@ -409,21 +410,24 @@ var _ = Describe("Gateway", func() {
})
c.mockBackendConfig.EXPECT().WaitForConfig(gomock.Any()).AnyTimes()
var err error
wait := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
go func() {
err = gateway.StartWebHandler(ctx)
Expect(err).To(BeNil())
close(wait)
}()
require.Eventually(GinkgoT(), func() bool {
Eventually(func() bool {
verifyEndpoint([]string{"/version"}, http.MethodGet)
return true
}, time.Second*10, time.Second)
}, time.Second*10, time.Second).Should(BeTrue())

getEndpoint, postEndpoints, deleteEndpoints := getEndpointMethodMap()
verifyEndpoint(getEndpoint, http.MethodGet)
verifyEndpoint(postEndpoints, http.MethodPost)
verifyEndpoint(deleteEndpoints, http.MethodDelete)
cancel()
<-wait
})
})

Expand Down Expand Up @@ -1143,6 +1147,8 @@ func authorizedRequest(username string, body io.Reader) *http.Request {
func expectHandlerResponse(handler http.HandlerFunc, req *http.Request, responseStatus int, responseBody string) {
testutils.RunTestWithTimeout(func() {
rr := httptest.NewRecorder()
defer rr.Result().Body.Close()

handler.ServeHTTP(rr, req)

bodyBytes, _ := io.ReadAll(rr.Body)
Expand Down

0 comments on commit 5d00fc4

Please sign in to comment.