Skip to content

Commit 32f41c9

Browse files
committed
api: use connect client for sdp/peer candidate exchange
1 parent 117db6a commit 32f41c9

5 files changed

Lines changed: 250 additions & 277 deletions

File tree

api/end_to_end_test.go

Lines changed: 200 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"context"
55
"crypto/rand"
66
"crypto/rsa"
7+
"crypto/sha1"
78
"crypto/x509"
9+
"encoding/hex"
810
"encoding/json"
911
"encoding/pem"
1012
"fmt"
11-
"net/http"
1213
"net/http/httptest"
13-
"net/url"
1414
"os"
1515
"path/filepath"
1616
"testing"
@@ -20,9 +20,9 @@ import (
2020
"bringyour.com/bringyour/jwt"
2121
"bringyour.com/bringyour/model"
2222
"bringyour.com/bringyour/session"
23+
"bringyour.com/connect"
2324
"bringyour.com/service/api/apirouter"
24-
"bringyour.com/service/api/xweb"
25-
"github.com/bringyour/webrtc-conn/rest/client"
25+
webrtcconn "github.com/bringyour/webrtc-conn"
2626
"github.com/pion/webrtc/v3"
2727
"github.com/stretchr/testify/require"
2828
"github.com/testcontainers/testcontainers-go"
@@ -140,6 +140,12 @@ func setupPrivateKey(t *testing.T, tempDir string) {
140140

141141
}
142142

143+
func testID(t *testing.T) string {
144+
t.Helper()
145+
s := sha1.Sum([]byte(t.Name()))
146+
return hex.EncodeToString(s[:])
147+
}
148+
143149
func TestEndToEnd(t *testing.T) {
144150

145151
td := t.TempDir()
@@ -216,133 +222,245 @@ func TestEndToEnd(t *testing.T) {
216222

217223
require.Nil(t, cl.Error)
218224

219-
httpClient := s.Client()
225+
byApiClient := connect.NewBringYourApi(connect.DefaultClientStrategy(ctx), s.URL)
226+
byApiClient.SetByJwt(*cl.ByClientJwt)
220227

221228
t.Run("offer sdp", func(t *testing.T) {
222-
_, err = xweb.RequestJSON[string, json.RawMessage](ctx, "PUT", s.URL, "test offer body", []string{"peer-to-peer", "handshake", "123", "offer", "sdp"}, xweb.RequestOptions{
223-
HTTPClient: httpClient,
224-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
225-
})
229+
230+
handshakeID := testID(t)
231+
232+
err := byApiClient.PutPeerToPeerOfferSDPSync(
233+
ctx,
234+
handshakeID,
235+
webrtc.SessionDescription{
236+
Type: webrtc.SDPTypeOffer,
237+
SDP: "",
238+
},
239+
)
226240
require.NoError(t, err)
227241

228-
sdp, err := xweb.RequestJSON[string, string](ctx, "GET", s.URL, "", []string{"peer-to-peer", "handshake", "123", "offer", "sdp"}, xweb.RequestOptions{
229-
HTTPClient: httpClient,
230-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
231-
})
242+
sdp, err := byApiClient.PollPeerToPeerOfferSDPSync(
243+
ctx,
244+
handshakeID,
245+
)
232246

233247
require.NoError(t, err)
234-
require.Equal(t, "test offer body", sdp)
248+
require.Equal(
249+
t,
250+
webrtc.SessionDescription{
251+
Type: webrtc.SDPTypeOffer,
252+
SDP: "",
253+
},
254+
sdp,
255+
)
235256
})
236257

237258
t.Run("answer sdp", func(t *testing.T) {
238259

239-
_, err = xweb.RequestJSON[string, json.RawMessage](ctx, "PUT", s.URL, "test answer body", []string{"peer-to-peer", "handshake", "123", "answer", "sdp"}, xweb.RequestOptions{
240-
HTTPClient: httpClient,
241-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
242-
})
243-
require.NoError(t, err)
260+
handshakeID := testID(t)
244261

245-
sdp, err := xweb.RequestJSON[string, string](ctx, "GET", s.URL, "", []string{"peer-to-peer", "handshake", "123", "answer", "sdp"}, xweb.RequestOptions{
246-
HTTPClient: httpClient,
247-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
248-
})
262+
err := byApiClient.PutPeerToPeerAnswerSDPSync(
263+
ctx,
264+
handshakeID,
265+
webrtc.SessionDescription{
266+
Type: webrtc.SDPTypeAnswer,
267+
SDP: "",
268+
},
269+
)
270+
require.NoError(t, err)
249271

272+
sdp, err := byApiClient.PollPeerToPeerAnswerSDPSync(
273+
ctx,
274+
handshakeID,
275+
)
250276
require.NoError(t, err)
251-
require.Equal(t, "test answer body", sdp)
277+
278+
require.Equal(
279+
t,
280+
webrtc.SessionDescription{
281+
Type: webrtc.SDPTypeAnswer,
282+
SDP: "",
283+
},
284+
sdp,
285+
)
252286
})
253287

254288
t.Run("offer peer candidates", func(t *testing.T) {
255289

256-
_, err = xweb.RequestJSON[string, json.RawMessage](ctx, "POST", s.URL, "test peer candidate 1 body", []string{"peer-to-peer", "handshake", "123", "offer", "peer_candidates"}, xweb.RequestOptions{
257-
HTTPClient: httpClient,
258-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
259-
})
260-
require.NoError(t, err)
261-
262-
peerCandidates, err := xweb.RequestJSON[string, []string](ctx, "GET", s.URL, "", []string{"peer-to-peer", "handshake", "123", "offer", "peer_candidates"}, xweb.RequestOptions{
263-
HTTPClient: httpClient,
264-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
265-
})
290+
handshakeID := testID(t)
266291

267292
require.NoError(t, err)
268-
require.Equal(t, []string{"test peer candidate 1 body"}, peerCandidates)
269-
270-
_, err = xweb.RequestJSON[string, json.RawMessage](ctx, "POST", s.URL, "test peer candidate 2 body", []string{"peer-to-peer", "handshake", "123", "offer", "peer_candidates"}, xweb.RequestOptions{
271-
HTTPClient: httpClient,
272-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
273-
})
293+
err := byApiClient.PostPeerToPeerOfferPeerCandidateSync(
294+
ctx,
295+
handshakeID,
296+
webrtc.ICECandidate{
297+
Typ: webrtc.ICECandidateTypeHost,
298+
Foundation: "test",
299+
Priority: 1,
300+
Protocol: webrtc.ICEProtocolTCP,
301+
Address: "test1",
302+
Port: 1,
303+
},
304+
)
274305
require.NoError(t, err)
275306

276-
peerCandidates2, err := xweb.RequestJSON[string, []string](ctx, "GET", s.URL, "", []string{"peer-to-peer", "handshake", "123", "offer", "peer_candidates"}, xweb.RequestOptions{
277-
HTTPClient: httpClient,
278-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
279-
Query: url.Values{
280-
"from": []string{"1"},
307+
candidates, err := byApiClient.PollPeerToPeerOfferCandidatesSync(
308+
ctx,
309+
handshakeID,
310+
0,
311+
)
312+
require.NoError(t, err)
313+
require.Equal(
314+
t,
315+
[]webrtc.ICECandidate{
316+
{
317+
Typ: webrtc.ICECandidateTypeHost,
318+
Foundation: "test",
319+
Priority: 1,
320+
Protocol: webrtc.ICEProtocolTCP,
321+
Address: "test1",
322+
Port: 1,
323+
},
281324
},
282-
})
325+
candidates,
326+
)
327+
328+
err = byApiClient.PostPeerToPeerOfferPeerCandidateSync(
329+
ctx,
330+
handshakeID,
331+
webrtc.ICECandidate{
332+
Typ: webrtc.ICECandidateTypeHost,
333+
334+
Foundation: "test",
335+
Priority: 2,
336+
Protocol: webrtc.ICEProtocolTCP,
337+
Address: "test2",
338+
Port: 2,
339+
},
340+
)
341+
require.NoError(t, err)
283342

343+
candidates, err = byApiClient.PollPeerToPeerOfferCandidatesSync(
344+
ctx,
345+
handshakeID,
346+
1,
347+
)
284348
require.NoError(t, err)
285-
require.Equal(t, []string{"test peer candidate 2 body"}, peerCandidates2)
349+
require.Equal(
350+
t,
351+
[]webrtc.ICECandidate{
352+
{
353+
Typ: webrtc.ICECandidateTypeHost,
354+
355+
Foundation: "test",
356+
Priority: 2,
357+
Protocol: webrtc.ICEProtocolTCP,
358+
Address: "test2",
359+
Port: 2,
360+
},
361+
},
362+
candidates,
363+
)
286364

287365
})
288366

289367
t.Run("answer peer candidates", func(t *testing.T) {
290368

291-
_, err = xweb.RequestJSON[string, json.RawMessage](ctx, "POST", s.URL, "test peer candidate 1 body", []string{"peer-to-peer", "handshake", "123", "answer", "peer_candidates"}, xweb.RequestOptions{
292-
HTTPClient: httpClient,
293-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
294-
})
295-
require.NoError(t, err)
296-
297-
peerCandidates, err := xweb.RequestJSON[string, []string](ctx, "GET", s.URL, "", []string{"peer-to-peer", "handshake", "123", "answer", "peer_candidates"}, xweb.RequestOptions{
298-
HTTPClient: httpClient,
299-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
300-
})
301-
369+
handshakeID := testID(t)
370+
371+
err := byApiClient.PostPeerToPeerAnswerPeerCandidateSync(
372+
ctx,
373+
handshakeID,
374+
webrtc.ICECandidate{
375+
Typ: webrtc.ICECandidateTypeHost,
376+
Foundation: "test",
377+
Priority: 1,
378+
Protocol: webrtc.ICEProtocolTCP,
379+
Address: "test1",
380+
Port: 1,
381+
},
382+
)
302383
require.NoError(t, err)
303-
require.Equal(t, []string{"test peer candidate 1 body"}, peerCandidates)
304384

305-
_, err = xweb.RequestJSON[string, json.RawMessage](ctx, "POST", s.URL, "test peer candidate 2 body", []string{"peer-to-peer", "handshake", "123", "answer", "peer_candidates"}, xweb.RequestOptions{
306-
HTTPClient: httpClient,
307-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
308-
})
385+
candidates, err := byApiClient.PollPeerToPeerAnswerCandidatesSync(
386+
ctx,
387+
handshakeID,
388+
0,
389+
)
309390
require.NoError(t, err)
310-
311-
peerCandidates2, err := xweb.RequestJSON[string, []string](ctx, "GET", s.URL, "", []string{"peer-to-peer", "handshake", "123", "answer", "peer_candidates"}, xweb.RequestOptions{
312-
HTTPClient: httpClient,
313-
Header: http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
314-
Query: url.Values{
315-
"from": []string{"1"},
391+
require.Equal(
392+
t,
393+
[]webrtc.ICECandidate{
394+
{
395+
Typ: webrtc.ICECandidateTypeHost,
396+
Foundation: "test",
397+
Priority: 1,
398+
Protocol: webrtc.ICEProtocolTCP,
399+
Address: "test1",
400+
Port: 1,
401+
},
402+
}, candidates,
403+
)
404+
405+
err = byApiClient.PostPeerToPeerAnswerPeerCandidateSync(
406+
ctx,
407+
handshakeID,
408+
webrtc.ICECandidate{
409+
Typ: webrtc.ICECandidateTypeHost,
410+
411+
Foundation: "test",
412+
Priority: 2,
413+
Protocol: webrtc.ICEProtocolTCP,
414+
Address: "test2",
415+
Port: 2,
316416
},
317-
})
417+
)
418+
require.NoError(t, err)
318419

420+
candidates, err = byApiClient.PollPeerToPeerAnswerCandidatesSync(
421+
ctx,
422+
handshakeID,
423+
1,
424+
)
319425
require.NoError(t, err)
320-
require.Equal(t, []string{"test peer candidate 2 body"}, peerCandidates2)
426+
require.Equal(
427+
t,
428+
[]webrtc.ICECandidate{
429+
{
430+
Typ: webrtc.ICECandidateTypeHost,
431+
432+
Foundation: "test",
433+
Priority: 2,
434+
Protocol: webrtc.ICEProtocolTCP,
435+
Address: "test2",
436+
Port: 2,
437+
},
438+
},
439+
candidates,
440+
)
321441

322442
})
323443

324444
t.Run("establish WebRTC conn using the backend", func(t *testing.T) {
325445

446+
handshakeID := testID(t)
447+
326448
tctx, cancel := context.WithTimeout(ctx, 2*time.Second)
327449
defer cancel()
328450

329451
eg, egCtx := errgroup.WithContext(tctx)
330452

331-
baseURL, err := url.Parse(s.URL)
332-
require.NoError(t, err)
333-
334-
baseURL = baseURL.JoinPath("peer-to-peer", "handshake", "webrtc-123")
335-
336453
var offerReceived string
337454

338455
eg.Go(func() error {
339-
conn, err := client.Offer(
456+
457+
conn, err := webrtcconn.Offer(
340458
egCtx,
341-
baseURL.String(),
342-
http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
343459
webrtc.Configuration{},
460+
connect.NewWebRTCOfferHandshake(byApiClient, handshakeID),
344461
true,
345462
3,
463+
5*time.Second,
346464
)
347465
if err != nil {
348466
return fmt.Errorf("offer: %w", err)
@@ -370,11 +488,11 @@ func TestEndToEnd(t *testing.T) {
370488
var answerReceived string
371489

372490
eg.Go(func() error {
373-
conn, err := client.Answer(
491+
conn, err := webrtcconn.Answer(
374492
egCtx,
375-
baseURL.String(),
376-
http.Header{"Authorization": []string{"Bearer " + *cl.ByClientJwt}},
377493
webrtc.Configuration{},
494+
connect.NewWebRTCAnswerHandshake(byApiClient, handshakeID),
495+
5*time.Second,
378496
)
379497
if err != nil {
380498
return fmt.Errorf("answer: %w", err)

0 commit comments

Comments
 (0)