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

     1  package jwk_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/lestrrat-go/jwx/v2/jwa"
     8  	"github.com/lestrrat-go/jwx/v2/jwk"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestHeader(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	t.Run("Roundtrip", func(t *testing.T) {
    16  		t.Parallel()
    17  
    18  		values := map[string]interface{}{
    19  			jwk.KeyIDKey:                  "helloworld01",
    20  			jwk.KeyOpsKey:                 jwk.KeyOperationList{jwk.KeyOpSign},
    21  			jwk.KeyUsageKey:               "sig",
    22  			jwk.X509CertThumbprintKey:     "thumbprint",
    23  			jwk.X509CertThumbprintS256Key: "thumbprint256",
    24  			jwk.X509URLKey:                "cert1",
    25  			"private":                     "boofoo",
    26  		}
    27  
    28  		h, err := jwk.FromRaw([]byte("dummy"))
    29  		if !assert.NoError(t, err, `jwk.New should succeed`) {
    30  			return
    31  		}
    32  
    33  		for k, v := range values {
    34  			if !assert.NoError(t, h.Set(k, v), "Set works for '%s'", k) {
    35  				return
    36  			}
    37  
    38  			got, ok := h.Get(k)
    39  			if !assert.True(t, ok, "Get works for '%s'", k) {
    40  				return
    41  			}
    42  
    43  			if !assert.Equal(t, v, got, "values match '%s'", k) {
    44  				return
    45  			}
    46  
    47  			if !assert.NoError(t, h.Set(k, v), "Set works for '%s'", k) {
    48  				return
    49  			}
    50  		}
    51  
    52  		t.Run("Private params", func(t *testing.T) {
    53  			t.Parallel()
    54  			pp, err := h.AsMap(context.Background())
    55  			if !assert.NoError(t, err, `h.AsMap should succeed`) {
    56  				return
    57  			}
    58  
    59  			v, ok := pp["private"]
    60  			if !assert.True(t, ok, "key 'private' should exists") {
    61  				return
    62  			}
    63  
    64  			if !assert.Equal(t, v, "boofoo", "value for 'private' should match") {
    65  				return
    66  			}
    67  		})
    68  	})
    69  	t.Run("RoundtripError", func(t *testing.T) {
    70  		t.Parallel()
    71  		type dummyStruct struct {
    72  			dummy1 int
    73  			dummy2 float64
    74  		}
    75  		dummy := &dummyStruct{1, 3.4}
    76  		values := map[string]interface{}{
    77  			jwk.AlgorithmKey:              dummy,
    78  			jwk.KeyIDKey:                  dummy,
    79  			jwk.KeyUsageKey:               dummy,
    80  			jwk.KeyOpsKey:                 dummy,
    81  			jwk.X509CertChainKey:          dummy,
    82  			jwk.X509CertThumbprintKey:     dummy,
    83  			jwk.X509CertThumbprintS256Key: dummy,
    84  			jwk.X509URLKey:                dummy,
    85  		}
    86  
    87  		h, err := jwk.FromRaw([]byte("dummy"))
    88  		if !assert.NoError(t, err, `jwk.New should succeed`) {
    89  			return
    90  		}
    91  		for k, v := range values {
    92  			err := h.Set(k, v)
    93  			if err == nil {
    94  				t.Fatalf("Setting %s value should have failed", k)
    95  			}
    96  		}
    97  		if !assert.NoError(t, h.Set("Default", dummy), `Setting "Default" should succeed`) {
    98  			return
    99  		}
   100  		if !assert.Empty(t, h.Algorithm().String(), "Algorithm should be empty string") {
   101  			return
   102  		}
   103  		if h.KeyID() != "" {
   104  			t.Fatalf("KeyID should be empty string")
   105  		}
   106  		if h.KeyUsage() != "" {
   107  			t.Fatalf("KeyUsage should be empty string")
   108  		}
   109  		if h.KeyOps() != nil {
   110  			t.Fatalf("KeyOps should be empty string")
   111  		}
   112  	})
   113  
   114  	t.Run("Algorithm", func(t *testing.T) {
   115  		t.Parallel()
   116  		h, err := jwk.FromRaw([]byte("dummy"))
   117  		if !assert.NoError(t, err, `jwk.New should succeed`) {
   118  			return
   119  		}
   120  		for _, value := range []interface{}{jwa.RS256, jwa.RSA1_5} {
   121  			if !assert.NoError(t, h.Set(jwk.AlgorithmKey, value), "Set for alg should succeed") {
   122  				return
   123  			}
   124  
   125  			got, ok := h.Get("alg")
   126  			if !assert.True(t, ok, "Get for alg should succeed") {
   127  				return
   128  			}
   129  
   130  			if !assert.Equal(t, value, got, "values match") {
   131  				return
   132  			}
   133  		}
   134  	})
   135  }