...
Source file src/net/http/httptest/httptest.go
1
2
3
4
5
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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 func NewRequest(method, target string, body io.Reader) *http.Request {
42 if method == "" {
43 method = "GET"
44 }
45 req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(method + " " + target + " HTTP/1.0\r\n\r\n")))
46 if err != nil {
47 panic("invalid NewRequest arguments; " + err.Error())
48 }
49
50
51 req.Proto = "HTTP/1.1"
52 req.ProtoMinor = 1
53 req.Close = false
54
55 if body != nil {
56 switch v := body.(type) {
57 case *bytes.Buffer:
58 req.ContentLength = int64(v.Len())
59 case *bytes.Reader:
60 req.ContentLength = int64(v.Len())
61 case *strings.Reader:
62 req.ContentLength = int64(v.Len())
63 default:
64 req.ContentLength = -1
65 }
66 if rc, ok := body.(io.ReadCloser); ok {
67 req.Body = rc
68 } else {
69 req.Body = ioutil.NopCloser(body)
70 }
71 }
72
73
74
75
76 req.RemoteAddr = "192.0.2.1:1234"
77
78 if req.Host == "" {
79 req.Host = "example.com"
80 }
81
82 if strings.HasPrefix(target, "https://") {
83 req.TLS = &tls.ConnectionState{
84 Version: tls.VersionTLS12,
85 HandshakeComplete: true,
86 ServerName: req.Host,
87 }
88 }
89
90 return req
91 }
92
View as plain text