github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/api/health_handler_test.go (about) 1 // Copyright (c) 2017-2018 Uber Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package api 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "net/http" 21 "net/http/httptest" 22 23 "github.com/gorilla/mux" 24 "github.com/onsi/ginkgo" 25 . "github.com/onsi/gomega" 26 ) 27 28 var _ = ginkgo.Describe("HealthCheck", func() { 29 healthCheckHandler := NewHealthCheckHandler() 30 var testServer *httptest.Server 31 ginkgo.BeforeEach(func() { 32 testRouter := mux.NewRouter() 33 testRouter.HandleFunc("/health", healthCheckHandler.HealthCheck) 34 testServer = httptest.NewUnstartedServer(WithPanicHandling(testRouter)) 35 testServer.Start() 36 }) 37 38 ginkgo.AfterEach(func() { 39 testServer.Close() 40 }) 41 42 ginkgo.It("HealthCheck should work", func() { 43 hostPort := testServer.Listener.Addr().String() 44 resp, err := http.Post(fmt.Sprintf("http://%s/health", hostPort), "", nil) 45 Ω(err).Should(BeNil()) 46 b, err := ioutil.ReadAll(resp.Body) 47 Ω(string(b)).Should(Equal("OK")) 48 Ω(err).Should(BeNil()) 49 Ω(resp.StatusCode).Should(Equal(http.StatusOK)) 50 51 healthCheckHandler.disable = true 52 53 resp, err = http.Post(fmt.Sprintf("http://%s/health", hostPort), "", nil) 54 Ω(err).Should(BeNil()) 55 b, err = ioutil.ReadAll(resp.Body) 56 Ω(string(b)).Should(Equal("Health check disabled")) 57 Ω(err).Should(BeNil()) 58 Ω(resp.StatusCode).Should(Equal(http.StatusServiceUnavailable)) 59 }) 60 })