Add api endpoint to return supported schema
This commit is contained in:
Vendored
+19
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
|
||||||
|
{
|
||||||
|
"name": "Debug go2rtc",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"env": {
|
||||||
|
"CGO_ENABLED": "0"
|
||||||
|
},
|
||||||
|
"program": "main.go",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -176,3 +176,13 @@ func apiPreload(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "", http.StatusMethodNotAllowed)
|
http.Error(w, "", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func apiSchemes(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Error(w, "", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
schemes := GetSupportedSchemes()
|
||||||
|
api.ResponseJSON(w, schemes)
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,102 @@
|
|||||||
|
package streams
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestApiSchemes(t *testing.T) {
|
||||||
|
// Setup: Register some test handlers and redirects
|
||||||
|
HandleFunc("rtsp", func(url string) (core.Producer, error) { return nil, nil })
|
||||||
|
HandleFunc("rtmp", func(url string) (core.Producer, error) { return nil, nil })
|
||||||
|
RedirectFunc("http", func(url string) (string, error) { return "", nil })
|
||||||
|
|
||||||
|
t.Run("GET request returns schemes", func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest("GET", "/api/schemes", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
apiSchemes(w, req)
|
||||||
|
|
||||||
|
require.Equal(t, http.StatusOK, w.Code)
|
||||||
|
require.Equal(t, "application/json", w.Header().Get("Content-Type"))
|
||||||
|
|
||||||
|
var schemes []string
|
||||||
|
err := json.Unmarshal(w.Body.Bytes(), &schemes)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotEmpty(t, schemes)
|
||||||
|
|
||||||
|
// Check that our test schemes are in the response
|
||||||
|
require.Contains(t, schemes, "rtsp")
|
||||||
|
require.Contains(t, schemes, "rtmp")
|
||||||
|
require.Contains(t, schemes, "http")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("POST request returns method not allowed", func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest("POST", "/api/schemes", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
apiSchemes(w, req)
|
||||||
|
|
||||||
|
require.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("PUT request returns method not allowed", func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest("PUT", "/api/schemes", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
apiSchemes(w, req)
|
||||||
|
|
||||||
|
require.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("DELETE request returns method not allowed", func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest("DELETE", "/api/schemes", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
apiSchemes(w, req)
|
||||||
|
|
||||||
|
require.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("PATCH request returns method not allowed", func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest("PATCH", "/api/schemes", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
apiSchemes(w, req)
|
||||||
|
|
||||||
|
require.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApiSchemesNoDuplicates(t *testing.T) {
|
||||||
|
// Setup: Register a scheme in both handlers and redirects
|
||||||
|
HandleFunc("duplicate", func(url string) (core.Producer, error) { return nil, nil })
|
||||||
|
RedirectFunc("duplicate", func(url string) (string, error) { return "", nil })
|
||||||
|
|
||||||
|
req := httptest.NewRequest("GET", "/api/schemes", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
apiSchemes(w, req)
|
||||||
|
|
||||||
|
require.Equal(t, http.StatusOK, w.Code)
|
||||||
|
|
||||||
|
var schemes []string
|
||||||
|
err := json.Unmarshal(w.Body.Bytes(), &schemes)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Count occurrences of "duplicate"
|
||||||
|
count := 0
|
||||||
|
for _, scheme := range schemes {
|
||||||
|
if scheme == "duplicate" {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should only appear once
|
||||||
|
require.Equal(t, 1, count, "scheme 'duplicate' should appear exactly once")
|
||||||
|
}
|
||||||
@@ -2,7 +2,9 @@ package streams
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"maps"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
@@ -16,6 +18,17 @@ func HandleFunc(scheme string, handler Handler) {
|
|||||||
handlers[scheme] = handler
|
handlers[scheme] = handler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetSupportedSchemes() []string {
|
||||||
|
unique := make(map[string]bool)
|
||||||
|
for scheme := range handlers {
|
||||||
|
unique[scheme] = true
|
||||||
|
}
|
||||||
|
for scheme := range redirects {
|
||||||
|
unique[scheme] = true
|
||||||
|
}
|
||||||
|
return slices.Collect(maps.Keys(unique))
|
||||||
|
}
|
||||||
|
|
||||||
func HasProducer(url string) bool {
|
func HasProducer(url string) bool {
|
||||||
if i := strings.IndexByte(url, ':'); i > 0 {
|
if i := strings.IndexByte(url, ':'); i > 0 {
|
||||||
scheme := url[:i]
|
scheme := url[:i]
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ func Init() {
|
|||||||
api.HandleFunc("api/streams", apiStreams)
|
api.HandleFunc("api/streams", apiStreams)
|
||||||
api.HandleFunc("api/streams.dot", apiStreamsDOT)
|
api.HandleFunc("api/streams.dot", apiStreamsDOT)
|
||||||
api.HandleFunc("api/preload", apiPreload)
|
api.HandleFunc("api/preload", apiPreload)
|
||||||
|
api.HandleFunc("api/schemes", apiSchemes)
|
||||||
|
|
||||||
if cfg.Publish == nil && cfg.Preload == nil {
|
if cfg.Publish == nil && cfg.Preload == nil {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user