This repository has been archived by the owner on Jan 16, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
147 lines (137 loc) · 3.38 KB
/
http_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package jcp_test
import (
"bytes"
"context"
"crypto/ed25519"
"crypto/rand"
"encoding/json"
"io"
"log"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/MicahParks/jwkset"
"github.com/MicahParks/keyfunc"
"github.com/golang-jwt/jwt/v4"
"go.uber.org/zap"
"github.com/MicahParks/jcp"
)
var privateKey, jwksServer = createJWKSServer()
func createJWKSServer() (ed25519.PrivateKey, *httptest.Server) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
j := jwkset.NewMemory[any]()
public, private, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
log.Fatalf("Failed to generate key pair: %v.", err)
}
meta := jwkset.NewKey[any](public, testKID)
err = j.Store.WriteKey(ctx, meta)
if err != nil {
log.Fatalf("Failed to store key: %v.", err)
}
rawJWKS, err := j.JSONPublic(ctx)
if err != nil {
log.Fatalf("Failed to get JWKS: %v.", err)
}
js := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err = w.Write(rawJWKS)
if err != nil {
log.Fatalf("Failed to write JWKS: %v.", err)
}
}))
return private, js
}
func TestProxy(t *testing.T) {
multiple := map[string]keyfunc.Options{
jwksServer.URL: {},
}
proxy, err := jcp.NewProxy(multiple, keyfunc.MultipleOptions{})
if err != nil {
t.Fatalf("Failed to create proxy: %v.", err)
}
handler := jcp.HTTPHandler{
Logger: zap.NewNop(),
Proxy: proxy,
RequestMaxBytes: jcp.DefaultRequestMaxBytes,
}.Validate()
j := jwt.New(jwt.SigningMethodEdDSA)
j.Header[headerKID] = testKID
goodJWT, err := j.SignedString(privateKey)
if err != nil {
t.Fatalf("Failed to sign token: %v.", err)
}
testCases := []struct {
args *jcp.ValidateArgs
contentType string
method string
name string
rawBody io.Reader
responseCode int
}{
{
method: http.MethodGet,
name: "MethodNotAllowed",
responseCode: http.StatusMethodNotAllowed,
},
{
contentType: anyOtherString,
name: "ContentType",
responseCode: http.StatusBadRequest,
},
{
name: "TooLarge",
rawBody: bytes.NewReader(make([]byte, jcp.DefaultRequestMaxBytes+1)),
responseCode: http.StatusRequestEntityTooLarge,
},
{
name: "InvalidJSON",
rawBody: bytes.NewReader([]byte(anyOtherString)),
responseCode: http.StatusBadRequest,
},
{
args: &jcp.ValidateArgs{
Token: anyOtherString,
},
name: "NonJWT",
responseCode: http.StatusBadRequest,
},
{
args: &jcp.ValidateArgs{
Token: goodJWT,
},
name: "GoodJWT",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
w := httptest.NewRecorder()
if tc.method == "" {
tc.method = http.MethodPost
}
if tc.args != nil {
req := jcp.ValidateRequest{
Args: *tc.args,
}
b, err := json.Marshal(req)
if err != nil {
t.Fatalf("Failed to marshal args: %v.", err)
}
tc.rawBody = bytes.NewReader(b)
}
r := httptest.NewRequest(tc.method, jwksServer.URL, tc.rawBody)
if tc.contentType == "" {
tc.contentType = jcp.ContentTypeJSON
}
r.Header.Set(jcp.HeaderContentType, tc.contentType)
handler.ServeHTTP(w, r)
if tc.responseCode == 0 {
tc.responseCode = http.StatusOK
}
if w.Code != tc.responseCode {
t.Fatalf("Expected response code %d, but got %d.", tc.responseCode, w.Code)
}
})
}
}