| 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 |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"crypto/tls" |
| 10 |
"io" |
| 11 |
"net/http" |
| 12 |
"net/url" |
| 13 |
"reflect" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
) |
| 17 |
|
| 18 |
func TestNewRequest(t *testing.T) { |
| 19 |
got := NewRequest("GET", "/", nil) |
| 20 |
want := &http.Request{ |
| 21 |
Method: "GET", |
| 22 |
Host: "example.com", |
| 23 |
URL: &url.URL{Path: "/"}, |
| 24 |
Header: http.Header{}, |
| 25 |
Proto: "HTTP/1.1", |
| 26 |
ProtoMajor: 1, |
| 27 |
ProtoMinor: 1, |
| 28 |
RemoteAddr: "192.0.2.1:1234", |
| 29 |
RequestURI: "/", |
| 30 |
} |
| 31 |
got.Body = nil // before DeepEqual |
| 32 |
want = want.WithContext(context.Background()) |
| 33 |
if !reflect.DeepEqual(got, want) { |
| 34 |
t.Errorf("Request mismatch:\n got: %#v\nwant: %#v", got, want) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func TestNewRequestWithContext(t *testing.T) { |
| 39 |
for _, tt := range [...]struct { |
| 40 |
name string |
| 41 |
|
| 42 |
method, uri string |
| 43 |
body io.Reader |
| 44 |
|
| 45 |
want *http.Request |
| 46 |
wantBody string |
| 47 |
}{ |
| 48 |
{ |
| 49 |
name: "Empty method means GET", |
| 50 |
method: "", |
| 51 |
uri: "/", |
| 52 |
body: nil, |
| 53 |
want: &http.Request{ |
| 54 |
Method: "GET", |
| 55 |
Host: "example.com", |
| 56 |
URL: &url.URL{Path: "/"}, |
| 57 |
Header: http.Header{}, |
| 58 |
Proto: "HTTP/1.1", |
| 59 |
ProtoMajor: 1, |
| 60 |
ProtoMinor: 1, |
| 61 |
RemoteAddr: "192.0.2.1:1234", |
| 62 |
RequestURI: "/", |
| 63 |
}, |
| 64 |
wantBody: "", |
| 65 |
}, |
| 66 |
|
| 67 |
{ |
| 68 |
name: "GET with full URL", |
| 69 |
method: "GET", |
| 70 |
uri: "http://foo.com/path/%2f/bar/", |
| 71 |
body: nil, |
| 72 |
want: &http.Request{ |
| 73 |
Method: "GET", |
| 74 |
Host: "foo.com", |
| 75 |
URL: &url.URL{ |
| 76 |
Scheme: "http", |
| 77 |
Path: "/path///bar/", |
| 78 |
RawPath: "/path/%2f/bar/", |
| 79 |
Host: "foo.com", |
| 80 |
}, |
| 81 |
Header: http.Header{}, |
| 82 |
Proto: "HTTP/1.1", |
| 83 |
ProtoMajor: 1, |
| 84 |
ProtoMinor: 1, |
| 85 |
RemoteAddr: "192.0.2.1:1234", |
| 86 |
RequestURI: "http://foo.com/path/%2f/bar/", |
| 87 |
}, |
| 88 |
wantBody: "", |
| 89 |
}, |
| 90 |
|
| 91 |
{ |
| 92 |
name: "GET with full https URL", |
| 93 |
method: "GET", |
| 94 |
uri: "https://foo.com/path/", |
| 95 |
body: nil, |
| 96 |
want: &http.Request{ |
| 97 |
Method: "GET", |
| 98 |
Host: "foo.com", |
| 99 |
URL: &url.URL{ |
| 100 |
Scheme: "https", |
| 101 |
Path: "/path/", |
| 102 |
Host: "foo.com", |
| 103 |
}, |
| 104 |
Header: http.Header{}, |
| 105 |
Proto: "HTTP/1.1", |
| 106 |
ProtoMajor: 1, |
| 107 |
ProtoMinor: 1, |
| 108 |
RemoteAddr: "192.0.2.1:1234", |
| 109 |
RequestURI: "https://foo.com/path/", |
| 110 |
TLS: &tls.ConnectionState{ |
| 111 |
Version: tls.VersionTLS12, |
| 112 |
HandshakeComplete: true, |
| 113 |
ServerName: "foo.com", |
| 114 |
}, |
| 115 |
}, |
| 116 |
wantBody: "", |
| 117 |
}, |
| 118 |
|
| 119 |
{ |
| 120 |
name: "Post with known length", |
| 121 |
method: "POST", |
| 122 |
uri: "/", |
| 123 |
body: strings.NewReader("foo"), |
| 124 |
want: &http.Request{ |
| 125 |
Method: "POST", |
| 126 |
Host: "example.com", |
| 127 |
URL: &url.URL{Path: "/"}, |
| 128 |
Header: http.Header{}, |
| 129 |
Proto: "HTTP/1.1", |
| 130 |
ContentLength: 3, |
| 131 |
ProtoMajor: 1, |
| 132 |
ProtoMinor: 1, |
| 133 |
RemoteAddr: "192.0.2.1:1234", |
| 134 |
RequestURI: "/", |
| 135 |
}, |
| 136 |
wantBody: "foo", |
| 137 |
}, |
| 138 |
|
| 139 |
{ |
| 140 |
name: "Post with unknown length", |
| 141 |
method: "POST", |
| 142 |
uri: "/", |
| 143 |
body: struct{ io.Reader }{strings.NewReader("foo")}, |
| 144 |
want: &http.Request{ |
| 145 |
Method: "POST", |
| 146 |
Host: "example.com", |
| 147 |
URL: &url.URL{Path: "/"}, |
| 148 |
Header: http.Header{}, |
| 149 |
Proto: "HTTP/1.1", |
| 150 |
ContentLength: -1, |
| 151 |
ProtoMajor: 1, |
| 152 |
ProtoMinor: 1, |
| 153 |
RemoteAddr: "192.0.2.1:1234", |
| 154 |
RequestURI: "/", |
| 155 |
}, |
| 156 |
wantBody: "foo", |
| 157 |
}, |
| 158 |
|
| 159 |
{ |
| 160 |
name: "Post with NoBody", |
| 161 |
method: "POST", |
| 162 |
uri: "/", |
| 163 |
body: http.NoBody, |
| 164 |
want: &http.Request{ |
| 165 |
Method: "POST", |
| 166 |
Host: "example.com", |
| 167 |
URL: &url.URL{Path: "/"}, |
| 168 |
Header: http.Header{}, |
| 169 |
Proto: "HTTP/1.1", |
| 170 |
ProtoMajor: 1, |
| 171 |
ProtoMinor: 1, |
| 172 |
RemoteAddr: "192.0.2.1:1234", |
| 173 |
RequestURI: "/", |
| 174 |
}, |
| 175 |
}, |
| 176 |
|
| 177 |
{ |
| 178 |
name: "OPTIONS *", |
| 179 |
method: "OPTIONS", |
| 180 |
uri: "*", |
| 181 |
want: &http.Request{ |
| 182 |
Method: "OPTIONS", |
| 183 |
Host: "example.com", |
| 184 |
URL: &url.URL{Path: "*"}, |
| 185 |
Header: http.Header{}, |
| 186 |
Proto: "HTTP/1.1", |
| 187 |
ProtoMajor: 1, |
| 188 |
ProtoMinor: 1, |
| 189 |
RemoteAddr: "192.0.2.1:1234", |
| 190 |
RequestURI: "*", |
| 191 |
}, |
| 192 |
}, |
| 193 |
} { |
| 194 |
t.Run(tt.name, func(t *testing.T) { |
| 195 |
got := NewRequestWithContext(context.Background(), tt.method, tt.uri, tt.body) |
| 196 |
slurp, err := io.ReadAll(got.Body) |
| 197 |
if err != nil { |
| 198 |
t.Errorf("ReadAll: %v", err) |
| 199 |
} |
| 200 |
if string(slurp) != tt.wantBody { |
| 201 |
t.Errorf("Body = %q; want %q", slurp, tt.wantBody) |
| 202 |
} |
| 203 |
tt.want = tt.want.WithContext(context.Background()) |
| 204 |
got.Body = nil // before DeepEqual |
| 205 |
if !reflect.DeepEqual(got.URL, tt.want.URL) { |
| 206 |
t.Errorf("Request.URL mismatch:\n got: %#v\nwant: %#v", got.URL, tt.want.URL) |
| 207 |
} |
| 208 |
if !reflect.DeepEqual(got.Header, tt.want.Header) { |
| 209 |
t.Errorf("Request.Header mismatch:\n got: %#v\nwant: %#v", got.Header, tt.want.Header) |
| 210 |
} |
| 211 |
if !reflect.DeepEqual(got.TLS, tt.want.TLS) { |
| 212 |
t.Errorf("Request.TLS mismatch:\n got: %#v\nwant: %#v", got.TLS, tt.want.TLS) |
| 213 |
} |
| 214 |
if !reflect.DeepEqual(got, tt.want) { |
| 215 |
t.Errorf("Request mismatch:\n got: %#v\nwant: %#v", got, tt.want) |
| 216 |
} |
| 217 |
}) |
| 218 |
} |
| 219 |
} |