Skip to content

Commit

Permalink
api: make replace paths idempotent (#3362)
Browse files Browse the repository at this point in the history
Don't return a 404 error if the path doesn't exist yet
  • Loading branch information
bouk committed May 19, 2024
1 parent e283725 commit 6793a12
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
32 changes: 32 additions & 0 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
5 changes: 2 additions & 3 deletions internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6793a12

Please sign in to comment.