-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware_test.go
More file actions
103 lines (94 loc) · 2.7 KB
/
Copy pathmiddleware_test.go
File metadata and controls
103 lines (94 loc) · 2.7 KB
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
package attestation_test
import (
"bytes"
"context"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
"github.com/0xsequence/nitrocontrol/attestation"
"github.com/0xsequence/nitrocontrol/enclave"
"github.com/stretchr/testify/require"
)
func TestMiddleware(t *testing.T) {
enc, err := enclave.New(context.Background(), enclave.DummyProvider(nil), nil)
require.NoError(t, err)
errorFn := func(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(err.Error()))
}
loggerFromContextFn := func(ctx context.Context) *slog.Logger {
return slog.New(slog.DiscardHandler)
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
srv := httptest.NewServer(attestation.Middleware(enc, errorFn, loggerFromContextFn)(handler))
defer srv.Close()
tests := map[string]struct {
request func() (http.Header, []byte)
wantStatus int
wantErrText string
}{
"NoNonce": {
wantStatus: http.StatusOK,
request: func() (http.Header, []byte) {
return http.Header{}, []byte("test")
},
},
"ValidNonce": {
wantStatus: http.StatusOK,
request: func() (http.Header, []byte) {
return http.Header{
"X-Attestation-Nonce": []string{"test"},
}, []byte("test")
},
},
"InvalidNonce": {
wantStatus: http.StatusBadRequest,
wantErrText: "X-Attestation-Nonce value contains invalid characters",
request: func() (http.Header, []byte) {
return http.Header{
"X-Attestation-Nonce": []string{"!@#$%^&*()"},
}, []byte("test")
},
},
"LongNonce": {
wantStatus: http.StatusBadRequest,
wantErrText: "X-Attestation-Nonce value cannot be longer than 32",
request: func() (http.Header, []byte) {
return http.Header{
"X-Attestation-Nonce": []string{"test-123456789012345678901234567890123"},
}, []byte("test")
},
},
"WhitespaceNonce": {
wantStatus: http.StatusBadRequest,
wantErrText: "X-Attestation-Nonce value contains invalid characters",
request: func() (http.Header, []byte) {
return http.Header{
"X-Attestation-Nonce": []string{" \t a a \t "},
}, []byte("test")
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
header, reqBody := test.request()
req, err := http.NewRequest("POST", srv.URL, bytes.NewBuffer(reqBody))
require.NoError(t, err)
req.Header = header
resp, err := srv.Client().Do(req)
require.NoError(t, err)
defer func() {
require.NoError(t, resp.Body.Close())
}()
body, _ := io.ReadAll(resp.Body)
require.Equal(t, test.wantStatus, resp.StatusCode, string(body))
if test.wantErrText != "" {
require.Contains(t, string(body), test.wantErrText)
}
})
}
}