-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for core bilibili API wrappers.
- Loading branch information
Showing
6 changed files
with
235 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package bilibili | ||
|
||
import ( | ||
"github.com/keuin/slbr/common" | ||
"github.com/keuin/slbr/logging" | ||
"log" | ||
"testing" | ||
) | ||
|
||
func TestBilibili_GetDanmakuServerInfo(t *testing.T) { | ||
// get an online live room for testing | ||
liveList, err := common.GetLiveListForGuestUser() | ||
if err != nil { | ||
t.Fatalf("Cannot get live list for testing: %v", err) | ||
} | ||
lives := liveList.Data.Data | ||
if len(lives) <= 0 { | ||
t.Fatalf("No available live for guest user") | ||
} | ||
roomId := common.RoomId(lives[0].Roomid) | ||
|
||
logger := log.Default() | ||
bi := NewBilibili(logging.NewWrappedLogger(logger, "test-logger")) | ||
dmInfo, err := bi.GetDanmakuServerInfo(roomId) | ||
if err != nil { | ||
t.Fatalf("GetDanmakuServerInfo: %v", err) | ||
} | ||
if dmInfo.Code != 0 || | ||
dmInfo.Message != "0" || | ||
len(dmInfo.Data.Token) < 10 || | ||
len(dmInfo.Data.HostList) <= 0 { | ||
t.Fatalf("Invalid GetDanmakuServerInfo response: %v", dmInfo) | ||
} | ||
for _, h := range dmInfo.Data.HostList { | ||
if h.Port == 0 || h.WssPort == 0 || h.WsPort == 0 || h.Host == "" { | ||
t.Fatalf("Invalid host: %v", h) | ||
} | ||
} | ||
} |
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,35 @@ | ||
package bilibili | ||
|
||
import ( | ||
"github.com/keuin/slbr/common" | ||
"github.com/keuin/slbr/logging" | ||
"log" | ||
"testing" | ||
) | ||
|
||
func TestBilibili_GetStreamingInfo(t *testing.T) { | ||
// get an online live room for testing | ||
liveList, err := common.GetLiveListForGuestUser() | ||
if err != nil { | ||
t.Fatalf("cannot get live list for testing: %v", err) | ||
} | ||
lives := liveList.Data.Data | ||
if len(lives) <= 0 { | ||
t.Fatalf("no live for guest available") | ||
} | ||
roomId := common.RoomId(lives[0].Roomid) | ||
|
||
logger := log.Default() | ||
bi := NewBilibili(logging.NewWrappedLogger(logger, "test-logger")) | ||
info, err := bi.GetStreamingInfo(roomId) | ||
if err != nil { | ||
t.Fatalf("GetStreamingInfo: %v", err) | ||
} | ||
if info.Code != 0 || | ||
info.Message != "0" || | ||
len(info.Data.URLs) <= 0 || | ||
len(info.Data.AcceptQuality) <= 0 || | ||
len(info.Data.QualityDescription) <= 0 { | ||
t.Fatalf("Invalid GetStreamingInfo response: %v", info) | ||
} | ||
} |
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,36 @@ | ||
package bilibili | ||
|
||
import ( | ||
"github.com/keuin/slbr/common" | ||
"github.com/keuin/slbr/logging" | ||
"log" | ||
"testing" | ||
) | ||
|
||
func TestBilibili_GetRoomProfile(t *testing.T) { | ||
// get an online live room for testing | ||
liveList, err := common.GetLiveListForGuestUser() | ||
if err != nil { | ||
t.Fatalf("cannot get live list for testing: %v", err) | ||
} | ||
lives := liveList.Data.Data | ||
if len(lives) <= 0 { | ||
t.Fatalf("no live for guest available") | ||
} | ||
roomId := common.RoomId(lives[0].Roomid) | ||
|
||
logger := log.Default() | ||
bi := NewBilibili(logging.NewWrappedLogger(logger, "test-logger")) | ||
resp, err := bi.GetRoomProfile(roomId) | ||
if err != nil { | ||
t.Fatalf("GetRoomProfile: %v", err) | ||
} | ||
if resp.Code != 0 || | ||
resp.Message != "ok" || | ||
resp.Data.UID <= 0 || | ||
resp.Data.RoomID != int(roomId) || | ||
resp.Data.LiveStatus != int(Streaming) || | ||
resp.Data.Title == "" { | ||
t.Fatalf("Invalid GetRoomProfile response: %v", resp) | ||
} | ||
} |
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,35 @@ | ||
package bilibili | ||
|
||
import ( | ||
"github.com/keuin/slbr/common" | ||
"github.com/keuin/slbr/logging" | ||
"log" | ||
"testing" | ||
) | ||
|
||
func TestBilibili_GetRoomPlayInfo(t *testing.T) { | ||
// get an online live room for testing | ||
liveList, err := common.GetLiveListForGuestUser() | ||
if err != nil { | ||
t.Fatalf("cannot get live list for testing: %v", err) | ||
} | ||
lives := liveList.Data.Data | ||
if len(lives) <= 0 { | ||
t.Fatalf("no live for guest available") | ||
} | ||
roomId := common.RoomId(lives[0].Roomid) | ||
|
||
logger := log.Default() | ||
bi := NewBilibili(logging.NewWrappedLogger(logger, "test-logger")) | ||
resp, err := bi.GetRoomPlayInfo(roomId) | ||
if err != nil { | ||
t.Fatalf("GetRoomPlayInfo: %v", err) | ||
} | ||
if resp.Code != 0 || | ||
resp.Message != "0" || | ||
resp.Data.UID <= 0 || | ||
resp.Data.RoomID != uint64(roomId) || | ||
resp.Data.LiveStatus != Streaming { | ||
t.Fatalf("Invalid GetRoomPlayInfo response: %v", resp) | ||
} | ||
} |
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,44 @@ | ||
package bilibili | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/keuin/slbr/common" | ||
"github.com/keuin/slbr/logging" | ||
"log" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestBilibili_CopyLiveStream(t *testing.T) { | ||
// get an online live room for testing | ||
liveList, err := common.GetLiveListForGuestUser() | ||
if err != nil { | ||
t.Fatalf("cannot get live list for testing: %v", err) | ||
} | ||
lives := liveList.Data.Data | ||
if len(lives) <= 0 { | ||
t.Fatalf("no live for guest available") | ||
} | ||
roomId := common.RoomId(lives[0].Roomid) | ||
|
||
logger := log.Default() | ||
bi := NewBilibili(logging.NewWrappedLogger(logger, "test-logger")) | ||
|
||
si, err := bi.GetStreamingInfo(roomId) | ||
if err != nil { | ||
t.Fatalf("GetStreamingInfo: %v", err) | ||
} | ||
|
||
// test file open failure | ||
testErr := fmt.Errorf("test error") | ||
err = bi.CopyLiveStream(context.Background(), roomId, si.Data.URLs[0], func() (*os.File, error) { | ||
return nil, testErr | ||
}, 1048576) | ||
if !errors.Is(err, testErr) { | ||
t.Fatalf("Unexpected error from CopyLiveStream: %v", err) | ||
} | ||
|
||
// TODO more tests | ||
} |
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,46 @@ | ||
package common | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
/* | ||
Some utility function for test-purpose only. | ||
*/ | ||
|
||
type LiveList struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
TTL int `json:"ttl"` | ||
Data struct { | ||
Count int `json:"count"` | ||
Data []struct { | ||
Face string `json:"face"` | ||
Link string `json:"link"` | ||
Roomid int `json:"roomid"` | ||
Roomname string `json:"roomname"` | ||
Nickname string `json:"nickname"` | ||
} `json:"data"` | ||
} `json:"data"` | ||
} | ||
|
||
func GetLiveListForGuestUser() (liveList LiveList, err error) { | ||
url := "https://api.live.bilibili.com/xlive/web-interface/v1/index/WebGetUnLoginRecList" | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
err = fmt.Errorf("bad http response: %v", resp.StatusCode) | ||
return | ||
} | ||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return | ||
} | ||
err = json.Unmarshal(b, &liveList) | ||
return | ||
} |