// Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package http_test import ( "bytes" "fmt" "io" "log" . "net/http" "slices" "strconv" "strings" "testing" ) var sniffTests = []struct { desc string data []byte contentType string }{ // Some nonsense. {"Empty", []byte{}, "text/plain; charset=utf-8"}, {"Binary", []byte{1, 2, 3}, "application/octet-stream"}, {"HTML document #1", []byte(`blah blah blah`), "text/html; charset=utf-8"}, {"HTML document #2", []byte(``), "text/html; charset=utf-8"}, {"HTML document #3 (leading whitespace)", []byte(` ...`), "text/html; charset=utf-8"}, {"HTML document #4 (leading CRLF)", []byte("\r\n..."), "text/html; charset=utf-8"}, {"Plain text", []byte(`This is not HTML. It has ☃ though.`), "text/plain; charset=utf-8"}, {"XML", []byte("\nhi") })) resp, err := cst.c.Get(cst.ts.URL) if err != nil { t.Fatal(err) } got := resp.Header["Content-Type"] want := []string{""} if !slices.Equal(got, want) { t.Errorf("Content-Type = %q; want %q", got, want) } resp.Body.Close() } type byteAtATimeReader struct { buf []byte } func (b *byteAtATimeReader) Read(p []byte) (n int, err error) { if len(p) < 1 { return 0, nil } if len(b.buf) == 0 { return 0, io.EOF } p[0] = b.buf[0] b.buf = b.buf[1:] return 1, nil } func TestContentTypeWithVariousSources(t *testing.T) { run(t, testContentTypeWithVariousSources) } func testContentTypeWithVariousSources(t *testing.T, mode testMode) { const ( input = "\n\n\t\n" expected = "text/html; charset=utf-8" ) for _, test := range []struct { name string handler func(ResponseWriter, *Request) }{{ name: "write", handler: func(w ResponseWriter, r *Request) { // Write the whole input at once. n, err := w.Write([]byte(input)) if int(n) != len(input) || err != nil { t.Errorf("w.Write(%q) = %v, %v want %d, nil", input, n, err, len(input)) } }, }, { name: "write one byte at a time", handler: func(w ResponseWriter, r *Request) { // Write the input one byte at a time. buf := []byte(input) for i := range buf { n, err := w.Write(buf[i : i+1]) if n != 1 || err != nil { t.Errorf("w.Write(%q) = %v, %v want 1, nil", input, n, err) } } }, }, { name: "copy from Reader", handler: func(w ResponseWriter, r *Request) { // Use io.Copy from a plain Reader. type readerOnly struct{ io.Reader } buf := bytes.NewBuffer([]byte(input)) n, err := io.Copy(w, readerOnly{buf}) if int(n) != len(input) || err != nil { t.Errorf("io.Copy(w, %q) = %v, %v want %d, nil", input, n, err, len(input)) } }, }, { name: "copy from bytes.Buffer", handler: func(w ResponseWriter, r *Request) { // Use io.Copy from a bytes.Buffer to trigger ReadFrom. buf := bytes.NewBuffer([]byte(input)) n, err := io.Copy(w, buf) if int(n) != len(input) || err != nil { t.Errorf("io.Copy(w, %q) = %v, %v want %d, nil", input, n, err, len(input)) } }, }, { name: "copy one byte at a time", handler: func(w ResponseWriter, r *Request) { // Use io.Copy from a Reader that returns one byte at a time. n, err := io.Copy(w, &byteAtATimeReader{[]byte(input)}) if int(n) != len(input) || err != nil { t.Errorf("io.Copy(w, %q) = %v, %v want %d, nil", input, n, err, len(input)) } }, }} { t.Run(test.name, func(t *testing.T) { cst := newClientServerTest(t, mode, HandlerFunc(test.handler)) resp, err := cst.c.Get(cst.ts.URL) if err != nil { t.Fatalf("Get: %v", err) } if ct := resp.Header.Get("Content-Type"); ct != expected { t.Errorf("Content-Type = %q, want %q", ct, expected) } if want, got := resp.Header.Get("Content-Length"), fmt.Sprint(len(input)); want != got { t.Errorf("Content-Length = %q, want %q", want, got) } data, err := io.ReadAll(resp.Body) if err != nil { t.Errorf("reading body: %v", err) } else if !bytes.Equal(data, []byte(input)) { t.Errorf("data is %q, want %q", data, input) } resp.Body.Close() }) } } func TestSniffWriteSize(t *testing.T) { run(t, testSniffWriteSize) } func testSniffWriteSize(t *testing.T, mode testMode) { cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) { size, _ := strconv.Atoi(r.FormValue("size")) written, err := io.WriteString(w, strings.Repeat("a", size)) if err != nil { t.Errorf("write of %d bytes: %v", size, err) return } if written != size { t.Errorf("write of %d bytes wrote %d bytes", size, written) } })) for _, size := range []int{0, 1, 200, 600, 999, 1000, 1023, 1024, 512 << 10, 1 << 20} { res, err := cst.c.Get(fmt.Sprintf("%s/?size=%d", cst.ts.URL, size)) if err != nil { t.Fatalf("size %d: %v", size, err) } if _, err := io.Copy(io.Discard, res.Body); err != nil { t.Fatalf("size %d: io.Copy of body = %v", size, err) } if err := res.Body.Close(); err != nil { t.Fatalf("size %d: body Close = %v", size, err) } } }