You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.5 KiB
Go

package serviceboot
// fiber_test.go contains backend tests for package behavior, error paths, and regressions.
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
// TestRegisterHealth ensures register health behavior is handled correctly.
func TestRegisterHealth(t *testing.T) {
app := NewFiberApp(Config{AppName: "test-service"})
RegisterHealth(app, "svc")
req := httptest.NewRequest(http.MethodGet, "/health", nil)
resp, err := app.Test(req)
require.NoError(t, err)
defer resp.Body.Close()
var body map[string]string
require.NoError(t, json.NewDecoder(resp.Body).Decode(&body))
require.Equal(t, "healthy", body["status"])
require.Equal(t, "svc", body["service"])
}
// TestListenAddressFromEnv ensures listen address from env behavior is handled correctly.
func TestListenAddressFromEnv(t *testing.T) {
t.Setenv("SERVICE_PORT", "9090")
require.Equal(t, ":9090", ListenAddress("SERVICE_PORT", 8080))
}
// TestListenAddressFallback ensures listen address fallback behavior is handled correctly.
func TestListenAddressFallback(t *testing.T) {
t.Setenv("SERVICE_PORT", "bad")
require.Equal(t, ":8080", ListenAddress("SERVICE_PORT", 8080))
}
// TestListenAddressOutOfRangeFallback ensures listen address out of range fallback behavior is handled correctly.
func TestListenAddressOutOfRangeFallback(t *testing.T) {
t.Setenv("SERVICE_PORT", "70000")
require.Equal(t, ":8080", ListenAddress("SERVICE_PORT", 8080))
}