github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/net/http/responsewrite_test.go (about)

     1  // Copyright 2010 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
     6  
     7  import (
     8  	"bytes"
     9  	"io/ioutil"
    10  	"testing"
    11  )
    12  
    13  type respWriteTest struct {
    14  	Resp Response
    15  	Raw  string
    16  }
    17  
    18  func TestResponseWrite(t *testing.T) {
    19  	respWriteTests := []respWriteTest{
    20  		// HTTP/1.0, identity coding; no trailer
    21  		{
    22  			Response{
    23  				StatusCode:    503,
    24  				ProtoMajor:    1,
    25  				ProtoMinor:    0,
    26  				Request:       dummyReq("GET"),
    27  				Header:        Header{},
    28  				Body:          ioutil.NopCloser(bytes.NewBufferString("abcdef")),
    29  				ContentLength: 6,
    30  			},
    31  
    32  			"HTTP/1.0 503 Service Unavailable\r\n" +
    33  				"Content-Length: 6\r\n\r\n" +
    34  				"abcdef",
    35  		},
    36  		// Unchunked response without Content-Length.
    37  		{
    38  			Response{
    39  				StatusCode:    200,
    40  				ProtoMajor:    1,
    41  				ProtoMinor:    0,
    42  				Request:       dummyReq("GET"),
    43  				Header:        Header{},
    44  				Body:          ioutil.NopCloser(bytes.NewBufferString("abcdef")),
    45  				ContentLength: -1,
    46  			},
    47  			"HTTP/1.0 200 OK\r\n" +
    48  				"\r\n" +
    49  				"abcdef",
    50  		},
    51  		// HTTP/1.1, chunked coding; empty trailer; close
    52  		{
    53  			Response{
    54  				StatusCode:       200,
    55  				ProtoMajor:       1,
    56  				ProtoMinor:       1,
    57  				Request:          dummyReq("GET"),
    58  				Header:           Header{},
    59  				Body:             ioutil.NopCloser(bytes.NewBufferString("abcdef")),
    60  				ContentLength:    6,
    61  				TransferEncoding: []string{"chunked"},
    62  				Close:            true,
    63  			},
    64  
    65  			"HTTP/1.1 200 OK\r\n" +
    66  				"Connection: close\r\n" +
    67  				"Transfer-Encoding: chunked\r\n\r\n" +
    68  				"6\r\nabcdef\r\n0\r\n\r\n",
    69  		},
    70  
    71  		// Header value with a newline character (Issue 914).
    72  		// Also tests removal of leading and trailing whitespace.
    73  		{
    74  			Response{
    75  				StatusCode: 204,
    76  				ProtoMajor: 1,
    77  				ProtoMinor: 1,
    78  				Request:    dummyReq("GET"),
    79  				Header: Header{
    80  					"Foo": []string{" Bar\nBaz "},
    81  				},
    82  				Body:             nil,
    83  				ContentLength:    0,
    84  				TransferEncoding: []string{"chunked"},
    85  				Close:            true,
    86  			},
    87  
    88  			"HTTP/1.1 204 No Content\r\n" +
    89  				"Connection: close\r\n" +
    90  				"Foo: Bar Baz\r\n" +
    91  				"\r\n",
    92  		},
    93  	}
    94  
    95  	for i := range respWriteTests {
    96  		tt := &respWriteTests[i]
    97  		var braw bytes.Buffer
    98  		err := tt.Resp.Write(&braw)
    99  		if err != nil {
   100  			t.Errorf("error writing #%d: %s", i, err)
   101  			continue
   102  		}
   103  		sraw := braw.String()
   104  		if sraw != tt.Raw {
   105  			t.Errorf("Test %d, expecting:\n%q\nGot:\n%q\n", i, tt.Raw, sraw)
   106  			continue
   107  		}
   108  	}
   109  }