diff --git a/pkg/service/handlers/handlers_bmx.go b/pkg/service/handlers/handlers_bmx.go index 0cc818c..1770f35 100644 --- a/pkg/service/handlers/handlers_bmx.go +++ b/pkg/service/handlers/handlers_bmx.go @@ -4,7 +4,6 @@ package handlers import ( "encoding/json" "net/http" - "os" "strings" "github.com/gesellix/bose-soundtouch/pkg/service/bmx" @@ -13,10 +12,7 @@ import ( // HandleBMXRegistry returns the BMX service registry. func (s *Server) HandleBMXRegistry(w http.ResponseWriter, _ *http.Request) { - baseURL := os.Getenv("BASE_URL") - if baseURL == "" { - baseURL = "http://localhost:8000" - } + baseURL := s.serverURL content := string(bmxServicesJSON) content = strings.ReplaceAll(content, "{BMX_SERVER}", baseURL) diff --git a/pkg/service/handlers/handlers_bmx_test.go b/pkg/service/handlers/handlers_bmx_test.go index 078ee6a..98b7128 100644 --- a/pkg/service/handlers/handlers_bmx_test.go +++ b/pkg/service/handlers/handlers_bmx_test.go @@ -39,6 +39,10 @@ func TestBMXServices(t *testing.T) { // Verify placeholder replacement bodyStr := string(body) + if !strings.Contains(bodyStr, "http://localhost:8001") { + t.Errorf("Response does not contain expected baseURL http://localhost:8001, got: %s", bodyStr) + } + if strings.Contains(bodyStr, "{BMX_SERVER}") { t.Error("Response still contains {BMX_SERVER} placeholder") } @@ -48,6 +52,31 @@ func TestBMXServices(t *testing.T) { } } +func TestBMXServices_EmptyBaseURL(t *testing.T) { + r, _ := setupRouter("", nil) + + ts := httptest.NewServer(r) + defer ts.Close() + + res, err := http.Get(ts.URL + "/bmx/registry/v1/services") + if err != nil { + t.Fatal(err) + } + defer func() { _ = res.Body.Close() }() + + body, _ := io.ReadAll(res.Body) + bodyStr := string(body) + + // Since we removed the fallback, it should use the empty baseURL + if strings.Contains(bodyStr, "http://localhost:8000") { + t.Error("Response contains fallback URL http://localhost:8000, which should be removed") + } + + if strings.Contains(bodyStr, "{BMX_SERVER}") { + t.Error("Response still contains {BMX_SERVER} placeholder") + } +} + func TestOrionPlayback(t *testing.T) { r, _ := setupRouter("http://localhost:8001", nil) diff --git a/pkg/service/handlers/main_test.go b/pkg/service/handlers/main_test.go index c16dce0..5bf7c9a 100644 --- a/pkg/service/handlers/main_test.go +++ b/pkg/service/handlers/main_test.go @@ -6,7 +6,7 @@ import ( ) func setupRouter(targetURL string, ds *datastore.DataStore) (*chi.Mux, *Server) { - server := NewServer(ds, nil, "http://localhost:8000", false, false, false, false, false, false) + server := NewServer(ds, nil, targetURL, false, false, false, false, false, false) server.SetSoundcorkURL(targetURL) r := chi.NewRouter()