Skip to content

Commit 7ce4587

Browse files
committed
net/http: add tests covering non-GET methods for file serving
ServeFile and FileServer will respond to methods such as DELETE by serving the file contents. This is surprising, but we don't want to change it without some consideration. Add tests covering the current behavior. For #59470 Change-Id: Ib6a2594c5b2b7f380149fc1628f7204b308161e1 Reviewed-on: https://go-review.googlesource.com/c/go/+/482876 Run-TryBot: Damien Neil <dneil@google.com> Reviewed-by: Tatiana Bradley <tatianabradley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
1 parent 96d2e41 commit 7ce4587

File tree

1 file changed

+78
-5
lines changed

1 file changed

+78
-5
lines changed

src/net/http/fs_test.go

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,39 @@ func testServeFile(t *testing.T, mode testMode) {
8787
if req.URL, err = url.Parse(ts.URL); err != nil {
8888
t.Fatal("ParseURL:", err)
8989
}
90-
req.Method = "GET"
9190

92-
// straight GET
93-
_, body := getBody(t, "straight get", req, c)
94-
if !bytes.Equal(body, file) {
95-
t.Fatalf("body mismatch: got %q, want %q", body, file)
91+
// Get contents via various methods.
92+
//
93+
// See https://go.dev/issue/59471 for a proposal to limit the set of methods handled.
94+
// For now, test the historical behavior.
95+
for _, method := range []string{
96+
MethodGet,
97+
MethodPost,
98+
MethodPut,
99+
MethodPatch,
100+
MethodDelete,
101+
MethodOptions,
102+
MethodTrace,
103+
} {
104+
req.Method = method
105+
_, body := getBody(t, method, req, c)
106+
if !bytes.Equal(body, file) {
107+
t.Fatalf("body mismatch for %v request: got %q, want %q", method, body, file)
108+
}
109+
}
110+
111+
// HEAD request.
112+
req.Method = MethodHead
113+
resp, body := getBody(t, "HEAD", req, c)
114+
if len(body) != 0 {
115+
t.Fatalf("body mismatch for HEAD request: got %q, want empty", body)
116+
}
117+
if got, want := resp.Header.Get("Content-Length"), fmt.Sprint(len(file)); got != want {
118+
t.Fatalf("Content-Length mismatch for HEAD request: got %v, want %v", got, want)
96119
}
97120

98121
// Range tests
122+
req.Method = MethodGet
99123
Cases:
100124
for _, rt := range ServeFileRangeTests {
101125
if rt.r != "" {
@@ -1521,3 +1545,52 @@ func testServeFileRejectsInvalidSuffixLengths(t *testing.T, mode testMode) {
15211545
})
15221546
}
15231547
}
1548+
1549+
func TestFileServerMethods(t *testing.T) {
1550+
run(t, testFileServerMethods)
1551+
}
1552+
func testFileServerMethods(t *testing.T, mode testMode) {
1553+
ts := newClientServerTest(t, mode, FileServer(Dir("testdata"))).ts
1554+
1555+
file, err := os.ReadFile(testFile)
1556+
if err != nil {
1557+
t.Fatal("reading file:", err)
1558+
}
1559+
1560+
// Get contents via various methods.
1561+
//
1562+
// See https://go.dev/issue/59471 for a proposal to limit the set of methods handled.
1563+
// For now, test the historical behavior.
1564+
for _, method := range []string{
1565+
MethodGet,
1566+
MethodHead,
1567+
MethodPost,
1568+
MethodPut,
1569+
MethodPatch,
1570+
MethodDelete,
1571+
MethodOptions,
1572+
MethodTrace,
1573+
} {
1574+
req, _ := NewRequest(method, ts.URL+"/file", nil)
1575+
t.Log(req.URL)
1576+
res, err := ts.Client().Do(req)
1577+
if err != nil {
1578+
t.Fatal(err)
1579+
}
1580+
body, err := io.ReadAll(res.Body)
1581+
res.Body.Close()
1582+
if err != nil {
1583+
t.Fatal(err)
1584+
}
1585+
wantBody := file
1586+
if method == MethodHead {
1587+
wantBody = nil
1588+
}
1589+
if !bytes.Equal(body, wantBody) {
1590+
t.Fatalf("%v: got body %q, want %q", method, body, wantBody)
1591+
}
1592+
if got, want := res.Header.Get("Content-Length"), fmt.Sprint(len(file)); got != want {
1593+
t.Fatalf("%v: got Content-Length %q, want %q", method, got, want)
1594+
}
1595+
}
1596+
}

0 commit comments

Comments
 (0)