-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix support for HTTP preflight requests (#1792)
- Loading branch information
Showing
11 changed files
with
273 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package metrics | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/bluenviron/mediamtx/internal/conf" | ||
"github.com/bluenviron/mediamtx/internal/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPreflightRequest(t *testing.T) { | ||
api := Metrics{ | ||
Address: "localhost:9998", | ||
AllowOrigin: "*", | ||
ReadTimeout: conf.StringDuration(10 * time.Second), | ||
AuthManager: test.NilAuthManager, | ||
Parent: test.NilLogger, | ||
} | ||
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:9998", 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", res.Header.Get("Access-Control-Allow-Methods")) | ||
require.Equal(t, "Authorization", res.Header.Get("Access-Control-Allow-Headers")) | ||
require.Equal(t, byts, []byte{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package playback | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/bluenviron/mediamtx/internal/conf" | ||
"github.com/bluenviron/mediamtx/internal/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPreflightRequest(t *testing.T) { | ||
s := &Server{ | ||
Address: "127.0.0.1:9996", | ||
AllowOrigin: "*", | ||
ReadTimeout: conf.StringDuration(10 * time.Second), | ||
Parent: test.NilLogger, | ||
} | ||
err := s.Initialize() | ||
require.NoError(t, err) | ||
defer s.Close() | ||
|
||
tr := &http.Transport{} | ||
defer tr.CloseIdleConnections() | ||
hc := &http.Client{Transport: tr} | ||
|
||
req, err := http.NewRequest(http.MethodOptions, "http://localhost:9996", 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", res.Header.Get("Access-Control-Allow-Methods")) | ||
require.Equal(t, "Authorization", res.Header.Get("Access-Control-Allow-Headers")) | ||
require.Equal(t, byts, []byte{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package pprof | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/bluenviron/mediamtx/internal/conf" | ||
"github.com/bluenviron/mediamtx/internal/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPreflightRequest(t *testing.T) { | ||
s := &PPROF{ | ||
Address: "127.0.0.1:9999", | ||
AllowOrigin: "*", | ||
ReadTimeout: conf.StringDuration(10 * time.Second), | ||
Parent: test.NilLogger, | ||
} | ||
err := s.Initialize() | ||
require.NoError(t, err) | ||
defer s.Close() | ||
|
||
tr := &http.Transport{} | ||
defer tr.CloseIdleConnections() | ||
hc := &http.Client{Transport: tr} | ||
|
||
req, err := http.NewRequest(http.MethodOptions, "http://localhost:9999", 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", res.Header.Get("Access-Control-Allow-Methods")) | ||
require.Equal(t, "Authorization", res.Header.Get("Access-Control-Allow-Headers")) | ||
require.Equal(t, byts, []byte{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters