github.com/pion/webrtc/v4@v4.0.1/icecredentialtype_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 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestNewICECredentialType(t *testing.T) { 14 testCases := []struct { 15 credentialTypeString string 16 expectedCredentialType ICECredentialType 17 }{ 18 {"password", ICECredentialTypePassword}, 19 {"oauth", ICECredentialTypeOauth}, 20 } 21 22 for i, testCase := range testCases { 23 tpe, err := newICECredentialType(testCase.credentialTypeString) 24 assert.NoError(t, err) 25 assert.Equal(t, 26 testCase.expectedCredentialType, tpe, 27 "testCase: %d %v", i, testCase, 28 ) 29 } 30 } 31 32 func TestICECredentialType_String(t *testing.T) { 33 testCases := []struct { 34 credentialType ICECredentialType 35 expectedString string 36 }{ 37 {ICECredentialTypePassword, "password"}, 38 {ICECredentialTypeOauth, "oauth"}, 39 } 40 41 for i, testCase := range testCases { 42 assert.Equal(t, 43 testCase.expectedString, 44 testCase.credentialType.String(), 45 "testCase: %d %v", i, testCase, 46 ) 47 } 48 } 49 50 func TestICECredentialType_new(t *testing.T) { 51 testCases := []struct { 52 credentialType ICECredentialType 53 expectedString string 54 }{ 55 {ICECredentialTypePassword, "password"}, 56 {ICECredentialTypeOauth, "oauth"}, 57 } 58 59 for i, testCase := range testCases { 60 tpe, err := newICECredentialType(testCase.expectedString) 61 assert.NoError(t, err) 62 assert.Equal(t, 63 tpe, testCase.credentialType, 64 "testCase: %d %v", i, testCase, 65 ) 66 } 67 } 68 69 func TestICECredentialType_Json(t *testing.T) { 70 testCases := []struct { 71 credentialType ICECredentialType 72 jsonRepresentation []byte 73 }{ 74 {ICECredentialTypePassword, []byte("\"password\"")}, 75 {ICECredentialTypeOauth, []byte("\"oauth\"")}, 76 } 77 78 for i, testCase := range testCases { 79 m, err := json.Marshal(testCase.credentialType) 80 assert.NoError(t, err) 81 assert.Equal(t, 82 testCase.jsonRepresentation, 83 m, 84 "Marshal testCase: %d %v", i, testCase, 85 ) 86 var ct ICECredentialType 87 err = json.Unmarshal(testCase.jsonRepresentation, &ct) 88 assert.NoError(t, err) 89 assert.Equal(t, 90 testCase.credentialType, 91 ct, 92 "Unmarshal testCase: %d %v", i, testCase, 93 ) 94 } 95 96 { 97 ct := ICECredentialType(1000) 98 err := json.Unmarshal([]byte("\"invalid\""), &ct) 99 assert.Error(t, err) 100 assert.Equal(t, ct, ICECredentialType(1000)) 101 err = json.Unmarshal([]byte("\"invalid"), &ct) 102 assert.Error(t, err) 103 assert.Equal(t, ct, ICECredentialType(1000)) 104 } 105 }