test(whep-client): add -token flag for JWT-gated /api/v3/whep endpoints
Some checks failed
tests / build (push) Failing after 2s
CodeQL / Analyze (pull_request) Failing after 2s
tests / build (pull_request) Failing after 1s

The M2 WHEP route lives under /api/v3 and inherits Core's JWT auth.
The M1 test client was written for the unauth'd PoC port; without
this flag it's useless against the real Core build.

- Subscribe() and postOffer() take a token string; empty means no
  Authorization header (M1 behavior preserved).
- main.go gains a -token flag.
- main_test.go pass empty token (existing tests run against an
  in-process unauth'd handler).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Zac Gaetano 2026-05-03 04:59:08 +00:00
parent f6d36bfa66
commit 0417aff3b1
2 changed files with 9 additions and 5 deletions

View file

@ -23,6 +23,7 @@ import (
func main() { func main() {
var ( var (
whepURL = flag.String("url", "http://127.0.0.1:8787/whep/test", "WHEP endpoint URL") whepURL = flag.String("url", "http://127.0.0.1:8787/whep/test", "WHEP endpoint URL")
token = flag.String("token", "", "Authorization: Bearer <token>; empty means no auth header")
timeout = flag.Duration("timeout", 10*time.Second, "overall subscribe+receive timeout") timeout = flag.Duration("timeout", 10*time.Second, "overall subscribe+receive timeout")
) )
flag.Parse() flag.Parse()
@ -30,7 +31,7 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), *timeout) ctx, cancel := context.WithTimeout(context.Background(), *timeout)
defer cancel() defer cancel()
if err := Subscribe(ctx, *whepURL); err != nil { if err := Subscribe(ctx, *whepURL, *token); err != nil {
log.Fatalf("subscribe failed: %v", err) log.Fatalf("subscribe failed: %v", err)
} }
fmt.Println("OK: received video and audio RTP") fmt.Println("OK: received video and audio RTP")
@ -40,7 +41,7 @@ func main() {
// Subscribe performs a full WHEP subscribe against whepURL and returns // Subscribe performs a full WHEP subscribe against whepURL and returns
// nil when both a video and an audio RTP packet have been observed // nil when both a video and an audio RTP packet have been observed
// before ctx expires. It is exported so tests can exercise it. // before ctx expires. It is exported so tests can exercise it.
func Subscribe(ctx context.Context, whepURL string) error { func Subscribe(ctx context.Context, whepURL, token string) error {
me := &webrtc.MediaEngine{} me := &webrtc.MediaEngine{}
if err := me.RegisterDefaultCodecs(); err != nil { if err := me.RegisterDefaultCodecs(); err != nil {
return fmt.Errorf("register codecs: %w", err) return fmt.Errorf("register codecs: %w", err)
@ -105,7 +106,7 @@ func Subscribe(ctx context.Context, whepURL string) error {
return fmt.Errorf("ice gather: %w", ctx.Err()) return fmt.Errorf("ice gather: %w", ctx.Err())
} }
answerSDP, err := postOffer(ctx, whepURL, pc.LocalDescription().SDP) answerSDP, err := postOffer(ctx, whepURL, token, pc.LocalDescription().SDP)
if err != nil { if err != nil {
return err return err
} }
@ -130,13 +131,16 @@ func Subscribe(ctx context.Context, whepURL string) error {
return nil return nil
} }
func postOffer(ctx context.Context, url, sdp string) (string, error) { func postOffer(ctx context.Context, url, token, sdp string) (string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, req, err := http.NewRequestWithContext(ctx, http.MethodPost, url,
bytes.NewReader([]byte(sdp))) bytes.NewReader([]byte(sdp)))
if err != nil { if err != nil {
return "", fmt.Errorf("new request: %w", err) return "", fmt.Errorf("new request: %w", err)
} }
req.Header.Set("Content-Type", "application/sdp") req.Header.Set("Content-Type", "application/sdp")
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {

View file

@ -104,7 +104,7 @@ func TestSubscribe_EndToEnd(t *testing.T) {
defer cancel() defer cancel()
whepURL := strings.TrimRight(ts.URL, "/") + "/whep/stream-e2e" whepURL := strings.TrimRight(ts.URL, "/") + "/whep/stream-e2e"
if err := Subscribe(ctx, whepURL); err != nil { if err := Subscribe(ctx, whepURL, ""); err != nil {
t.Fatalf("Subscribe: %v", err) t.Fatalf("Subscribe: %v", err)
} }
} }