-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
209 lines (175 loc) · 5.15 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package ece
import (
"bytes"
"crypto/rand"
"io"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestResponseWriter(t *testing.T) {
fn := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(plain)
}
encoding, ok := encodingFromKeySize(key)
if !ok {
t.Fatal("unsupported encoding")
}
h := Handler(key, 25, http.HandlerFunc(fn))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(plain))
req.Header.Add("Content-Encoding", encoding.Name)
req.Header.Add("Accept-Encoding", encoding.Name)
h.ServeHTTP(rec, req)
resp := rec.Result()
sent, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
// Ensure the data sent is not the same as the one passed.
assertNotEqual(t, "sent data", plain, sent)
// Try to decrypt it
r := NewReader(key, io.NopCloser(bytes.NewReader(sent)))
received, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}
assertEqual(t, "received data", plain, received)
}
// TestRequestBody sends a request with encrypted data,
// and expects the middleware to decrypt it transparently
// before it hits the handler.
func TestRequestBody(t *testing.T) {
fn := func(w http.ResponseWriter, r *http.Request) {
received, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
assertEqual(t, "request payload", plain, received)
}
encoding, ok := encodingFromKeySize(key)
if !ok {
t.Fatal("unsupported encoding")
}
h := Handler(key, 25, http.HandlerFunc(fn))
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(encrypted))
req.Header.Add("Content-Encoding", encoding.Name)
req.Header.Add("Accept-Encoding", encoding.Name)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
}
func TestIgnoreUnknownEncoding(t *testing.T) {
fn := func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
io.Copy(w, r.Body)
}
h := Handler(key, 25, http.HandlerFunc(fn))
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(encrypted))
req.Header.Add("Content-Encoding", "invalid string")
req.Header.Add("Accept-Encoding", "invalid string")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
resp := rec.Result()
if wanted := http.StatusOK; wanted != resp.StatusCode {
t.Fatal("invalid status code. Wanted:", wanted, "Got:", resp.StatusCode)
}
}
func TestMiddlewareInvalidKey(t *testing.T) {
fn := func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
io.Copy(w, r.Body)
}
h := Handler(key, 25, http.HandlerFunc(fn))
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(encrypted))
req.Header.Add("Content-Encoding", "aes256gcm")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
resp := rec.Result()
if wanted := http.StatusUnsupportedMediaType; wanted != resp.StatusCode {
t.Fatal("invalid status code. Wanted:", wanted, "Got:", resp.StatusCode)
}
}
func TestClient(t *testing.T) {
encoding, ok := encodingFromKeySize(key)
if !ok {
t.Fatal("unsupported encoding")
}
plainText := make([]byte, 10240)
if _, err := rand.Read(plainText); err != nil {
t.Fatal(err)
}
fn := func(w http.ResponseWriter, r *http.Request) {
ce := r.Header.Get("Content-Encoding")
assertEqual(t, "Content-Encoding", []byte(ce), []byte(encoding.Name))
// Payload should be plainText
payload, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
assertEqual(t, "payload received", plainText, payload)
// // Write plainText, with the expectation that it will be
// // transparently encrypted.
w.WriteHeader(http.StatusOK)
w.Write(plainText)
}
h := Handler(key, 25, http.HandlerFunc(fn))
server := httptest.NewServer(h)
t.Cleanup(server.Close)
client, err := NewClient("", key)
if err != nil {
t.Fatal(err)
}
resp, err := client.Post(server.URL, "application/json", bytes.NewReader(plainText))
if err != nil {
t.Fatal(err)
}
ce := resp.Header.Get("Content-Encoding")
assertEqual(t, "Content-Encoding", []byte(ce), []byte(encoding.Name))
received, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
assertEqual(t, "received", plainText, received)
}
func ExampleHandler() {
h := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
// r.Body now contains plain data if the client sent
// encrypted request.
// w.Write will encrypt the data before sending
// it back.
},
)
var (
key = []byte("256-bit long key")
rs = 4096
)
http.ListenAndServe(":8000", Handler(key, rs, h))
}
func ExampleClient() {
var (
keyID = "ID of the key below" // (Empty string to omit)
key = []byte("16 or 32 byte long key")
payload = strings.NewReader(`{"key": "value"}`)
)
c, err := NewClient(keyID, key)
if err != nil {
log.Fatalf("error initializing the client: %v", err)
}
resp, err := c.Post("https://api.example.com", "application/json", payload)
if err != nil {
log.Fatalf("HTTP request failed: %v", err)
}
// payload was encrypted before it was sent
// resp.Body is decrypted if the server returned an encrypted response.
data, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("error reading response: %v", err)
}
log.Println(data) // plain data
}