@@ -20,9 +20,11 @@ import (
20
20
"encoding/base64"
21
21
"encoding/json"
22
22
"encoding/pem"
23
+ "io"
23
24
"net/http"
24
25
"net/http/httptest"
25
26
"path"
27
+ "strings"
26
28
"testing"
27
29
"time"
28
30
@@ -153,6 +155,130 @@ func newPosixStorageFunc(t *testing.T) storage.CreateStorage {
153
155
}
154
156
}
155
157
158
+ func getHandlers (t * testing.T , handlers pathHandlers ) pathHandlers {
159
+ t .Helper ()
160
+ path := path .Join (prefix , types .GetRootsPath )
161
+ handler , ok := handlers [path ]
162
+ if ! ok {
163
+ t .Fatalf ("%q path not registered" , types .GetRootsPath )
164
+ }
165
+ return pathHandlers {path : handler }
166
+ }
167
+
168
+ func postHandlers (t * testing.T , handlers pathHandlers ) pathHandlers {
169
+ t .Helper ()
170
+ addChainPath := path .Join (prefix , types .AddChainPath )
171
+ addPreChainPath := path .Join (prefix , types .AddPreChainPath )
172
+
173
+ addChainHandler , ok := handlers [addChainPath ]
174
+ if ! ok {
175
+ t .Fatalf ("%q path not registered" , types .AddPreChainStr )
176
+ }
177
+ addPreChainHandler , ok := handlers [addPreChainPath ]
178
+ if ! ok {
179
+ t .Fatalf ("%q path not registered" , types .AddPreChainStr )
180
+ }
181
+
182
+ return map [string ]appHandler {
183
+ addChainPath : addChainHandler ,
184
+ addPreChainPath : addPreChainHandler ,
185
+ }
186
+ }
187
+
188
+ func TestPostHandlersRejectGet (t * testing.T ) {
189
+ log := setupTestLog (t )
190
+ opts := & HandlerOptions {
191
+ Deadline : time .Millisecond * 500 ,
192
+ RequestLog : & DefaultRequestLog {},
193
+ MaskInternalErrors : false ,
194
+ TimeSource : newFixedTimeSource (fakeTime ),
195
+ }
196
+ handlers := NewPathHandlers (opts , log )
197
+
198
+ // Anything in the post handler list should reject GET
199
+ for path , handler := range postHandlers (t , handlers ) {
200
+ t .Run (path , func (t * testing.T ) {
201
+ s := httptest .NewServer (handler )
202
+ defer s .Close ()
203
+
204
+ resp , err := http .Get (s .URL + path )
205
+ if err != nil {
206
+ t .Fatalf ("http.Get(%s)=(_,%q); want (_,nil)" , path , err )
207
+ }
208
+ if got , want := resp .StatusCode , http .StatusMethodNotAllowed ; got != want {
209
+ t .Errorf ("http.Get(%s)=(%d,nil); want (%d,nil)" , path , got , want )
210
+ }
211
+ })
212
+ }
213
+ }
214
+
215
+ func TestGetHandlersRejectPost (t * testing.T ) {
216
+ log := setupTestLog (t )
217
+ opts := & HandlerOptions {
218
+ Deadline : time .Millisecond * 500 ,
219
+ RequestLog : & DefaultRequestLog {},
220
+ MaskInternalErrors : false ,
221
+ TimeSource : newFixedTimeSource (fakeTime ),
222
+ }
223
+ handlers := NewPathHandlers (opts , log )
224
+
225
+ // Anything in the get handler list should reject POST.
226
+ for path , handler := range getHandlers (t , handlers ) {
227
+ t .Run (path , func (t * testing.T ) {
228
+ s := httptest .NewServer (handler )
229
+ defer s .Close ()
230
+
231
+ resp , err := http .Post (s .URL + path , "application/json" , nil )
232
+ if err != nil {
233
+ t .Fatalf ("http.Post(%s)=(_,%q); want (_,nil)" , path , err )
234
+ }
235
+ if got , want := resp .StatusCode , http .StatusMethodNotAllowed ; got != want {
236
+ t .Errorf ("http.Post(%s)=(%d,nil); want (%d,nil)" , path , got , want )
237
+ }
238
+ })
239
+ }
240
+ }
241
+
242
+ func TestPostHandlersFailure (t * testing.T ) {
243
+ var tests = []struct {
244
+ descr string
245
+ body io.Reader
246
+ want int
247
+ }{
248
+ {"nil" , nil , http .StatusBadRequest },
249
+ {"''" , strings .NewReader ("" ), http .StatusBadRequest },
250
+ {"malformed-json" , strings .NewReader ("{ !$%^& not valid json " ), http .StatusBadRequest },
251
+ {"empty-chain" , strings .NewReader (`{ "chain": [] }` ), http .StatusBadRequest },
252
+ {"wrong-chain" , strings .NewReader (`{ "chain": [ "test" ] }` ), http .StatusBadRequest },
253
+ }
254
+
255
+ log := setupTestLog (t )
256
+ opts := & HandlerOptions {
257
+ Deadline : time .Millisecond * 500 ,
258
+ RequestLog : & DefaultRequestLog {},
259
+ MaskInternalErrors : false ,
260
+ TimeSource : newFixedTimeSource (fakeTime ),
261
+ }
262
+ handlers := NewPathHandlers (opts , log )
263
+
264
+ for path , handler := range postHandlers (t , handlers ) {
265
+ t .Run (path , func (t * testing.T ) {
266
+ s := httptest .NewServer (handler )
267
+
268
+ for _ , test := range tests {
269
+ resp , err := http .Post (s .URL + path , "application/json" , test .body )
270
+ if err != nil {
271
+ t .Errorf ("http.Post(%s,%s)=(_,%q); want (_,nil)" , path , test .descr , err )
272
+ continue
273
+ }
274
+ if resp .StatusCode != test .want {
275
+ t .Errorf ("http.Post(%s,%s)=(%d,nil); want (%d,nil)" , path , test .descr , resp .StatusCode , test .want )
276
+ }
277
+ }
278
+ })
279
+ }
280
+ }
281
+
156
282
func TestNewPathHandlers (t * testing.T ) {
157
283
log := setupTestLog (t )
158
284
t .Run ("Handlers" , func (t * testing.T ) {
0 commit comments