mirror of
https://github.com/golang/go
synced 2024-11-19 13:44:52 -07:00
c2ef005486
Failing ones are marked skipped. Fixes #13543 (was just a test issue) Updates #13555 (to be fixed later) Updates #13556 (to be fixed later) Updates #13557 (to be fixed later) Fixes bug in golang.org/cl/17428 (http1 now uses HTTP status 431, not 413) Change-Id: I8f7431fee35f2fc081cfe2c232ae75a00800a60b Reviewed-on: https://go-review.googlesource.com/17683 Reviewed-by: Blake Mizerany <blake.mizerany@gmail.com> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Reviewed-by: Burcu Dogan <jbd@google.com> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
178 lines
5.4 KiB
Go
178 lines
5.4 KiB
Go
// 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"
|
|
"io/ioutil"
|
|
"log"
|
|
. "net/http"
|
|
"reflect"
|
|
"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(`<HtMl><bOdY>blah blah blah</body></html>`), "text/html; charset=utf-8"},
|
|
{"HTML document #2", []byte(`<HTML></HTML>`), "text/html; charset=utf-8"},
|
|
{"HTML document #3 (leading whitespace)", []byte(` <!DOCTYPE HTML>...`), "text/html; charset=utf-8"},
|
|
{"HTML document #4 (leading CRLF)", []byte("\r\n<html>..."), "text/html; charset=utf-8"},
|
|
|
|
{"Plain text", []byte(`This is not HTML. It has ☃ though.`), "text/plain; charset=utf-8"},
|
|
|
|
{"XML", []byte("\n<?xml!"), "text/xml; charset=utf-8"},
|
|
|
|
// Image types.
|
|
{"GIF 87a", []byte(`GIF87a`), "image/gif"},
|
|
{"GIF 89a", []byte(`GIF89a...`), "image/gif"},
|
|
|
|
{"MP4 video", []byte("\x00\x00\x00\x18ftypmp42\x00\x00\x00\x00mp42isom<\x06t\xbfmdat"), "video/mp4"},
|
|
}
|
|
|
|
func TestDetectContentType(t *testing.T) {
|
|
for _, tt := range sniffTests {
|
|
ct := DetectContentType(tt.data)
|
|
if ct != tt.contentType {
|
|
t.Errorf("%v: DetectContentType = %q, want %q", tt.desc, ct, tt.contentType)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestServerContentType_h1(t *testing.T) { testServerContentType(t, h1Mode) }
|
|
func TestServerContentType_h2(t *testing.T) { testServerContentType(t, h2Mode) }
|
|
|
|
func testServerContentType(t *testing.T, h2 bool) {
|
|
defer afterTest(t)
|
|
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
|
|
i, _ := strconv.Atoi(r.FormValue("i"))
|
|
tt := sniffTests[i]
|
|
n, err := w.Write(tt.data)
|
|
if n != len(tt.data) || err != nil {
|
|
log.Fatalf("%v: Write(%q) = %v, %v want %d, nil", tt.desc, tt.data, n, err, len(tt.data))
|
|
}
|
|
}))
|
|
defer cst.close()
|
|
|
|
for i, tt := range sniffTests {
|
|
resp, err := cst.c.Get(cst.ts.URL + "/?i=" + strconv.Itoa(i))
|
|
if err != nil {
|
|
t.Errorf("%v: %v", tt.desc, err)
|
|
continue
|
|
}
|
|
if ct := resp.Header.Get("Content-Type"); ct != tt.contentType {
|
|
t.Errorf("%v: Content-Type = %q, want %q", tt.desc, ct, tt.contentType)
|
|
}
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Errorf("%v: reading body: %v", tt.desc, err)
|
|
} else if !bytes.Equal(data, tt.data) {
|
|
t.Errorf("%v: data is %q, want %q", tt.desc, data, tt.data)
|
|
}
|
|
resp.Body.Close()
|
|
}
|
|
}
|
|
|
|
// Issue 5953: shouldn't sniff if the handler set a Content-Type header,
|
|
// even if it's the empty string.
|
|
func TestServerIssue5953_h1(t *testing.T) { testServerIssue5953(t, h1Mode) }
|
|
func TestServerIssue5953_h2(t *testing.T) { testServerIssue5953(t, h2Mode) }
|
|
func testServerIssue5953(t *testing.T, h2 bool) {
|
|
defer afterTest(t)
|
|
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
|
|
w.Header()["Content-Type"] = []string{""}
|
|
fmt.Fprintf(w, "<html><head></head><body>hi</body></html>")
|
|
}))
|
|
defer cst.close()
|
|
|
|
resp, err := cst.c.Get(cst.ts.URL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got := resp.Header["Content-Type"]
|
|
want := []string{""}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("Content-Type = %q; want %q", got, want)
|
|
}
|
|
resp.Body.Close()
|
|
}
|
|
|
|
func TestContentTypeWithCopy_h1(t *testing.T) { testContentTypeWithCopy(t, h1Mode) }
|
|
func TestContentTypeWithCopy_h2(t *testing.T) { testContentTypeWithCopy(t, h2Mode) }
|
|
func testContentTypeWithCopy(t *testing.T, h2 bool) {
|
|
defer afterTest(t)
|
|
|
|
const (
|
|
input = "\n<html>\n\t<head>\n"
|
|
expected = "text/html; charset=utf-8"
|
|
)
|
|
|
|
cst := newClientServerTest(t, h2, HandlerFunc(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))
|
|
}
|
|
}))
|
|
defer cst.close()
|
|
|
|
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)
|
|
}
|
|
data, err := ioutil.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_h1(t *testing.T) { testSniffWriteSize(t, h1Mode) }
|
|
func TestSniffWriteSize_h2(t *testing.T) { testSniffWriteSize(t, h2Mode) }
|
|
func testSniffWriteSize(t *testing.T, h2 bool) {
|
|
defer afterTest(t)
|
|
cst := newClientServerTest(t, h2, 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)
|
|
}
|
|
}))
|
|
defer cst.close()
|
|
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(ioutil.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)
|
|
}
|
|
}
|
|
}
|