github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/jsonparserw/jsonparserw_test.go (about)

     1  package jsonparserw
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestHappyPath(t *testing.T) {
    10  	jsonBlob := []byte("{\"foo\": 1, \"bar\": true, \"baz\": \"bing\"}")
    11  
    12  	foo, err := GetInt(jsonBlob, "foo")
    13  	require.NoError(t, err)
    14  	require.Equal(t, foo, int64(1))
    15  
    16  	bar, err := GetBoolean(jsonBlob, "bar")
    17  	require.NoError(t, err)
    18  	require.Equal(t, bar, true)
    19  
    20  	baz, err := GetString(jsonBlob, "baz")
    21  	require.NoError(t, err)
    22  	require.Equal(t, baz, "bing")
    23  }
    24  
    25  func TestDescriptiveErrorMessage(t *testing.T) {
    26  	jsonBlob := []byte("{\"raimbaut\": \"roussillon\", \"bradamante\": \"aedificium\"}")
    27  	_, err := GetString(jsonBlob, "agilulf", "bertrandin", "guildivern", "corbentraz")
    28  
    29  	// Should not include data to avoid leaking sensitive information in log sends
    30  	require.NotContains(t, err.Error(), "raimbaut")
    31  
    32  	// Should include keys
    33  	require.Contains(t, err.Error(), "with keys [agilulf bertrandin guildivern corbentraz]")
    34  
    35  	// Should include original error message
    36  	require.Contains(t, err.Error(), "Key path not found")
    37  }