github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/net/http/httptest/httptest.go (about)

     1  // Copyright 2016 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 httptest provides utilities for HTTP testing.
     6  package httptest
     7  
     8  import (
     9  	"bufio"
    10  	"bytes"
    11  	"crypto/tls"
    12  	"io"
    13  	"io/ioutil"
    14  	"net/http"
    15  	"strings"
    16  )
    17  
    18  // NewRequest returns a new incoming server Request, suitable
    19  // for passing to an http.Handler for testing.
    20  //
    21  // The target is the RFC 7230 "request-target": it may be either a
    22  // path or an absolute URL. If target is an absolute URL, the host name
    23  // from the URL is used. Otherwise, "example.com" is used.
    24  //
    25  // The TLS field is set to a non-nil dummy value if target has scheme
    26  // "https".
    27  //
    28  // The Request.Proto is always HTTP/1.1.
    29  //
    30  // An empty method means "GET".
    31  //
    32  // The provided body may be nil. If the body is of type *bytes.Reader,
    33  // *strings.Reader, or *bytes.Buffer, the Request.ContentLength is
    34  // set.
    35  //
    36  // NewRequest panics on error for ease of use in testing, where a
    37  // panic is acceptable.
    38  func NewRequest(method, target string, body io.Reader) *http.Request {
    39  	if method == "" {
    40  		method = "GET"
    41  	}
    42  	req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(method + " " + target + " HTTP/1.0\r\n\r\n")))
    43  	if err != nil {
    44  		panic("invalid NewRequest arguments; " + err.Error())
    45  	}
    46  
    47  	// HTTP/1.0 was used above to avoid needing a Host field. Change it to 1.1 here.
    48  	req.Proto = "HTTP/1.1"
    49  	req.ProtoMinor = 1
    50  	req.Close = false
    51  
    52  	if body != nil {
    53  		switch v := body.(type) {
    54  		case *bytes.Buffer:
    55  			req.ContentLength = int64(v.Len())
    56  		case *bytes.Reader:
    57  			req.ContentLength = int64(v.Len())
    58  		case *strings.Reader:
    59  			req.ContentLength = int64(v.Len())
    60  		default:
    61  			req.ContentLength = -1
    62  		}
    63  		if rc, ok := body.(io.ReadCloser); ok {
    64  			req.Body = rc
    65  		} else {
    66  			req.Body = ioutil.NopCloser(body)
    67  		}
    68  	}
    69  
    70  	// 192.0.2.0/24 is "TEST-NET" in RFC 5737 for use solely in
    71  	// documentation and example source code and should not be
    72  	// used publicly.
    73  	req.RemoteAddr = "192.0.2.1:1234"
    74  
    75  	if req.Host == "" {
    76  		req.Host = "example.com"
    77  	}
    78  
    79  	if strings.HasPrefix(target, "https://") {
    80  		req.TLS = &tls.ConnectionState{
    81  			Version:           tls.VersionTLS12,
    82  			HandshakeComplete: true,
    83  			ServerName:        req.Host,
    84  		}
    85  	}
    86  
    87  	return req
    88  }