Merge remote-tracking branch 'origin/260201-readonly' into beta
# Conflicts: # README.md # www/index.html
This commit is contained in:
@@ -43,6 +43,10 @@ func apiDiscovery(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func apiHomekit(w http.ResponseWriter, r *http.Request) {
|
||||
if api.IsReadOnly() && r.Method != "GET" {
|
||||
api.ReadOnlyError(w)
|
||||
return
|
||||
}
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package homekit
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/internal/api"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestApiHomekitReadOnly(t *testing.T) {
|
||||
prevReadOnly := api.ReadOnly
|
||||
t.Cleanup(func() {
|
||||
api.ReadOnly = prevReadOnly
|
||||
})
|
||||
|
||||
api.ReadOnly = true
|
||||
|
||||
t.Run("POST blocked", func(t *testing.T) {
|
||||
req := httptest.NewRequest("POST", "/api/homekit", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
apiHomekit(w, req)
|
||||
|
||||
require.Equal(t, http.StatusForbidden, w.Code)
|
||||
})
|
||||
|
||||
t.Run("GET allowed", func(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/api/homekit", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
apiHomekit(w, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, w.Code)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user