Skip to content

Commit

Permalink
fix: removes extra slash for healthcheck. Fixes #2643 (#2645)
Browse files Browse the repository at this point in the history
  • Loading branch information
amir20 authored Jan 1, 2024
1 parent ed43889 commit 45cf78c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
18 changes: 12 additions & 6 deletions internal/healthcheck/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package healthcheck
import (
"fmt"
"net/http"
"os"
"strings"

log "github.com/sirupsen/logrus"
Expand All @@ -14,7 +13,16 @@ func HttpRequest(addr string, base string) error {
addr = "localhost" + addr
}

url := fmt.Sprintf("http://%s%s/healthcheck", addr, base)
if base == "/" {
base = ""
}

url := fmt.Sprintf("%s%s/healthcheck", addr, base)

if !strings.HasPrefix(url, "http") {
url = "http://" + url
}

log.Info("Checking health of " + url)
resp, err := http.Get(url)

Expand All @@ -24,10 +32,8 @@ func HttpRequest(addr string, base string) error {
defer resp.Body.Close()

if resp.StatusCode == 200 {
os.Exit(0)
return nil
}

os.Exit(1)

return nil
return fmt.Errorf("Healthcheck failed with status code %d", resp.StatusCode)
}
51 changes: 51 additions & 0 deletions internal/healthcheck/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package healthcheck

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestHttpRequest(t *testing.T) {
// Test server that always responds with a status code of 200
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
}))
defer server.Close()

// Test server that always responds with a status code of 500
errorServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusInternalServerError)
}))
defer errorServer.Close()

tests := []struct {
name string
addr string
base string
wantErr bool
}{
{
name: "Healthcheck OK",
addr: server.URL,
base: "/",
wantErr: false,
},
{
name: "Healthcheck Fail",
addr: errorServer.URL,
base: "/",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := HttpRequest(tt.addr, tt.base)

if (err != nil) != tt.wantErr {
t.Errorf("HttpRequest() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func main() {
if err := healthcheck.HttpRequest(args.Addr, args.Base); err != nil {
log.Fatal(err)
}
return
os.Exit(0)
}

if args.AuthProvider != "none" && args.AuthProvider != "forward-proxy" && args.AuthProvider != "simple" {
Expand Down

0 comments on commit 45cf78c

Please sign in to comment.