-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
66 lines (53 loc) · 1.56 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/dgrijalva/jwt-go"
)
// Test JWKS endpoint
func TestJWKSHandler(t *testing.T) {
// Generate the RSA key pair for the test
generateKeyPair()
req, err := http.NewRequest("GET", "/.well-known/jwks.json", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(JWKSHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// Check if the returned key is valid
var jwks map[string][]map[string]interface{}
json.Unmarshal(rr.Body.Bytes(), &jwks)
if _, ok := jwks["keys"][0]["n"]; !ok {
t.Errorf("handler returned unexpected body: missing key 'n'")
}
}
// Test JWT generation
func TestGenerateJWTHandler(t *testing.T) {
// Generate the RSA key pair for the test
generateKeyPair()
req, err := http.NewRequest("GET", "/generate-jwt", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(GenerateJWTHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
tokenString := strings.TrimSpace(rr.Body.String())
_, err = jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return publicKey, nil
})
if err != nil {
t.Errorf("failed to parse generated JWT: %v", err)
}
}
// TODO: Add more specific tests, error scenarios, etc.