github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/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, h1Mode) }
    55  func TestServerContentType_h2(t *testing.T) { testServerContentType(t, h2Mode) }
    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, h1Mode) }
    91  func TestServerIssue5953_h2(t *testing.T) { testServerIssue5953(t, h2Mode) }
    92  func testServerIssue5953(t *testing.T, h2 bool) {
    93  	defer afterTest(t)
    94  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
    95  		w.Header()["Content-Type"] = []string{""}
    96  		fmt.Fprintf(w, "<html><head></head><body>hi</body></html>")
    97  	}))
    98  	defer cst.close()
    99  
   100  	resp, err := cst.c.Get(cst.ts.URL)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	got := resp.Header["Content-Type"]
   106  	want := []string{""}
   107  	if !reflect.DeepEqual(got, want) {
   108  		t.Errorf("Content-Type = %q; want %q", got, want)
   109  	}
   110  	resp.Body.Close()
   111  }
   112  
   113  func TestContentTypeWithCopy_h1(t *testing.T) { testContentTypeWithCopy(t, h1Mode) }
   114  func TestContentTypeWithCopy_h2(t *testing.T) { testContentTypeWithCopy(t, h2Mode) }
   115  func testContentTypeWithCopy(t *testing.T, h2 bool) {
   116  	defer afterTest(t)
   117  
   118  	const (
   119  		input    = "\n<html>\n\t<head>\n"
   120  		expected = "text/html; charset=utf-8"
   121  	)
   122  
   123  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   124  		// Use io.Copy from a bytes.Buffer to trigger ReadFrom.
   125  		buf := bytes.NewBuffer([]byte(input))
   126  		n, err := io.Copy(w, buf)
   127  		if int(n) != len(input) || err != nil {
   128  			t.Errorf("io.Copy(w, %q) = %v, %v want %d, nil", input, n, err, len(input))
   129  		}
   130  	}))
   131  	defer cst.close()
   132  
   133  	resp, err := cst.c.Get(cst.ts.URL)
   134  	if err != nil {
   135  		t.Fatalf("Get: %v", err)
   136  	}
   137  	if ct := resp.Header.Get("Content-Type"); ct != expected {
   138  		t.Errorf("Content-Type = %q, want %q", ct, expected)
   139  	}
   140  	data, err := ioutil.ReadAll(resp.Body)
   141  	if err != nil {
   142  		t.Errorf("reading body: %v", err)
   143  	} else if !bytes.Equal(data, []byte(input)) {
   144  		t.Errorf("data is %q, want %q", data, input)
   145  	}
   146  	resp.Body.Close()
   147  }
   148  
   149  func TestSniffWriteSize_h1(t *testing.T) { testSniffWriteSize(t, h1Mode) }
   150  func TestSniffWriteSize_h2(t *testing.T) { testSniffWriteSize(t, h2Mode) }
   151  func testSniffWriteSize(t *testing.T, h2 bool) {
   152  	defer afterTest(t)
   153  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   154  		size, _ := strconv.Atoi(r.FormValue("size"))
   155  		written, err := io.WriteString(w, strings.Repeat("a", size))
   156  		if err != nil {
   157  			t.Errorf("write of %d bytes: %v", size, err)
   158  			return
   159  		}
   160  		if written != size {
   161  			t.Errorf("write of %d bytes wrote %d bytes", size, written)
   162  		}
   163  	}))
   164  	defer cst.close()
   165  	for _, size := range []int{0, 1, 200, 600, 999, 1000, 1023, 1024, 512 << 10, 1 << 20} {
   166  		res, err := cst.c.Get(fmt.Sprintf("%s/?size=%d", cst.ts.URL, size))
   167  		if err != nil {
   168  			t.Fatalf("size %d: %v", size, err)
   169  		}
   170  		if _, err := io.Copy(ioutil.Discard, res.Body); err != nil {
   171  			t.Fatalf("size %d: io.Copy of body = %v", size, err)
   172  		}
   173  		if err := res.Body.Close(); err != nil {
   174  			t.Fatalf("size %d: body Close = %v", size, err)
   175  		}
   176  	}
   177  }