mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-10-21 22:59:37 +08:00
fix race condition in tests (#1826)
This commit is contained in:
@@ -39,43 +39,35 @@ var testMediaH264 = &media.Media{
|
|||||||
Formats: []formats.Format{testFormatH264},
|
Formats: []formats.Format{testFormatH264},
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpRequest(method string, ur string, in interface{}, out interface{}) error {
|
func httpRequest(t *testing.T, hc *http.Client, method string, ur string, in interface{}, out interface{}) {
|
||||||
buf, err := func() (io.Reader, error) {
|
buf := func() io.Reader {
|
||||||
if in == nil {
|
if in == nil {
|
||||||
return nil, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
byts, err := json.Marshal(in)
|
byts, err := json.Marshal(in)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return bytes.NewBuffer(byts), nil
|
return bytes.NewBuffer(byts)
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest(method, ur, buf)
|
req, err := http.NewRequest(method, ur, buf)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := hc.Do(req)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
if res.StatusCode != http.StatusOK {
|
if res.StatusCode != http.StatusOK {
|
||||||
return fmt.Errorf("bad status code: %d", res.StatusCode)
|
t.Errorf("bad status code: %d", res.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
if out == nil {
|
if out == nil {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.NewDecoder(res.Body).Decode(out)
|
err = json.NewDecoder(res.Body).Decode(out)
|
||||||
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPagination(t *testing.T) {
|
func TestPagination(t *testing.T) {
|
||||||
@@ -111,17 +103,14 @@ func TestPagination(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAPIConfigGet(t *testing.T) {
|
func TestAPIConfigGet(t *testing.T) {
|
||||||
// since the HTTP server is created and deleted multiple times,
|
|
||||||
// we can't reuse TCP connections.
|
|
||||||
http.DefaultTransport.(*http.Transport).DisableKeepAlives = true
|
|
||||||
|
|
||||||
p, ok := newInstance("api: yes\n")
|
p, ok := newInstance("api: yes\n")
|
||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
var out map[string]interface{}
|
var out map[string]interface{}
|
||||||
err := httpRequest(http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, true, out["api"])
|
require.Equal(t, true, out["api"])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,18 +119,18 @@ func TestAPIConfigSet(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
err := httpRequest(http.MethodPost, "http://localhost:9997/v2/config/set", map[string]interface{}{
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
|
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/set", map[string]interface{}{
|
||||||
"rtmpDisable": true,
|
"rtmpDisable": true,
|
||||||
"readTimeout": "7s",
|
"readTimeout": "7s",
|
||||||
"protocols": []string{"tcp"},
|
"protocols": []string{"tcp"},
|
||||||
}, nil)
|
}, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
|
||||||
var out map[string]interface{}
|
var out map[string]interface{}
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, true, out["rtmpDisable"])
|
require.Equal(t, true, out["rtmpDisable"])
|
||||||
require.Equal(t, "7s", out["readTimeout"])
|
require.Equal(t, "7s", out["readTimeout"])
|
||||||
require.Equal(t, []interface{}{"tcp"}, out["protocols"])
|
require.Equal(t, []interface{}{"tcp"}, out["protocols"])
|
||||||
@@ -152,15 +141,15 @@ func TestAPIConfigPathsAdd(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
err := httpRequest(http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
|
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
|
||||||
"source": "rtsp://127.0.0.1:9999/mypath",
|
"source": "rtsp://127.0.0.1:9999/mypath",
|
||||||
"sourceOnDemand": true,
|
"sourceOnDemand": true,
|
||||||
}, nil)
|
}, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
var out map[string]interface{}
|
var out map[string]interface{}
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, "rtsp://127.0.0.1:9999/mypath",
|
require.Equal(t, "rtsp://127.0.0.1:9999/mypath",
|
||||||
out["paths"].(map[string]interface{})["my/path"].(map[string]interface{})["source"])
|
out["paths"].(map[string]interface{})["my/path"].(map[string]interface{})["source"])
|
||||||
}
|
}
|
||||||
@@ -170,25 +159,24 @@ func TestAPIConfigPathsEdit(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
err := httpRequest(http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
|
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
|
||||||
"source": "rtsp://127.0.0.1:9999/mypath",
|
"source": "rtsp://127.0.0.1:9999/mypath",
|
||||||
"sourceOnDemand": true,
|
"sourceOnDemand": true,
|
||||||
}, nil)
|
}, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
err = httpRequest(http.MethodPost, "http://localhost:9997/v2/config/paths/edit/my/path", map[string]interface{}{
|
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/paths/edit/my/path", map[string]interface{}{
|
||||||
"source": "rtsp://127.0.0.1:9998/mypath",
|
"source": "rtsp://127.0.0.1:9998/mypath",
|
||||||
"sourceOnDemand": true,
|
"sourceOnDemand": true,
|
||||||
}, nil)
|
}, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
var out struct {
|
var out struct {
|
||||||
Paths map[string]struct {
|
Paths map[string]struct {
|
||||||
Source string `json:"source"`
|
Source string `json:"source"`
|
||||||
} `json:"paths"`
|
} `json:"paths"`
|
||||||
}
|
}
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, "rtsp://127.0.0.1:9998/mypath", out.Paths["my/path"].Source)
|
require.Equal(t, "rtsp://127.0.0.1:9998/mypath", out.Paths["my/path"].Source)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,20 +185,19 @@ func TestAPIConfigPathsRemove(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
err := httpRequest(http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
|
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/paths/add/my/path", map[string]interface{}{
|
||||||
"source": "rtsp://127.0.0.1:9999/mypath",
|
"source": "rtsp://127.0.0.1:9999/mypath",
|
||||||
"sourceOnDemand": true,
|
"sourceOnDemand": true,
|
||||||
}, nil)
|
}, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
err = httpRequest(http.MethodPost, "http://localhost:9997/v2/config/paths/remove/my/path", nil, nil)
|
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/config/paths/remove/my/path", nil, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
var out struct {
|
var out struct {
|
||||||
Paths map[string]interface{} `json:"paths"`
|
Paths map[string]interface{} `json:"paths"`
|
||||||
}
|
}
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/config/get", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
_, ok = out.Paths["my/path"]
|
_, ok = out.Paths["my/path"]
|
||||||
require.Equal(t, false, ok)
|
require.Equal(t, false, ok)
|
||||||
}
|
}
|
||||||
@@ -240,6 +227,8 @@ func TestAPIPathsList(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
media0 := testMediaH264
|
media0 := testMediaH264
|
||||||
|
|
||||||
source := gortsplib.Client{}
|
source := gortsplib.Client{}
|
||||||
@@ -274,8 +263,7 @@ func TestAPIPathsList(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
var out pathList
|
var out pathList
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, pathList{
|
require.Equal(t, pathList{
|
||||||
PageCount: 1,
|
PageCount: 1,
|
||||||
Items: []path{{
|
Items: []path{{
|
||||||
@@ -308,6 +296,8 @@ func TestAPIPathsList(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
medias := media.Medias{
|
medias := media.Medias{
|
||||||
{
|
{
|
||||||
Type: media.TypeVideo,
|
Type: media.TypeVideo,
|
||||||
@@ -335,8 +325,7 @@ func TestAPIPathsList(t *testing.T) {
|
|||||||
defer source.Close()
|
defer source.Close()
|
||||||
|
|
||||||
var out pathList
|
var out pathList
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, pathList{
|
require.Equal(t, pathList{
|
||||||
PageCount: 1,
|
PageCount: 1,
|
||||||
Items: []path{{
|
Items: []path{{
|
||||||
@@ -359,9 +348,10 @@ func TestAPIPathsList(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
var out pathList
|
var out pathList
|
||||||
err := httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, pathList{
|
require.Equal(t, pathList{
|
||||||
PageCount: 1,
|
PageCount: 1,
|
||||||
Items: []path{{
|
Items: []path{{
|
||||||
@@ -384,9 +374,10 @@ func TestAPIPathsList(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
var out pathList
|
var out pathList
|
||||||
err := httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, pathList{
|
require.Equal(t, pathList{
|
||||||
PageCount: 1,
|
PageCount: 1,
|
||||||
Items: []path{{
|
Items: []path{{
|
||||||
@@ -409,9 +400,10 @@ func TestAPIPathsList(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
var out pathList
|
var out pathList
|
||||||
err := httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/list", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, pathList{
|
require.Equal(t, pathList{
|
||||||
PageCount: 1,
|
PageCount: 1,
|
||||||
Items: []path{{
|
Items: []path{{
|
||||||
@@ -433,6 +425,8 @@ func TestAPIPathsGet(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
source := gortsplib.Client{}
|
source := gortsplib.Client{}
|
||||||
err := source.StartRecording("rtsp://localhost:8554/mypath", media.Medias{testMediaH264})
|
err := source.StartRecording("rtsp://localhost:8554/mypath", media.Medias{testMediaH264})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -459,11 +453,9 @@ func TestAPIPathsGet(t *testing.T) {
|
|||||||
pathName = "nonexisting"
|
pathName = "nonexisting"
|
||||||
}
|
}
|
||||||
|
|
||||||
var out path
|
|
||||||
err := httpRequest(http.MethodGet, "http://localhost:9997/v2/paths/get/"+pathName, nil, &out)
|
|
||||||
|
|
||||||
if ca == "ok" {
|
if ca == "ok" {
|
||||||
require.NoError(t, err)
|
var out path
|
||||||
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/paths/get/"+pathName, nil, &out)
|
||||||
require.Equal(t, path{
|
require.Equal(t, path{
|
||||||
Name: "mypath",
|
Name: "mypath",
|
||||||
Source: pathSource{
|
Source: pathSource{
|
||||||
@@ -473,7 +465,10 @@ func TestAPIPathsGet(t *testing.T) {
|
|||||||
Tracks: []string{"H264"},
|
Tracks: []string{"H264"},
|
||||||
}, out)
|
}, out)
|
||||||
} else {
|
} else {
|
||||||
require.EqualError(t, err, "bad status code: 404")
|
res, err := hc.Get("http://localhost:9997/v2/paths/get/" + pathName)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer res.Body.Close()
|
||||||
|
require.Equal(t, 404, res.StatusCode)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -521,6 +516,8 @@ func TestAPIProtocolList(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
medi := testMediaH264
|
medi := testMediaH264
|
||||||
|
|
||||||
switch ca { //nolint:dupl
|
switch ca { //nolint:dupl
|
||||||
@@ -619,7 +616,7 @@ func TestAPIProtocolList(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
func() {
|
func() {
|
||||||
res, err := http.Get("http://localhost:8888/mypath/index.m3u8")
|
res, err := hc.Get("http://localhost:8888/mypath/index.m3u8")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
require.Equal(t, 200, res.StatusCode)
|
require.Equal(t, 200, res.StatusCode)
|
||||||
@@ -632,7 +629,7 @@ func TestAPIProtocolList(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer source.Close()
|
defer source.Close()
|
||||||
|
|
||||||
c := newWebRTCTestClient(t, "http://localhost:8889/mypath/whep", false)
|
c := newWebRTCTestClient(t, hc, "http://localhost:8889/mypath/whep", false)
|
||||||
defer c.close()
|
defer c.close()
|
||||||
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
@@ -680,8 +677,7 @@ func TestAPIProtocolList(t *testing.T) {
|
|||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
} `json:"items"`
|
} `json:"items"`
|
||||||
}
|
}
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
if ca != "rtsp conns" && ca != "rtsps conns" {
|
if ca != "rtsp conns" && ca != "rtsps conns" {
|
||||||
require.Equal(t, "publish", out.Items[0].State)
|
require.Equal(t, "publish", out.Items[0].State)
|
||||||
@@ -694,8 +690,7 @@ func TestAPIProtocolList(t *testing.T) {
|
|||||||
LastRequest string `json:"lastRequest"`
|
LastRequest string `json:"lastRequest"`
|
||||||
} `json:"items"`
|
} `json:"items"`
|
||||||
}
|
}
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/hlsmuxers/list", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/hlsmuxers/list", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
s := fmt.Sprintf("^%d-", time.Now().Year())
|
s := fmt.Sprintf("^%d-", time.Now().Year())
|
||||||
require.Regexp(t, s, out.Items[0].Created)
|
require.Regexp(t, s, out.Items[0].Created)
|
||||||
@@ -716,8 +711,7 @@ func TestAPIProtocolList(t *testing.T) {
|
|||||||
PageCount int `json:"pageCount"`
|
PageCount int `json:"pageCount"`
|
||||||
Items []item `json:"items"`
|
Items []item `json:"items"`
|
||||||
}
|
}
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/webrtcsessions/list", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/webrtcsessions/list", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
require.Equal(t, true, out.Items[0].PeerConnectionEstablished)
|
require.Equal(t, true, out.Items[0].PeerConnectionEstablished)
|
||||||
}
|
}
|
||||||
@@ -767,6 +761,8 @@ func TestAPIProtocolGet(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
medi := testMediaH264
|
medi := testMediaH264
|
||||||
|
|
||||||
switch ca { //nolint:dupl
|
switch ca { //nolint:dupl
|
||||||
@@ -865,7 +861,7 @@ func TestAPIProtocolGet(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
func() {
|
func() {
|
||||||
res, err := http.Get("http://localhost:8888/mypath/index.m3u8")
|
res, err := hc.Get("http://localhost:8888/mypath/index.m3u8")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
require.Equal(t, 200, res.StatusCode)
|
require.Equal(t, 200, res.StatusCode)
|
||||||
@@ -878,7 +874,7 @@ func TestAPIProtocolGet(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer source.Close()
|
defer source.Close()
|
||||||
|
|
||||||
c := newWebRTCTestClient(t, "http://localhost:8889/mypath/whep", false)
|
c := newWebRTCTestClient(t, hc, "http://localhost:8889/mypath/whep", false)
|
||||||
defer c.close()
|
defer c.close()
|
||||||
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
@@ -929,16 +925,14 @@ func TestAPIProtocolGet(t *testing.T) {
|
|||||||
var out1 struct {
|
var out1 struct {
|
||||||
Items []item `json:"items"`
|
Items []item `json:"items"`
|
||||||
}
|
}
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out1)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out1)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
if ca != "rtsp conns" && ca != "rtsps conns" {
|
if ca != "rtsp conns" && ca != "rtsps conns" {
|
||||||
require.Equal(t, "publish", out1.Items[0].State)
|
require.Equal(t, "publish", out1.Items[0].State)
|
||||||
}
|
}
|
||||||
|
|
||||||
var out2 item
|
var out2 item
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/"+pa+"/get/"+out1.Items[0].ID, nil, &out2)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/"+pa+"/get/"+out1.Items[0].ID, nil, &out2)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
if ca != "rtsp conns" && ca != "rtsps conns" {
|
if ca != "rtsp conns" && ca != "rtsps conns" {
|
||||||
require.Equal(t, "publish", out2.State)
|
require.Equal(t, "publish", out2.State)
|
||||||
@@ -951,8 +945,7 @@ func TestAPIProtocolGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var out item
|
var out item
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/hlsmuxers/get/mypath", nil, &out)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/hlsmuxers/get/mypath", nil, &out)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
s := fmt.Sprintf("^%d-", time.Now().Year())
|
s := fmt.Sprintf("^%d-", time.Now().Year())
|
||||||
require.Regexp(t, s, out.Created)
|
require.Regexp(t, s, out.Created)
|
||||||
@@ -973,12 +966,10 @@ func TestAPIProtocolGet(t *testing.T) {
|
|||||||
var out1 struct {
|
var out1 struct {
|
||||||
Items []item `json:"items"`
|
Items []item `json:"items"`
|
||||||
}
|
}
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/webrtcsessions/list", nil, &out1)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/webrtcsessions/list", nil, &out1)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
var out2 item
|
var out2 item
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/webrtcsessions/get/"+out1.Items[0].ID, nil, &out2)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/webrtcsessions/get/"+out1.Items[0].ID, nil, &out2)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
require.Equal(t, true, out2.PeerConnectionEstablished)
|
require.Equal(t, true, out2.PeerConnectionEstablished)
|
||||||
}
|
}
|
||||||
@@ -1018,6 +1009,8 @@ func TestAPIProtocolKick(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
medi := testMediaH264
|
medi := testMediaH264
|
||||||
|
|
||||||
switch ca {
|
switch ca {
|
||||||
@@ -1055,7 +1048,7 @@ func TestAPIProtocolKick(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
case "webrtc":
|
case "webrtc":
|
||||||
c := newWebRTCTestClient(t, "http://localhost:8889/mypath/whip", true)
|
c := newWebRTCTestClient(t, hc, "http://localhost:8889/mypath/whip", true)
|
||||||
defer c.close()
|
defer c.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1079,19 +1072,16 @@ func TestAPIProtocolKick(t *testing.T) {
|
|||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
} `json:"items"`
|
} `json:"items"`
|
||||||
}
|
}
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out1)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out1)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
err = httpRequest(http.MethodPost, "http://localhost:9997/v2/"+pa+"/kick/"+out1.Items[0].ID, nil, nil)
|
httpRequest(t, hc, http.MethodPost, "http://localhost:9997/v2/"+pa+"/kick/"+out1.Items[0].ID, nil, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
var out2 struct {
|
var out2 struct {
|
||||||
Items []struct {
|
Items []struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
} `json:"items"`
|
} `json:"items"`
|
||||||
}
|
}
|
||||||
err = httpRequest(http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out2)
|
httpRequest(t, hc, http.MethodGet, "http://localhost:9997/v2/"+pa+"/list", nil, &out2)
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, 0, len(out2.Items))
|
require.Equal(t, 0, len(out2.Items))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,6 @@ package core
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -86,18 +85,19 @@ func (ts *testHTTPAuthenticator) onAuth(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpPullFile(u string) ([]byte, error) {
|
func httpPullFile(t *testing.T, hc *http.Client, u string) []byte {
|
||||||
res, err := http.Get(u)
|
res, err := hc.Get(u)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
if res.StatusCode != http.StatusOK {
|
if res.StatusCode != http.StatusOK {
|
||||||
return nil, fmt.Errorf("bad status code: %v", res.StatusCode)
|
t.Errorf("bad status code: %v", res.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
return io.ReadAll(res.Body)
|
byts, err := io.ReadAll(res.Body)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
return byts
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHLSReadNotFound(t *testing.T) {
|
func TestHLSReadNotFound(t *testing.T) {
|
||||||
@@ -108,7 +108,9 @@ func TestHLSReadNotFound(t *testing.T) {
|
|||||||
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8888/stream/", nil)
|
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8888/stream/", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
|
res, err := hc.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
require.Equal(t, http.StatusNotFound, res.StatusCode)
|
require.Equal(t, http.StatusNotFound, res.StatusCode)
|
||||||
@@ -161,8 +163,9 @@ func TestHLSRead(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
cnt, err := httpPullFile("http://localhost:8888/stream/index.m3u8")
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
require.NoError(t, err)
|
|
||||||
|
cnt := httpPullFile(t, hc, "http://localhost:8888/stream/index.m3u8")
|
||||||
require.Equal(t, "#EXTM3U\n"+
|
require.Equal(t, "#EXTM3U\n"+
|
||||||
"#EXT-X-VERSION:9\n"+
|
"#EXT-X-VERSION:9\n"+
|
||||||
"#EXT-X-INDEPENDENT-SEGMENTS\n"+
|
"#EXT-X-INDEPENDENT-SEGMENTS\n"+
|
||||||
@@ -171,8 +174,7 @@ func TestHLSRead(t *testing.T) {
|
|||||||
"CODECS=\"avc1.42c028\",RESOLUTION=1920x1080,FRAME-RATE=30.000\n"+
|
"CODECS=\"avc1.42c028\",RESOLUTION=1920x1080,FRAME-RATE=30.000\n"+
|
||||||
"stream.m3u8\n", string(cnt))
|
"stream.m3u8\n", string(cnt))
|
||||||
|
|
||||||
cnt, err = httpPullFile("http://localhost:8888/stream/stream.m3u8")
|
cnt = httpPullFile(t, hc, "http://localhost:8888/stream/stream.m3u8")
|
||||||
require.NoError(t, err)
|
|
||||||
require.Regexp(t, "#EXTM3U\n"+
|
require.Regexp(t, "#EXTM3U\n"+
|
||||||
"#EXT-X-VERSION:9\n"+
|
"#EXT-X-VERSION:9\n"+
|
||||||
"#EXT-X-TARGETDURATION:1\n"+
|
"#EXT-X-TARGETDURATION:1\n"+
|
||||||
|
@@ -3,6 +3,7 @@ package core
|
|||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -36,8 +37,9 @@ func TestMetrics(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
bo, err := httpPullFile("http://localhost:9998/metrics")
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
require.NoError(t, err)
|
|
||||||
|
bo := httpPullFile(t, hc, "http://localhost:9998/metrics")
|
||||||
|
|
||||||
require.Equal(t, `paths 0
|
require.Equal(t, `paths 0
|
||||||
hls_muxers 0
|
hls_muxers 0
|
||||||
@@ -101,8 +103,7 @@ webrtc_sessions_bytes_sent 0
|
|||||||
err = conn.WriteTracks(videoTrack, nil)
|
err = conn.WriteTracks(videoTrack, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
bo, err = httpPullFile("http://localhost:9998/metrics")
|
bo = httpPullFile(t, hc, "http://localhost:9998/metrics")
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
require.Regexp(t,
|
require.Regexp(t,
|
||||||
`^paths\{name=".*?",state="ready"\} 1`+"\n"+
|
`^paths\{name=".*?",state="ready"\} 1`+"\n"+
|
||||||
|
@@ -17,11 +17,11 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func whipGetICEServers(t *testing.T, ur string) []webrtc.ICEServer {
|
func whipGetICEServers(t *testing.T, hc *http.Client, ur string) []webrtc.ICEServer {
|
||||||
req, err := http.NewRequest("OPTIONS", ur, nil)
|
req, err := http.NewRequest("OPTIONS", ur, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := hc.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
@@ -35,7 +35,9 @@ func whipGetICEServers(t *testing.T, ur string) []webrtc.ICEServer {
|
|||||||
return servers
|
return servers
|
||||||
}
|
}
|
||||||
|
|
||||||
func whipPostOffer(t *testing.T, ur string, offer *webrtc.SessionDescription) (*webrtc.SessionDescription, string) {
|
func whipPostOffer(t *testing.T, hc *http.Client, ur string,
|
||||||
|
offer *webrtc.SessionDescription,
|
||||||
|
) (*webrtc.SessionDescription, string) {
|
||||||
enc, err := json.Marshal(offer)
|
enc, err := json.Marshal(offer)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@@ -44,7 +46,7 @@ func whipPostOffer(t *testing.T, ur string, offer *webrtc.SessionDescription) (*
|
|||||||
|
|
||||||
req.Header.Set("Content-Type", "application/sdp")
|
req.Header.Set("Content-Type", "application/sdp")
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := hc.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
@@ -79,7 +81,9 @@ func whipPostCandidate(t *testing.T, ur string, offer *webrtc.SessionDescription
|
|||||||
req.Header.Set("Content-Type", "application/trickle-ice-sdpfrag")
|
req.Header.Set("Content-Type", "application/trickle-ice-sdpfrag")
|
||||||
req.Header.Set("If-Match", etag)
|
req.Header.Set("If-Match", etag)
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
|
res, err := hc.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
@@ -94,8 +98,8 @@ type webRTCTestClient struct {
|
|||||||
closed chan struct{}
|
closed chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWebRTCTestClient(t *testing.T, ur string, publish bool) *webRTCTestClient {
|
func newWebRTCTestClient(t *testing.T, hc *http.Client, ur string, publish bool) *webRTCTestClient {
|
||||||
iceServers := whipGetICEServers(t, ur)
|
iceServers := whipGetICEServers(t, hc, ur)
|
||||||
|
|
||||||
pc, err := webrtc.NewPeerConnection(webrtc.Configuration{
|
pc, err := webrtc.NewPeerConnection(webrtc.Configuration{
|
||||||
ICEServers: iceServers,
|
ICEServers: iceServers,
|
||||||
@@ -170,7 +174,7 @@ func newWebRTCTestClient(t *testing.T, ur string, publish bool) *webRTCTestClien
|
|||||||
offer, err := pc.CreateOffer(nil)
|
offer, err := pc.CreateOffer(nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
answer, etag := whipPostOffer(t, ur, &offer)
|
answer, etag := whipPostOffer(t, hc, ur, &offer)
|
||||||
|
|
||||||
// test adding additional candidates, even if it is not mandatory here
|
// test adding additional candidates, even if it is not mandatory here
|
||||||
gatheringDone := make(chan struct{})
|
gatheringDone := make(chan struct{})
|
||||||
@@ -260,7 +264,9 @@ func TestWebRTCRead(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer source.Close()
|
defer source.Close()
|
||||||
|
|
||||||
c := newWebRTCTestClient(t, "http://localhost:8889/stream/whep", false)
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
|
c := newWebRTCTestClient(t, hc, "http://localhost:8889/stream/whep", false)
|
||||||
defer c.close()
|
defer c.close()
|
||||||
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
@@ -301,7 +307,9 @@ func TestWebRTCReadNotFound(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
iceServers := whipGetICEServers(t, "http://localhost:8889/stream/whep")
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
|
iceServers := whipGetICEServers(t, hc, "http://localhost:8889/stream/whep")
|
||||||
|
|
||||||
pc, err := webrtc.NewPeerConnection(webrtc.Configuration{
|
pc, err := webrtc.NewPeerConnection(webrtc.Configuration{
|
||||||
ICEServers: iceServers,
|
ICEServers: iceServers,
|
||||||
@@ -323,7 +331,7 @@ func TestWebRTCReadNotFound(t *testing.T) {
|
|||||||
|
|
||||||
req.Header.Set("Content-Type", "application/sdp")
|
req.Header.Set("Content-Type", "application/sdp")
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := hc.Do(req)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
@@ -336,7 +344,9 @@ func TestWebRTCPublish(t *testing.T) {
|
|||||||
require.Equal(t, true, ok)
|
require.Equal(t, true, ok)
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
s := newWebRTCTestClient(t, "http://localhost:8889/stream/whip", true)
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
|
s := newWebRTCTestClient(t, hc, "http://localhost:8889/stream/whip", true)
|
||||||
defer s.close()
|
defer s.close()
|
||||||
|
|
||||||
c := gortsplib.Client{
|
c := gortsplib.Client{
|
||||||
|
@@ -171,7 +171,9 @@ func TestHLSServerAuth(t *testing.T) {
|
|||||||
usr = "testreader2"
|
usr = "testreader2"
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := http.Get("http://" + usr + ":testpass@127.0.0.1:8888/teststream/index.m3u8?param=value")
|
hc := &http.Client{Transport: &http.Transport{}}
|
||||||
|
|
||||||
|
res, err := hc.Get("http://" + usr + ":testpass@127.0.0.1:8888/teststream/index.m3u8?param=value")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user