github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/net/http/cgi/integration_test.go (about) 1 // Copyright 2011 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 // Tests a Go CGI program running under a Go CGI host process. 6 // Further, the two programs are the same binary, just checking 7 // their environment to figure out what mode to run in. 8 9 package cgi 10 11 import ( 12 "bytes" 13 "errors" 14 "fmt" 15 "internal/testenv" 16 "io" 17 "net/http" 18 "net/http/httptest" 19 "net/url" 20 "os" 21 "strings" 22 "testing" 23 "time" 24 ) 25 26 // This test is a CGI host (testing host.go) that runs its own binary 27 // as a child process testing the other half of CGI (child.go). 28 func TestHostingOurselves(t *testing.T) { 29 testenv.MustHaveExec(t) 30 31 h := &Handler{ 32 Path: os.Args[0], 33 Root: "/test.go", 34 Args: []string{"-test.run=TestBeChildCGIProcess"}, 35 } 36 expectedMap := map[string]string{ 37 "test": "Hello CGI-in-CGI", 38 "param-a": "b", 39 "param-foo": "bar", 40 "env-GATEWAY_INTERFACE": "CGI/1.1", 41 "env-HTTP_HOST": "example.com", 42 "env-PATH_INFO": "", 43 "env-QUERY_STRING": "foo=bar&a=b", 44 "env-REMOTE_ADDR": "1.2.3.4", 45 "env-REMOTE_HOST": "1.2.3.4", 46 "env-REMOTE_PORT": "1234", 47 "env-REQUEST_METHOD": "GET", 48 "env-REQUEST_URI": "/test.go?foo=bar&a=b", 49 "env-SCRIPT_FILENAME": os.Args[0], 50 "env-SCRIPT_NAME": "/test.go", 51 "env-SERVER_NAME": "example.com", 52 "env-SERVER_PORT": "80", 53 "env-SERVER_SOFTWARE": "go", 54 } 55 replay := runCgiTest(t, h, "GET /test.go?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap) 56 57 if expected, got := "text/plain; charset=utf-8", replay.Header().Get("Content-Type"); got != expected { 58 t.Errorf("got a Content-Type of %q; expected %q", got, expected) 59 } 60 if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected { 61 t.Errorf("got a X-Test-Header of %q; expected %q", got, expected) 62 } 63 } 64 65 type customWriterRecorder struct { 66 w io.Writer 67 *httptest.ResponseRecorder 68 } 69 70 func (r *customWriterRecorder) Write(p []byte) (n int, err error) { 71 return r.w.Write(p) 72 } 73 74 type limitWriter struct { 75 w io.Writer 76 n int 77 } 78 79 func (w *limitWriter) Write(p []byte) (n int, err error) { 80 if len(p) > w.n { 81 p = p[:w.n] 82 } 83 if len(p) > 0 { 84 n, err = w.w.Write(p) 85 w.n -= n 86 } 87 if w.n == 0 { 88 err = errors.New("past write limit") 89 } 90 return 91 } 92 93 // If there's an error copying the child's output to the parent, test 94 // that we kill the child. 95 func TestKillChildAfterCopyError(t *testing.T) { 96 testenv.MustHaveExec(t) 97 98 defer func() { testHookStartProcess = nil }() 99 proc := make(chan *os.Process, 1) 100 testHookStartProcess = func(p *os.Process) { 101 proc <- p 102 } 103 104 h := &Handler{ 105 Path: os.Args[0], 106 Root: "/test.go", 107 Args: []string{"-test.run=TestBeChildCGIProcess"}, 108 } 109 req, _ := http.NewRequest("GET", "http://example.com/test.cgi?write-forever=1", nil) 110 rec := httptest.NewRecorder() 111 var out bytes.Buffer 112 const writeLen = 50 << 10 113 rw := &customWriterRecorder{&limitWriter{&out, writeLen}, rec} 114 115 donec := make(chan bool, 1) 116 go func() { 117 h.ServeHTTP(rw, req) 118 donec <- true 119 }() 120 121 select { 122 case <-donec: 123 if out.Len() != writeLen || out.Bytes()[0] != 'a' { 124 t.Errorf("unexpected output: %q", out.Bytes()) 125 } 126 case <-time.After(5 * time.Second): 127 t.Errorf("timeout. ServeHTTP hung and didn't kill the child process?") 128 select { 129 case p := <-proc: 130 p.Kill() 131 t.Logf("killed process") 132 default: 133 t.Logf("didn't kill process") 134 } 135 } 136 } 137 138 // Test that a child handler writing only headers works. 139 // golang.org/issue/7196 140 func TestChildOnlyHeaders(t *testing.T) { 141 testenv.MustHaveExec(t) 142 143 h := &Handler{ 144 Path: os.Args[0], 145 Root: "/test.go", 146 Args: []string{"-test.run=TestBeChildCGIProcess"}, 147 } 148 expectedMap := map[string]string{ 149 "_body": "", 150 } 151 replay := runCgiTest(t, h, "GET /test.go?no-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap) 152 if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected { 153 t.Errorf("got a X-Test-Header of %q; expected %q", got, expected) 154 } 155 } 156 157 // Test that a child handler does not receive a nil Request Body. 158 // golang.org/issue/39190 159 func TestNilRequestBody(t *testing.T) { 160 testenv.MustHaveExec(t) 161 162 h := &Handler{ 163 Path: os.Args[0], 164 Root: "/test.go", 165 Args: []string{"-test.run=TestBeChildCGIProcess"}, 166 } 167 expectedMap := map[string]string{ 168 "nil-request-body": "false", 169 } 170 _ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap) 171 _ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\nContent-Length: 0\n\n", expectedMap) 172 } 173 174 func TestChildContentType(t *testing.T) { 175 testenv.MustHaveExec(t) 176 177 h := &Handler{ 178 Path: os.Args[0], 179 Root: "/test.go", 180 Args: []string{"-test.run=TestBeChildCGIProcess"}, 181 } 182 var tests = []struct { 183 name string 184 body string 185 wantCT string 186 }{ 187 { 188 name: "no body", 189 wantCT: "text/plain; charset=utf-8", 190 }, 191 { 192 name: "html", 193 body: "<html><head><title>test page</title></head><body>This is a body</body></html>", 194 wantCT: "text/html; charset=utf-8", 195 }, 196 { 197 name: "text", 198 body: strings.Repeat("gopher", 86), 199 wantCT: "text/plain; charset=utf-8", 200 }, 201 { 202 name: "jpg", 203 body: "\xFF\xD8\xFF" + strings.Repeat("B", 1024), 204 wantCT: "image/jpeg", 205 }, 206 } 207 for _, tt := range tests { 208 t.Run(tt.name, func(t *testing.T) { 209 expectedMap := map[string]string{"_body": tt.body} 210 req := fmt.Sprintf("GET /test.go?exact-body=%s HTTP/1.0\nHost: example.com\n\n", url.QueryEscape(tt.body)) 211 replay := runCgiTest(t, h, req, expectedMap) 212 if got := replay.Header().Get("Content-Type"); got != tt.wantCT { 213 t.Errorf("got a Content-Type of %q; expected it to start with %q", got, tt.wantCT) 214 } 215 }) 216 } 217 } 218 219 // golang.org/issue/7198 220 func Test500WithNoHeaders(t *testing.T) { want500Test(t, "/immediate-disconnect") } 221 func Test500WithNoContentType(t *testing.T) { want500Test(t, "/no-content-type") } 222 func Test500WithEmptyHeaders(t *testing.T) { want500Test(t, "/empty-headers") } 223 224 func want500Test(t *testing.T, path string) { 225 h := &Handler{ 226 Path: os.Args[0], 227 Root: "/test.go", 228 Args: []string{"-test.run=TestBeChildCGIProcess"}, 229 } 230 expectedMap := map[string]string{ 231 "_body": "", 232 } 233 replay := runCgiTest(t, h, "GET "+path+" HTTP/1.0\nHost: example.com\n\n", expectedMap) 234 if replay.Code != 500 { 235 t.Errorf("Got code %d; want 500", replay.Code) 236 } 237 } 238 239 type neverEnding byte 240 241 func (b neverEnding) Read(p []byte) (n int, err error) { 242 for i := range p { 243 p[i] = byte(b) 244 } 245 return len(p), nil 246 } 247 248 // Note: not actually a test. 249 func TestBeChildCGIProcess(t *testing.T) { 250 if os.Getenv("REQUEST_METHOD") == "" { 251 // Not in a CGI environment; skipping test. 252 return 253 } 254 switch os.Getenv("REQUEST_URI") { 255 case "/immediate-disconnect": 256 os.Exit(0) 257 case "/no-content-type": 258 fmt.Printf("Content-Length: 6\n\nHello\n") 259 os.Exit(0) 260 case "/empty-headers": 261 fmt.Printf("\nHello") 262 os.Exit(0) 263 } 264 Serve(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { 265 if req.FormValue("nil-request-body") == "1" { 266 fmt.Fprintf(rw, "nil-request-body=%v\n", req.Body == nil) 267 return 268 } 269 rw.Header().Set("X-Test-Header", "X-Test-Value") 270 req.ParseForm() 271 if req.FormValue("no-body") == "1" { 272 return 273 } 274 if eb, ok := req.Form["exact-body"]; ok { 275 io.WriteString(rw, eb[0]) 276 return 277 } 278 if req.FormValue("write-forever") == "1" { 279 io.Copy(rw, neverEnding('a')) 280 for { 281 time.Sleep(5 * time.Second) // hang forever, until killed 282 } 283 } 284 fmt.Fprintf(rw, "test=Hello CGI-in-CGI\n") 285 for k, vv := range req.Form { 286 for _, v := range vv { 287 fmt.Fprintf(rw, "param-%s=%s\n", k, v) 288 } 289 } 290 for _, kv := range os.Environ() { 291 fmt.Fprintf(rw, "env-%s\n", kv) 292 } 293 })) 294 os.Exit(0) 295 }