github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/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  	"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(`<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  	// 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  	defer afterTest(t)
    58  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
    59  		i, _ := strconv.Atoi(r.FormValue("i"))
    60  		tt := sniffTests[i]
    61  		n, err := w.Write(tt.data)
    62  		if n != len(tt.data) || err != nil {
    63  			log.Fatalf("%v: Write(%q) = %v, %v want %d, nil", tt.desc, tt.data, n, err, len(tt.data))
    64  		}
    65  	}))
    66  	defer ts.Close()
    67  
    68  	for i, tt := range sniffTests {
    69  		resp, err := Get(ts.URL + "/?i=" + strconv.Itoa(i))
    70  		if err != nil {
    71  			t.Errorf("%v: %v", tt.desc, err)
    72  			continue
    73  		}
    74  		if ct := resp.Header.Get("Content-Type"); ct != tt.contentType {
    75  			t.Errorf("%v: Content-Type = %q, want %q", tt.desc, ct, tt.contentType)
    76  		}
    77  		data, err := ioutil.ReadAll(resp.Body)
    78  		if err != nil {
    79  			t.Errorf("%v: reading body: %v", tt.desc, err)
    80  		} else if !bytes.Equal(data, tt.data) {
    81  			t.Errorf("%v: data is %q, want %q", tt.desc, data, tt.data)
    82  		}
    83  		resp.Body.Close()
    84  	}
    85  }
    86  
    87  func TestContentTypeWithCopy(t *testing.T) {
    88  	defer afterTest(t)
    89  
    90  	const (
    91  		input    = "\n<html>\n\t<head>\n"
    92  		expected = "text/html; charset=utf-8"
    93  	)
    94  
    95  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
    96  		// Use io.Copy from a bytes.Buffer to trigger ReadFrom.
    97  		buf := bytes.NewBuffer([]byte(input))
    98  		n, err := io.Copy(w, buf)
    99  		if int(n) != len(input) || err != nil {
   100  			t.Errorf("io.Copy(w, %q) = %v, %v want %d, nil", input, n, err, len(input))
   101  		}
   102  	}))
   103  	defer ts.Close()
   104  
   105  	resp, err := Get(ts.URL)
   106  	if err != nil {
   107  		t.Fatalf("Get: %v", err)
   108  	}
   109  	if ct := resp.Header.Get("Content-Type"); ct != expected {
   110  		t.Errorf("Content-Type = %q, want %q", ct, expected)
   111  	}
   112  	data, err := ioutil.ReadAll(resp.Body)
   113  	if err != nil {
   114  		t.Errorf("reading body: %v", err)
   115  	} else if !bytes.Equal(data, []byte(input)) {
   116  		t.Errorf("data is %q, want %q", data, input)
   117  	}
   118  	resp.Body.Close()
   119  }
   120  
   121  func TestSniffWriteSize(t *testing.T) {
   122  	defer afterTest(t)
   123  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
   124  		size, _ := strconv.Atoi(r.FormValue("size"))
   125  		written, err := io.WriteString(w, strings.Repeat("a", size))
   126  		if err != nil {
   127  			t.Errorf("write of %d bytes: %v", size, err)
   128  			return
   129  		}
   130  		if written != size {
   131  			t.Errorf("write of %d bytes wrote %d bytes", size, written)
   132  		}
   133  	}))
   134  	defer ts.Close()
   135  	for _, size := range []int{0, 1, 200, 600, 999, 1000, 1023, 1024, 512 << 10, 1 << 20} {
   136  		res, err := Get(fmt.Sprintf("%s/?size=%d", ts.URL, size))
   137  		if err != nil {
   138  			t.Fatalf("size %d: %v", size, err)
   139  		}
   140  		if _, err := io.Copy(ioutil.Discard, res.Body); err != nil {
   141  			t.Fatalf("size %d: io.Copy of body = %v", size, err)
   142  		}
   143  		if err := res.Body.Close(); err != nil {
   144  			t.Fatalf("size %d: body Close = %v", size, err)
   145  		}
   146  	}
   147  }