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