mirror of
https://github.com/bluenviron/mediamtx.git
synced 2026-01-26 13:29:15 -08:00
parent
342c257df5
commit
3f1d182d2c
11 changed files with 273 additions and 10 deletions
|
|
@ -251,6 +251,15 @@ func (a *API) writeError(ctx *gin.Context, status int, err error) {
|
|||
func (a *API) middlewareOrigin(ctx *gin.Context) {
|
||||
ctx.Writer.Header().Set("Access-Control-Allow-Origin", a.AllowOrigin)
|
||||
ctx.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
|
||||
// preflight requests
|
||||
if ctx.Request.Method == http.MethodOptions &&
|
||||
ctx.Request.Header.Get("Access-Control-Request-Method") != "" {
|
||||
ctx.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PATCH, DELETE")
|
||||
ctx.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
|
||||
ctx.AbortWithStatus(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) middlewareAuth(ctx *gin.Context) {
|
||||
|
|
|
|||
|
|
@ -74,6 +74,43 @@ func checkError(t *testing.T, msg string, body io.Reader) {
|
|||
require.Equal(t, map[string]interface{}{"error": msg}, resErr)
|
||||
}
|
||||
|
||||
func TestPreflightRequest(t *testing.T) {
|
||||
api := API{
|
||||
Address: "localhost:9997",
|
||||
AllowOrigin: "*",
|
||||
ReadTimeout: conf.StringDuration(10 * time.Second),
|
||||
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}
|
||||
|
||||
req, err := http.NewRequest(http.MethodOptions, "http://localhost:9997", nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
req.Header.Add("Access-Control-Request-Method", "GET")
|
||||
|
||||
res, err := hc.Do(req)
|
||||
require.NoError(t, err)
|
||||
defer res.Body.Close()
|
||||
|
||||
require.Equal(t, http.StatusNoContent, res.StatusCode)
|
||||
|
||||
byts, err := io.ReadAll(res.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "*", res.Header.Get("Access-Control-Allow-Origin"))
|
||||
require.Equal(t, "true", res.Header.Get("Access-Control-Allow-Credentials"))
|
||||
require.Equal(t, "OPTIONS, GET, POST, PATCH, DELETE", res.Header.Get("Access-Control-Allow-Methods"))
|
||||
require.Equal(t, "Authorization, Content-Type", res.Header.Get("Access-Control-Allow-Headers"))
|
||||
require.Equal(t, byts, []byte{})
|
||||
}
|
||||
|
||||
func TestConfigAuth(t *testing.T) {
|
||||
cnf := tempConf(t, "api: yes\n")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue