@@ -24,6 +24,7 @@ import (
24
24
"reflect"
25
25
"regexp"
26
26
"runtime"
27
+ "sort"
27
28
"strings"
28
29
"testing"
29
30
"time"
@@ -404,6 +405,47 @@ func TestFileServerImplicitLeadingSlash(t *testing.T) {
404
405
}
405
406
}
406
407
408
+ func TestFileServerMethodOptions (t * testing.T ) {
409
+ defer afterTest (t )
410
+ const want = "GET, HEAD, OPTIONS"
411
+ ts := httptest .NewServer (FileServer (Dir ("." )))
412
+ defer ts .Close ()
413
+
414
+ tests := []struct {
415
+ method string
416
+ wantStatus int
417
+ }{
418
+ {MethodOptions , StatusOK },
419
+
420
+ {MethodDelete , StatusMethodNotAllowed },
421
+ {MethodPut , StatusMethodNotAllowed },
422
+ {MethodPost , StatusMethodNotAllowed },
423
+ }
424
+
425
+ for _ , test := range tests {
426
+ req , err := NewRequest (test .method , ts .URL , nil )
427
+ if err != nil {
428
+ t .Fatal (err )
429
+ }
430
+ res , err := ts .Client ().Do (req )
431
+ if err != nil {
432
+ t .Fatal (err )
433
+ }
434
+ defer res .Body .Close ()
435
+
436
+ if res .StatusCode != test .wantStatus {
437
+ t .Errorf ("%s got status %q, want code %d" , test .method , res .Status , test .wantStatus )
438
+ }
439
+
440
+ a := strings .Split (res .Header .Get ("Allow" ), ", " )
441
+ sort .Strings (a )
442
+ got := strings .Join (a , ", " )
443
+ if got != want {
444
+ t .Errorf ("%s got Allow header %q, want %q" , test .method , got , want )
445
+ }
446
+ }
447
+ }
448
+
407
449
func TestDirJoin (t * testing.T ) {
408
450
if runtime .GOOS == "windows" {
409
451
t .Skip ("skipping test on windows" )
0 commit comments