From 1fe20c9a568148dc4262a2407f3cdbcb0b513cf5 Mon Sep 17 00:00:00 2001 From: Bouke van der Bijl Date: Thu, 16 May 2024 16:57:11 +0200 Subject: [PATCH] api: make replace paths idempotent Don't return a 404 error if the path doesn't exist yet --- internal/api/api_test.go | 32 ++++++++++++++++++++++++++++++++ internal/conf/conf.go | 5 ++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 6fb8801162c..f34c307a1af 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -515,6 +515,38 @@ func TestConfigPathsReplace(t *testing.T) { //nolint:dupl require.Equal(t, false, out["rpiCameraVFlip"]) } +func TestConfigPathsReplaceNonExisting(t *testing.T) { //nolint:dupl + cnf := tempConf(t, "api: yes\n") + + api := API{ + Address: "localhost:9997", + ReadTimeout: conf.StringDuration(10 * time.Second), + Conf: cnf, + AuthManager: test.NilAuthManager, + Parent: &testParent{}, + } + err := api.Initialize() + require.NoError(t, err) + defer api.Close() + + tr := &http.Transport{} + defer tr.CloseIdleConnections() + hc := &http.Client{Transport: tr} + + httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v3/config/paths/replace/my/path", + map[string]interface{}{ + "source": "rtsp://127.0.0.1:9998/mypath", + "sourceOnDemand": true, + }, nil) + + var out map[string]interface{} + httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v3/config/paths/get/my/path", nil, &out) + require.Equal(t, "rtsp://127.0.0.1:9998/mypath", out["source"]) + require.Equal(t, true, out["sourceOnDemand"]) + require.Equal(t, nil, out["disablePublisherOverride"]) + require.Equal(t, false, out["rpiCameraVFlip"]) +} + func TestConfigPathsDelete(t *testing.T) { cnf := tempConf(t, "api: yes\n") diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 22c8460ef9e..de97c2020b8 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -758,9 +758,8 @@ func (conf *Conf) PatchPath(name string, optional2 *OptionalPath) error { // ReplacePath replaces a path. func (conf *Conf) ReplacePath(name string, optional2 *OptionalPath) error { - _, ok := conf.OptionalPaths[name] - if !ok { - return ErrPathNotFound + if conf.OptionalPaths == nil { + conf.OptionalPaths = make(map[string]*OptionalPath) } conf.OptionalPaths[name] = optional2