github.com/euank/go@v0.0.0-20160829210321-495514729181/src/net/http/sniff_test.go (about) 1 // 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 "reflect" 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(`<HtMl><bOdY>blah blah blah</body></html>`), "text/html; charset=utf-8"}, 30 {"HTML document #2", []byte(`<HTML></HTML>`), "text/html; charset=utf-8"}, 31 {"HTML document #3 (leading whitespace)", []byte(` <!DOCTYPE HTML>...`), "text/html; charset=utf-8"}, 32 {"HTML document #4 (leading CRLF)", []byte("\r\n<html>..."), "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<?xml!"), "text/xml; charset=utf-8"}, 37 38 // Image types. 39 {"GIF 87a", []byte(`GIF87a`), "image/gif"}, 40 {"GIF 89a", []byte(`GIF89a...`), "image/gif"}, 41 42 // Audio types. 43 {"MIDI audio", []byte("MThd\x00\x00\x00\x06\x00\x01"), "audio/midi"}, 44 {"MP3 audio/MPEG audio", []byte("ID3\x03\x00\x00\x00\x00\x0f"), "audio/mpeg"}, 45 {"WAV audio #1", []byte("RIFFb\xb8\x00\x00WAVEfmt \x12\x00\x00\x00\x06"), "audio/wave"}, 46 {"WAV audio #2", []byte("RIFF,\x00\x00\x00WAVEfmt \x12\x00\x00\x00\x06"), "audio/wave"}, 47 {"AIFF audio #1", []byte("FORM\x00\x00\x00\x00AIFFCOMM\x00\x00\x00\x12\x00\x01\x00\x00\x57\x55\x00\x10\x40\x0d\xf3\x34"), "audio/aiff"}, 48 {"OGG audio", []byte("OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x7e\x46\x00\x00\x00\x00\x00\x00\x1f\xf6\xb4\xfc\x01\x1e\x01\x76\x6f\x72"), "application/ogg"}, 49 50 // Video types. 51 {"MP4 video", []byte("\x00\x00\x00\x18ftypmp42\x00\x00\x00\x00mp42isom<\x06t\xbfmdat"), "video/mp4"}, 52 {"AVI video #1", []byte("RIFF,O\n\x00AVI LISTÀ"), "video/avi"}, 53 {"AVI video #2", []byte("RIFF,\n\x00\x00AVI LISTÀ"), "video/avi"}, 54 } 55 56 func TestDetectContentType(t *testing.T) { 57 for _, tt := range sniffTests { 58 ct := DetectContentType(tt.data) 59 if ct != tt.contentType { 60 t.Errorf("%v: DetectContentType = %q, want %q", tt.desc, ct, tt.contentType) 61 } 62 } 63 } 64 65 func TestServerContentType_h1(t *testing.T) { testServerContentType(t, h1Mode) } 66 func TestServerContentType_h2(t *testing.T) { testServerContentType(t, h2Mode) } 67 68 func testServerContentType(t *testing.T, h2 bool) { 69 defer afterTest(t) 70 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 71 i, _ := strconv.Atoi(r.FormValue("i")) 72 tt := sniffTests[i] 73 n, err := w.Write(tt.data) 74 if n != len(tt.data) || err != nil { 75 log.Fatalf("%v: Write(%q) = %v, %v want %d, nil", tt.desc, tt.data, n, err, len(tt.data)) 76 } 77 })) 78 defer cst.close() 79 80 for i, tt := range sniffTests { 81 resp, err := cst.c.Get(cst.ts.URL + "/?i=" + strconv.Itoa(i)) 82 if err != nil { 83 t.Errorf("%v: %v", tt.desc, err) 84 continue 85 } 86 if ct := resp.Header.Get("Content-Type"); ct != tt.contentType { 87 t.Errorf("%v: Content-Type = %q, want %q", tt.desc, ct, tt.contentType) 88 } 89 data, err := ioutil.ReadAll(resp.Body) 90 if err != nil { 91 t.Errorf("%v: reading body: %v", tt.desc, err) 92 } else if !bytes.Equal(data, tt.data) { 93 t.Errorf("%v: data is %q, want %q", tt.desc, data, tt.data) 94 } 95 resp.Body.Close() 96 } 97 } 98 99 // Issue 5953: shouldn't sniff if the handler set a Content-Type header, 100 // even if it's the empty string. 101 func TestServerIssue5953_h1(t *testing.T) { testServerIssue5953(t, h1Mode) } 102 func TestServerIssue5953_h2(t *testing.T) { testServerIssue5953(t, h2Mode) } 103 func testServerIssue5953(t *testing.T, h2 bool) { 104 defer afterTest(t) 105 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 106 w.Header()["Content-Type"] = []string{""} 107 fmt.Fprintf(w, "<html><head></head><body>hi</body></html>") 108 })) 109 defer cst.close() 110 111 resp, err := cst.c.Get(cst.ts.URL) 112 if err != nil { 113 t.Fatal(err) 114 } 115 116 got := resp.Header["Content-Type"] 117 want := []string{""} 118 if !reflect.DeepEqual(got, want) { 119 t.Errorf("Content-Type = %q; want %q", got, want) 120 } 121 resp.Body.Close() 122 } 123 124 func TestContentTypeWithCopy_h1(t *testing.T) { testContentTypeWithCopy(t, h1Mode) } 125 func TestContentTypeWithCopy_h2(t *testing.T) { testContentTypeWithCopy(t, h2Mode) } 126 func testContentTypeWithCopy(t *testing.T, h2 bool) { 127 defer afterTest(t) 128 129 const ( 130 input = "\n<html>\n\t<head>\n" 131 expected = "text/html; charset=utf-8" 132 ) 133 134 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 135 // Use io.Copy from a bytes.Buffer to trigger ReadFrom. 136 buf := bytes.NewBuffer([]byte(input)) 137 n, err := io.Copy(w, buf) 138 if int(n) != len(input) || err != nil { 139 t.Errorf("io.Copy(w, %q) = %v, %v want %d, nil", input, n, err, len(input)) 140 } 141 })) 142 defer cst.close() 143 144 resp, err := cst.c.Get(cst.ts.URL) 145 if err != nil { 146 t.Fatalf("Get: %v", err) 147 } 148 if ct := resp.Header.Get("Content-Type"); ct != expected { 149 t.Errorf("Content-Type = %q, want %q", ct, expected) 150 } 151 data, err := ioutil.ReadAll(resp.Body) 152 if err != nil { 153 t.Errorf("reading body: %v", err) 154 } else if !bytes.Equal(data, []byte(input)) { 155 t.Errorf("data is %q, want %q", data, input) 156 } 157 resp.Body.Close() 158 } 159 160 func TestSniffWriteSize_h1(t *testing.T) { testSniffWriteSize(t, h1Mode) } 161 func TestSniffWriteSize_h2(t *testing.T) { testSniffWriteSize(t, h2Mode) } 162 func testSniffWriteSize(t *testing.T, h2 bool) { 163 defer afterTest(t) 164 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 165 size, _ := strconv.Atoi(r.FormValue("size")) 166 written, err := io.WriteString(w, strings.Repeat("a", size)) 167 if err != nil { 168 t.Errorf("write of %d bytes: %v", size, err) 169 return 170 } 171 if written != size { 172 t.Errorf("write of %d bytes wrote %d bytes", size, written) 173 } 174 })) 175 defer cst.close() 176 for _, size := range []int{0, 1, 200, 600, 999, 1000, 1023, 1024, 512 << 10, 1 << 20} { 177 res, err := cst.c.Get(fmt.Sprintf("%s/?size=%d", cst.ts.URL, size)) 178 if err != nil { 179 t.Fatalf("size %d: %v", size, err) 180 } 181 if _, err := io.Copy(ioutil.Discard, res.Body); err != nil { 182 t.Fatalf("size %d: io.Copy of body = %v", size, err) 183 } 184 if err := res.Body.Close(); err != nil { 185 t.Fatalf("size %d: body Close = %v", size, err) 186 } 187 } 188 }