Commits (Ingo Oppermann): - Add experimental SRT connection stats and logs - Hide /config/reload endpoint in reade-only mode - Add SRT server - Create v16 in go.mod - Fix data races, tests, lint, and update dependencies - Add trailing slash for routed directories (datarhei/restreamer#340) - Allow relative URLs in content in static routes Co-Authored-By: Ingo Oppermann <57445+ioppermann@users.noreply.github.com>
145 lines
3.8 KiB
Go
145 lines
3.8 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/datarhei/core/v16/http/api"
|
|
"github.com/datarhei/core/v16/http/mock"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type Response struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data interface{}
|
|
}
|
|
|
|
func getDummyRestreamHandler() *RestreamHandler {
|
|
rs := mock.DummyRestreamer()
|
|
|
|
handler := NewRestream(rs)
|
|
|
|
return handler
|
|
}
|
|
|
|
func getDummyRestreamRouter() *echo.Echo {
|
|
router := mock.DummyEcho()
|
|
|
|
restream := getDummyRestreamHandler()
|
|
|
|
router.GET("/", restream.GetAll)
|
|
router.POST("/", restream.Add)
|
|
router.GET("/:id", restream.Get)
|
|
router.GET("/:id/report", restream.GetReport)
|
|
router.PUT("/:id", restream.Update)
|
|
router.DELETE("/:id", restream.Delete)
|
|
router.PUT("/:id/command", restream.Command)
|
|
|
|
return router
|
|
}
|
|
|
|
func TestAddProcessMissingField(t *testing.T) {
|
|
router := getDummyRestreamRouter()
|
|
|
|
data := mock.Read(t, "./fixtures/addProcessMissingField.json")
|
|
|
|
mock.Request(t, http.StatusOK, router, "POST", "/", data)
|
|
}
|
|
|
|
func TestAddProcessInvalidType(t *testing.T) {
|
|
router := getDummyRestreamRouter()
|
|
|
|
data := mock.Read(t, "./fixtures/addProcessInvalidType.json")
|
|
|
|
mock.Request(t, http.StatusBadRequest, router, "POST", "/", data)
|
|
}
|
|
|
|
func TestAddProcess(t *testing.T) {
|
|
router := getDummyRestreamRouter()
|
|
|
|
data := mock.Read(t, "./fixtures/addProcess.json")
|
|
|
|
response := mock.Request(t, http.StatusOK, router, "POST", "/", data)
|
|
|
|
mock.Validate(t, &api.ProcessConfig{}, response.Data)
|
|
}
|
|
|
|
func TestRemoveUnknownProcess(t *testing.T) {
|
|
router := getDummyRestreamRouter()
|
|
|
|
mock.Request(t, http.StatusNotFound, router, "DELETE", "/foobar", nil)
|
|
}
|
|
|
|
func TestRemoveProcess(t *testing.T) {
|
|
router := getDummyRestreamRouter()
|
|
|
|
data := mock.Read(t, "./fixtures/removeProcess.json")
|
|
|
|
mock.Request(t, http.StatusOK, router, "POST", "/", data)
|
|
mock.Request(t, http.StatusOK, router, "DELETE", "/test", nil)
|
|
}
|
|
|
|
func TestProcessInfo(t *testing.T) {
|
|
router := getDummyRestreamRouter()
|
|
|
|
data := mock.Read(t, "./fixtures/addProcess.json")
|
|
|
|
mock.Request(t, http.StatusOK, router, "POST", "/", data)
|
|
response := mock.Request(t, http.StatusOK, router, "GET", "/test", nil)
|
|
|
|
mock.Validate(t, &api.Process{}, response.Data)
|
|
}
|
|
|
|
func TestProcessReportNotFound(t *testing.T) {
|
|
router := getDummyRestreamRouter()
|
|
|
|
mock.Request(t, http.StatusNotFound, router, "GET", "/test/report", nil)
|
|
}
|
|
|
|
func TestProcessReport(t *testing.T) {
|
|
router := getDummyRestreamRouter()
|
|
|
|
data := mock.Read(t, "./fixtures/addProcess.json")
|
|
|
|
mock.Request(t, http.StatusOK, router, "POST", "/", data)
|
|
response := mock.Request(t, http.StatusOK, router, "GET", "/test/report", nil)
|
|
|
|
mock.Validate(t, &api.ProcessReport{}, response.Data)
|
|
}
|
|
|
|
func TestProcessCommandNotFound(t *testing.T) {
|
|
router := getDummyRestreamRouter()
|
|
|
|
command := mock.Read(t, "./fixtures/commandStart.json")
|
|
mock.Request(t, http.StatusBadRequest, router, "PUT", "/test/command", command)
|
|
}
|
|
|
|
func TestProcessCommandInvalid(t *testing.T) {
|
|
router := getDummyRestreamRouter()
|
|
|
|
data := mock.Read(t, "./fixtures/addProcess.json")
|
|
|
|
mock.Request(t, http.StatusOK, router, "POST", "/", data)
|
|
|
|
command := mock.Read(t, "./fixtures/commandInvalid.json")
|
|
mock.Request(t, http.StatusBadRequest, router, "PUT", "/test/command", command)
|
|
}
|
|
|
|
func TestProcessCommand(t *testing.T) {
|
|
router := getDummyRestreamRouter()
|
|
|
|
data := mock.Read(t, "./fixtures/addProcess.json")
|
|
|
|
mock.Request(t, http.StatusOK, router, "POST", "/", data)
|
|
|
|
command := mock.Read(t, "./fixtures/commandStart.json")
|
|
mock.Request(t, http.StatusOK, router, "PUT", "/test/command", command)
|
|
mock.Request(t, http.StatusOK, router, "GET", "/test", data)
|
|
|
|
command = mock.Read(t, "./fixtures/commandStop.json")
|
|
mock.Request(t, http.StatusOK, router, "PUT", "/test/command", command)
|
|
mock.Request(t, http.StatusOK, router, "GET", "/test", data)
|
|
}
|