github.com/fjballest/golang@v0.0.0-20151209143359-e4c5fe594ca8/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 {"MP4 video", []byte("\x00\x00\x00\x18ftypmp42\x00\x00\x00\x00mp42isom<\x06t\xbfmdat"), "video/mp4"}, 43 } 44 45 func TestDetectContentType(t *testing.T) { 46 for _, tt := range sniffTests { 47 ct := DetectContentType(tt.data) 48 if ct != tt.contentType { 49 t.Errorf("%v: DetectContentType = %q, want %q", tt.desc, ct, tt.contentType) 50 } 51 } 52 } 53 54 func TestServerContentType_h1(t *testing.T) { testServerContentType(t, false) } 55 func TestServerContentType_h2(t *testing.T) { testServerContentType(t, true) } 56 57 func testServerContentType(t *testing.T, h2 bool) { 58 defer afterTest(t) 59 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 60 i, _ := strconv.Atoi(r.FormValue("i")) 61 tt := sniffTests[i] 62 n, err := w.Write(tt.data) 63 if n != len(tt.data) || err != nil { 64 log.Fatalf("%v: Write(%q) = %v, %v want %d, nil", tt.desc, tt.data, n, err, len(tt.data)) 65 } 66 })) 67 defer cst.close() 68 69 for i, tt := range sniffTests { 70 resp, err := cst.c.Get(cst.ts.URL + "/?i=" + strconv.Itoa(i)) 71 if err != nil { 72 t.Errorf("%v: %v", tt.desc, err) 73 continue 74 } 75 if ct := resp.Header.Get("Content-Type"); ct != tt.contentType { 76 t.Errorf("%v: Content-Type = %q, want %q", tt.desc, ct, tt.contentType) 77 } 78 data, err := ioutil.ReadAll(resp.Body) 79 if err != nil { 80 t.Errorf("%v: reading body: %v", tt.desc, err) 81 } else if !bytes.Equal(data, tt.data) { 82 t.Errorf("%v: data is %q, want %q", tt.desc, data, tt.data) 83 } 84 resp.Body.Close() 85 } 86 } 87 88 // Issue 5953: shouldn't sniff if the handler set a Content-Type header, 89 // even if it's the empty string. 90 func TestServerIssue5953_h1(t *testing.T) { testServerIssue5953(t, false) } 91 func TestServerIssue5953_h2(t *testing.T) { testServerIssue5953(t, true) } 92 93 func testServerIssue5953(t *testing.T, h2 bool) { 94 defer afterTest(t) 95 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 96 w.Header()["Content-Type"] = []string{""} 97 fmt.Fprintf(w, "<html><head></head><body>hi</body></html>") 98 })) 99 defer cst.close() 100 101 resp, err := cst.c.Get(cst.ts.URL) 102 if err != nil { 103 t.Fatal(err) 104 } 105 106 got := resp.Header["Content-Type"] 107 want := []string{""} 108 if !reflect.DeepEqual(got, want) { 109 t.Errorf("Content-Type = %q; want %q", got, want) 110 } 111 resp.Body.Close() 112 } 113 114 func TestContentTypeWithCopy_h1(t *testing.T) { testContentTypeWithCopy(t, false) } 115 func TestContentTypeWithCopy_h2(t *testing.T) { testContentTypeWithCopy(t, true) } 116 117 func testContentTypeWithCopy(t *testing.T, h2 bool) { 118 defer afterTest(t) 119 120 const ( 121 input = "\n<html>\n\t<head>\n" 122 expected = "text/html; charset=utf-8" 123 ) 124 125 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 126 // Use io.Copy from a bytes.Buffer to trigger ReadFrom. 127 buf := bytes.NewBuffer([]byte(input)) 128 n, err := io.Copy(w, buf) 129 if int(n) != len(input) || err != nil { 130 t.Errorf("io.Copy(w, %q) = %v, %v want %d, nil", input, n, err, len(input)) 131 } 132 })) 133 defer cst.close() 134 135 resp, err := cst.c.Get(cst.ts.URL) 136 if err != nil { 137 t.Fatalf("Get: %v", err) 138 } 139 if ct := resp.Header.Get("Content-Type"); ct != expected { 140 t.Errorf("Content-Type = %q, want %q", ct, expected) 141 } 142 data, err := ioutil.ReadAll(resp.Body) 143 if err != nil { 144 t.Errorf("reading body: %v", err) 145 } else if !bytes.Equal(data, []byte(input)) { 146 t.Errorf("data is %q, want %q", data, input) 147 } 148 resp.Body.Close() 149 } 150 151 func TestSniffWriteSize_h1(t *testing.T) { testSniffWriteSize(t, false) } 152 func TestSniffWriteSize_h2(t *testing.T) { testSniffWriteSize(t, true) } 153 154 func testSniffWriteSize(t *testing.T, h2 bool) { 155 defer afterTest(t) 156 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 157 size, _ := strconv.Atoi(r.FormValue("size")) 158 written, err := io.WriteString(w, strings.Repeat("a", size)) 159 if err != nil { 160 t.Errorf("write of %d bytes: %v", size, err) 161 return 162 } 163 if written != size { 164 t.Errorf("write of %d bytes wrote %d bytes", size, written) 165 } 166 })) 167 defer cst.close() 168 for _, size := range []int{0, 1, 200, 600, 999, 1000, 1023, 1024, 512 << 10, 1 << 20} { 169 res, err := cst.c.Get(fmt.Sprintf("%s/?size=%d", cst.ts.URL, size)) 170 if err != nil { 171 t.Fatalf("size %d: %v", size, err) 172 } 173 if _, err := io.Copy(ioutil.Discard, res.Body); err != nil { 174 t.Fatalf("size %d: io.Copy of body = %v", size, err) 175 } 176 if err := res.Body.Close(); err != nil { 177 t.Fatalf("size %d: body Close = %v", size, err) 178 } 179 } 180 }