File: sessiondescription_test.go

package info (click to toggle)
golang-github-pion-webrtc.v3 3.1.56-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,392 kB
  • sloc: javascript: 595; sh: 28; makefile: 5
file content (83 lines) | stat: -rw-r--r-- 2,104 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package webrtc

import (
	"encoding/json"
	"reflect"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestSessionDescription_JSON(t *testing.T) {
	testCases := []struct {
		desc           SessionDescription
		expectedString string
		unmarshalErr   error
	}{
		{SessionDescription{Type: SDPTypeOffer, SDP: "sdp"}, `{"type":"offer","sdp":"sdp"}`, nil},
		{SessionDescription{Type: SDPTypePranswer, SDP: "sdp"}, `{"type":"pranswer","sdp":"sdp"}`, nil},
		{SessionDescription{Type: SDPTypeAnswer, SDP: "sdp"}, `{"type":"answer","sdp":"sdp"}`, nil},
		{SessionDescription{Type: SDPTypeRollback, SDP: "sdp"}, `{"type":"rollback","sdp":"sdp"}`, nil},
		{SessionDescription{Type: SDPType(Unknown), SDP: "sdp"}, `{"type":"unknown","sdp":"sdp"}`, ErrUnknownType},
	}

	for i, testCase := range testCases {
		descData, err := json.Marshal(testCase.desc)
		assert.Nil(t,
			err,
			"testCase: %d %v marshal err: %v", i, testCase, err,
		)

		assert.Equal(t,
			string(descData),
			testCase.expectedString,
			"testCase: %d %v", i, testCase,
		)

		var desc SessionDescription
		err = json.Unmarshal(descData, &desc)

		if testCase.unmarshalErr != nil {
			assert.Equal(t,
				err,
				testCase.unmarshalErr,
				"testCase: %d %v", i, testCase,
			)
			continue
		}

		assert.Nil(t,
			err,
			"testCase: %d %v unmarshal err: %v", i, testCase, err,
		)

		assert.Equal(t,
			desc,
			testCase.desc,
			"testCase: %d %v", i, testCase,
		)
	}
}

func TestSessionDescription_Unmarshal(t *testing.T) {
	pc, err := NewPeerConnection(Configuration{})
	assert.NoError(t, err)
	offer, err := pc.CreateOffer(nil)
	assert.NoError(t, err)
	desc := SessionDescription{
		Type: offer.Type,
		SDP:  offer.SDP,
	}
	assert.Nil(t, desc.parsed)
	parsed1, err := desc.Unmarshal()
	assert.NotNil(t, parsed1)
	assert.NotNil(t, desc.parsed)
	assert.NoError(t, err)
	parsed2, err2 := desc.Unmarshal()
	assert.NotNil(t, parsed2)
	assert.NoError(t, err2)
	assert.NoError(t, pc.Close())

	// check if the two parsed results _really_ match, could be affected by internal caching
	assert.True(t, reflect.DeepEqual(parsed1, parsed2))
}