github.com/lestrrat-go/jwx/v2@v2.0.21/jwk/set_test.go (about)

     1  package jwk_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lestrrat-go/jwx/v2/internal/jwxtest"
     7  	"github.com/lestrrat-go/jwx/v2/jwk"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestSet(t *testing.T) {
    12  	set := jwk.NewSet()
    13  
    14  	keygens := []func() (jwk.Key, error){
    15  		jwxtest.GenerateRsaJwk,
    16  		jwxtest.GenerateEcdsaJwk,
    17  		jwxtest.GenerateSymmetricJwk,
    18  	}
    19  
    20  	//nolint:prealloc
    21  	var keys []jwk.Key
    22  	for _, gen := range keygens {
    23  		k, err := gen()
    24  		if !assert.NoError(t, err, `key generation should succeed`) {
    25  			return
    26  		}
    27  		if !assert.NoError(t, set.AddKey(k), `set.AddKey should succeed`) {
    28  			return
    29  		}
    30  		keys = append(keys, k)
    31  	}
    32  
    33  	if !assert.Equal(t, set.Len(), 3, `set.Len should be 3`) {
    34  		return
    35  	}
    36  
    37  	for i, k := range keys {
    38  		if !assert.Equal(t, i, set.Index(k), `set.Index should return %d`, i) {
    39  			return
    40  		}
    41  	}
    42  
    43  	for _, k := range keys {
    44  		if !assert.NoError(t, set.RemoveKey(k), `set.RemoveKey should succeed`) {
    45  			return
    46  		}
    47  	}
    48  
    49  	if !assert.Equal(t, set.Len(), 0, `set.Len should be 0`) {
    50  		return
    51  	}
    52  
    53  	for _, gen := range keygens {
    54  		k, err := gen()
    55  		if !assert.NoError(t, err, `key generation should succeed`) {
    56  			return
    57  		}
    58  		if !assert.NoError(t, set.AddKey(k), `set.Add should succeed`) {
    59  			return
    60  		}
    61  	}
    62  
    63  	if !assert.Equal(t, set.Len(), 3, `set.Len should be 3`) {
    64  		return
    65  	}
    66  
    67  	set.Clear()
    68  
    69  	if !assert.Equal(t, set.Len(), 0, `set.Len should be 0`) {
    70  		return
    71  	}
    72  }