sessiondescription_test.go 2.2 KB

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