| 1 |
747 |
jeremybenn |
// Copyright 2011 The Go Authors. All rights reserved.
|
| 2 |
|
|
// Use of this source code is governed by a BSD-style
|
| 3 |
|
|
// license that can be found in the LICENSE file.
|
| 4 |
|
|
|
| 5 |
|
|
package http_test
|
| 6 |
|
|
|
| 7 |
|
|
import (
|
| 8 |
|
|
"bytes"
|
| 9 |
|
|
"fmt"
|
| 10 |
|
|
"io"
|
| 11 |
|
|
"io/ioutil"
|
| 12 |
|
|
"log"
|
| 13 |
|
|
. "net/http"
|
| 14 |
|
|
"net/http/httptest"
|
| 15 |
|
|
"strconv"
|
| 16 |
|
|
"strings"
|
| 17 |
|
|
"testing"
|
| 18 |
|
|
)
|
| 19 |
|
|
|
| 20 |
|
|
var sniffTests = []struct {
|
| 21 |
|
|
desc string
|
| 22 |
|
|
data []byte
|
| 23 |
|
|
contentType string
|
| 24 |
|
|
}{
|
| 25 |
|
|
// Some nonsense.
|
| 26 |
|
|
{"Empty", []byte{}, "text/plain; charset=utf-8"},
|
| 27 |
|
|
{"Binary", []byte{1, 2, 3}, "application/octet-stream"},
|
| 28 |
|
|
|
| 29 |
|
|
{"HTML document #1", []byte(`blah blah blah`), "text/html; charset=utf-8"},
|
| 30 |
|
|
{"HTML document #2", []byte(``), "text/html; charset=utf-8"},
|
| 31 |
|
|
{"HTML document #3 (leading whitespace)", []byte(` ...`), "text/html; charset=utf-8"},
|
| 32 |
|
|
{"HTML document #4 (leading CRLF)", []byte("\r\n..."), "text/html; charset=utf-8"},
|
| 33 |
|
|
|
| 34 |
|
|
{"Plain text", []byte(`This is not HTML. It has ☃ though.`), "text/plain; charset=utf-8"},
|
| 35 |
|
|
|
| 36 |
|
|
{"XML", []byte("\n
|
| 37 |
|
|
|
| 38 |
|
|
// Image types.
|
| 39 |
|
|
{"GIF 87a", []byte(`GIF87a`), "image/gif"},
|
| 40 |
|
|
{"GIF 89a", []byte(`GIF89a...`), "image/gif"},
|
| 41 |
|
|
|
| 42 |
|
|
// TODO(dsymonds): Re-enable this when the spec is sorted w.r.t. MP4.
|
| 43 |
|
|
//{"MP4 video", []byte("\x00\x00\x00\x18ftypmp42\x00\x00\x00\x00mp42isom<\x06t\xbfmdat"), "video/mp4"},
|
| 44 |
|
|
//{"MP4 audio", []byte("\x00\x00\x00\x20ftypM4A \x00\x00\x00\x00M4A mp42isom\x00\x00\x00\x00"), "audio/mp4"},
|
| 45 |
|
|
}
|
| 46 |
|
|
|
| 47 |
|
|
func TestDetectContentType(t *testing.T) {
|
| 48 |
|
|
for _, tt := range sniffTests {
|
| 49 |
|
|
ct := DetectContentType(tt.data)
|
| 50 |
|
|
if ct != tt.contentType {
|
| 51 |
|
|
t.Errorf("%v: DetectContentType = %q, want %q", tt.desc, ct, tt.contentType)
|
| 52 |
|
|
}
|
| 53 |
|
|
}
|
| 54 |
|
|
}
|
| 55 |
|
|
|
| 56 |
|
|
func TestServerContentType(t *testing.T) {
|
| 57 |
|
|
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
|
| 58 |
|
|
i, _ := strconv.Atoi(r.FormValue("i"))
|
| 59 |
|
|
tt := sniffTests[i]
|
| 60 |
|
|
n, err := w.Write(tt.data)
|
| 61 |
|
|
if n != len(tt.data) || err != nil {
|
| 62 |
|
|
log.Fatalf("%v: Write(%q) = %v, %v want %d, nil", tt.desc, tt.data, n, err, len(tt.data))
|
| 63 |
|
|
}
|
| 64 |
|
|
}))
|
| 65 |
|
|
defer ts.Close()
|
| 66 |
|
|
|
| 67 |
|
|
for i, tt := range sniffTests {
|
| 68 |
|
|
resp, err := Get(ts.URL + "/?i=" + strconv.Itoa(i))
|
| 69 |
|
|
if err != nil {
|
| 70 |
|
|
t.Errorf("%v: %v", tt.desc, err)
|
| 71 |
|
|
continue
|
| 72 |
|
|
}
|
| 73 |
|
|
if ct := resp.Header.Get("Content-Type"); ct != tt.contentType {
|
| 74 |
|
|
t.Errorf("%v: Content-Type = %q, want %q", tt.desc, ct, tt.contentType)
|
| 75 |
|
|
}
|
| 76 |
|
|
data, err := ioutil.ReadAll(resp.Body)
|
| 77 |
|
|
if err != nil {
|
| 78 |
|
|
t.Errorf("%v: reading body: %v", tt.desc, err)
|
| 79 |
|
|
} else if !bytes.Equal(data, tt.data) {
|
| 80 |
|
|
t.Errorf("%v: data is %q, want %q", tt.desc, data, tt.data)
|
| 81 |
|
|
}
|
| 82 |
|
|
resp.Body.Close()
|
| 83 |
|
|
}
|
| 84 |
|
|
}
|
| 85 |
|
|
|
| 86 |
|
|
func TestContentTypeWithCopy(t *testing.T) {
|
| 87 |
|
|
const (
|
| 88 |
|
|
input = "\n\n\t\n"
|
| 89 |
|
|
expected = "text/html; charset=utf-8"
|
| 90 |
|
|
)
|
| 91 |
|
|
|
| 92 |
|
|
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
|
| 93 |
|
|
// Use io.Copy from a bytes.Buffer to trigger ReadFrom.
|
| 94 |
|
|
buf := bytes.NewBuffer([]byte(input))
|
| 95 |
|
|
n, err := io.Copy(w, buf)
|
| 96 |
|
|
if int(n) != len(input) || err != nil {
|
| 97 |
|
|
t.Errorf("io.Copy(w, %q) = %v, %v want %d, nil", input, n, err, len(input))
|
| 98 |
|
|
}
|
| 99 |
|
|
}))
|
| 100 |
|
|
defer ts.Close()
|
| 101 |
|
|
|
| 102 |
|
|
resp, err := Get(ts.URL)
|
| 103 |
|
|
if err != nil {
|
| 104 |
|
|
t.Fatalf("Get: %v", err)
|
| 105 |
|
|
}
|
| 106 |
|
|
if ct := resp.Header.Get("Content-Type"); ct != expected {
|
| 107 |
|
|
t.Errorf("Content-Type = %q, want %q", ct, expected)
|
| 108 |
|
|
}
|
| 109 |
|
|
data, err := ioutil.ReadAll(resp.Body)
|
| 110 |
|
|
if err != nil {
|
| 111 |
|
|
t.Errorf("reading body: %v", err)
|
| 112 |
|
|
} else if !bytes.Equal(data, []byte(input)) {
|
| 113 |
|
|
t.Errorf("data is %q, want %q", data, input)
|
| 114 |
|
|
}
|
| 115 |
|
|
resp.Body.Close()
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
func TestSniffWriteSize(t *testing.T) {
|
| 119 |
|
|
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
|
| 120 |
|
|
size, _ := strconv.Atoi(r.FormValue("size"))
|
| 121 |
|
|
written, err := io.WriteString(w, strings.Repeat("a", size))
|
| 122 |
|
|
if err != nil {
|
| 123 |
|
|
t.Errorf("write of %d bytes: %v", size, err)
|
| 124 |
|
|
return
|
| 125 |
|
|
}
|
| 126 |
|
|
if written != size {
|
| 127 |
|
|
t.Errorf("write of %d bytes wrote %d bytes", size, written)
|
| 128 |
|
|
}
|
| 129 |
|
|
}))
|
| 130 |
|
|
defer ts.Close()
|
| 131 |
|
|
for _, size := range []int{0, 1, 200, 600, 999, 1000, 1023, 1024, 512 << 10, 1 << 20} {
|
| 132 |
|
|
_, err := Get(fmt.Sprintf("%s/?size=%d", ts.URL, size))
|
| 133 |
|
|
if err != nil {
|
| 134 |
|
|
t.Fatalf("size %d: %v", size, err)
|
| 135 |
|
|
}
|
| 136 |
|
|
}
|
| 137 |
|
|
}
|