main
 1package web
 2
 3import (
 4	"bytes"
 5	"crypto/rand"
 6	"crypto/rsa"
 7	"crypto/x509"
 8	"encoding/json"
 9	"encoding/pem"
10	"net/http/httptest"
11	"testing"
12
13	"github.com/stretchr/testify/assert"
14	"mokhan.ca/xlgmokha/idp/pkg/dto"
15)
16
17func TestJsonWebKeySets(t *testing.T) {
18	key, _ := rsa.GenerateKey(rand.Reader, 1024)
19	b := new(bytes.Buffer)
20	pem.Encode(b, &pem.Block{
21		Type:  "RSA PRIVATE KEY",
22		Bytes: x509.MarshalPKCS1PrivateKey(key),
23	})
24
25	cfg := &Configuration{
26		Issuer:  "https://example.org",
27		KeyData: b.Bytes(),
28	}
29	// h := NewHttpContext("https://example.org", b.Bytes())
30	h := NewHttpContext(cfg)
31
32	t.Run(".well-known/jwks.json", func(t *testing.T) {
33		w := httptest.NewRecorder()
34		r := httptest.NewRequest("GET", "/.well-known/jwks.json", nil)
35
36		h.Router().ServeHTTP(w, r)
37
38		assert.Equal(t, w.Header().Get("Content-Type"), "application/json")
39
40		var c dto.JsonWebKeySet
41		json.NewDecoder(w.Body).Decode(&c)
42
43		assert.Equal(t, 1, len(c.Keys))
44		assert.Equal(t, "X", c.Keys[0].KeyId)
45		assert.Equal(t, "RSA", c.Keys[0].KeyType)
46		assert.NotEmpty(t, c.Keys[0].E)
47		assert.NotEmpty(t, c.Keys[0].N)
48	})
49}