github.com/pion/webrtc/v4@v4.0.1/sessiondescription_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package webrtc
     5  
     6  import (
     7  	"encoding/json"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestSessionDescription_JSON(t *testing.T) {
    15  	testCases := []struct {
    16  		desc           SessionDescription
    17  		expectedString string
    18  		unmarshalErr   error
    19  	}{
    20  		{SessionDescription{Type: SDPTypeOffer, SDP: "sdp"}, `{"type":"offer","sdp":"sdp"}`, nil},
    21  		{SessionDescription{Type: SDPTypePranswer, SDP: "sdp"}, `{"type":"pranswer","sdp":"sdp"}`, nil},
    22  		{SessionDescription{Type: SDPTypeAnswer, SDP: "sdp"}, `{"type":"answer","sdp":"sdp"}`, nil},
    23  		{SessionDescription{Type: SDPTypeRollback, SDP: "sdp"}, `{"type":"rollback","sdp":"sdp"}`, nil},
    24  		{SessionDescription{Type: SDPTypeUnknown, SDP: "sdp"}, `{"type":"unknown","sdp":"sdp"}`, ErrUnknownType},
    25  	}
    26  
    27  	for i, testCase := range testCases {
    28  		descData, err := json.Marshal(testCase.desc)
    29  		assert.Nil(t,
    30  			err,
    31  			"testCase: %d %v marshal err: %v", i, testCase, err,
    32  		)
    33  
    34  		assert.Equal(t,
    35  			string(descData),
    36  			testCase.expectedString,
    37  			"testCase: %d %v", i, testCase,
    38  		)
    39  
    40  		var desc SessionDescription
    41  		err = json.Unmarshal(descData, &desc)
    42  
    43  		if testCase.unmarshalErr != nil {
    44  			assert.Equal(t,
    45  				err,
    46  				testCase.unmarshalErr,
    47  				"testCase: %d %v", i, testCase,
    48  			)
    49  			continue
    50  		}
    51  
    52  		assert.Nil(t,
    53  			err,
    54  			"testCase: %d %v unmarshal err: %v", i, testCase, err,
    55  		)
    56  
    57  		assert.Equal(t,
    58  			desc,
    59  			testCase.desc,
    60  			"testCase: %d %v", i, testCase,
    61  		)
    62  	}
    63  }
    64  
    65  func TestSessionDescription_Unmarshal(t *testing.T) {
    66  	pc, err := NewPeerConnection(Configuration{})
    67  	assert.NoError(t, err)
    68  	offer, err := pc.CreateOffer(nil)
    69  	assert.NoError(t, err)
    70  	desc := SessionDescription{
    71  		Type: offer.Type,
    72  		SDP:  offer.SDP,
    73  	}
    74  	assert.Nil(t, desc.parsed)
    75  	parsed1, err := desc.Unmarshal()
    76  	assert.NotNil(t, parsed1)
    77  	assert.NotNil(t, desc.parsed)
    78  	assert.NoError(t, err)
    79  	parsed2, err2 := desc.Unmarshal()
    80  	assert.NotNil(t, parsed2)
    81  	assert.NoError(t, err2)
    82  	assert.NoError(t, pc.Close())
    83  
    84  	// check if the two parsed results _really_ match, could be affected by internal caching
    85  	assert.True(t, reflect.DeepEqual(parsed1, parsed2))
    86  }