package web import ( "bytes" "crypto/rand" "crypto/rsa" "crypto/x509" "encoding/json" "encoding/pem" "net/http/httptest" "testing" "github.com/stretchr/testify/assert" "mokhan.ca/xlgmokha/idp/pkg/dto" ) func TestJsonWebKeySets(t *testing.T) { key, _ := rsa.GenerateKey(rand.Reader, 1024) b := new(bytes.Buffer) pem.Encode(b, &pem.Block{ Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key), }) cfg := &Configuration{ Issuer: "https://example.org", KeyData: b.Bytes(), } // h := NewHttpContext("https://example.org", b.Bytes()) h := NewHttpContext(cfg) t.Run(".well-known/jwks.json", func(t *testing.T) { w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/.well-known/jwks.json", nil) h.Router().ServeHTTP(w, r) assert.Equal(t, w.Header().Get("Content-Type"), "application/json") var c dto.JsonWebKeySet json.NewDecoder(w.Body).Decode(&c) assert.Equal(t, 1, len(c.Keys)) assert.Equal(t, "X", c.Keys[0].KeyId) assert.Equal(t, "RSA", c.Keys[0].KeyType) assert.NotEmpty(t, c.Keys[0].E) assert.NotEmpty(t, c.Keys[0].N) }) }