github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/health_check_test.go (about) 1 /* 2 * Copyright (C) 2017 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package endpoints 19 20 import ( 21 "net/http" 22 "net/http/httptest" 23 "testing" 24 "time" 25 26 "github.com/gin-gonic/gin" 27 28 "github.com/mysteriumnetwork/node/metadata" 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestHealthCheckReturnsExpectedJSONObject(t *testing.T) { 33 34 req := httptest.NewRequest("GET", "/irrelevant", nil) 35 resp := httptest.NewRecorder() 36 37 tick1 := time.Unix(0, 0) 38 tick2 := tick1.Add(time.Minute) 39 40 metadata.BuildBranch = "some" 41 metadata.BuildCommit = "abc123" 42 metadata.BuildNumber = "travis build #" 43 44 g := gin.Default() 45 handlerFunc := HealthCheckEndpointFactory( 46 newMockTimer([]time.Time{tick1, tick2}).Now, 47 func() int { return 1 }, 48 ).HealthCheck 49 g.GET("/healthcheck", handlerFunc) 50 51 req, err := http.NewRequest("GET", "/healthcheck", nil) 52 assert.NoError(t, err) 53 54 g.ServeHTTP(resp, req) 55 56 assert.JSONEq( 57 t, 58 `{ 59 "uptime" : "1m0s", 60 "process" : 1, 61 "version": "`+metadata.VersionAsString()+`", 62 "build_info" : { 63 "branch": "some", 64 "commit": "abc123", 65 "build_number": "travis build #" 66 } 67 }`, 68 resp.Body.String()) 69 } 70 71 type mockTimer struct { 72 values []time.Time 73 current int 74 } 75 76 func newMockTimer(values []time.Time) *mockTimer { 77 return &mockTimer{ 78 values, 79 0, 80 } 81 } 82 83 func (mockTimer *mockTimer) Now() time.Time { 84 value := mockTimer.values[mockTimer.current%len(mockTimer.values)] 85 mockTimer.current++ 86 return value 87 }