rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/net/http/cgi/matryoshka_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 "io" 16 "net/http" 17 "net/http/httptest" 18 "os" 19 "runtime" 20 "testing" 21 "time" 22 ) 23 24 // iOS cannot fork, so we skip some tests 25 var iOS = runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") 26 27 // This test is a CGI host (testing host.go) that runs its own binary 28 // as a child process testing the other half of CGI (child.go). 29 func TestHostingOurselves(t *testing.T) { 30 if runtime.GOOS == "nacl" || iOS { 31 t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH) 32 } 33 34 h := &Handler{ 35 Path: os.Args[0], 36 Root: "/test.go", 37 Args: []string{"-test.run=TestBeChildCGIProcess"}, 38 } 39 expectedMap := map[string]string{ 40 "test": "Hello CGI-in-CGI", 41 "param-a": "b", 42 "param-foo": "bar", 43 "env-GATEWAY_INTERFACE": "CGI/1.1", 44 "env-HTTP_HOST": "example.com", 45 "env-PATH_INFO": "", 46 "env-QUERY_STRING": "foo=bar&a=b", 47 "env-REMOTE_ADDR": "1.2.3.4", 48 "env-REMOTE_HOST": "1.2.3.4", 49 "env-REMOTE_PORT": "1234", 50 "env-REQUEST_METHOD": "GET", 51 "env-REQUEST_URI": "/test.go?foo=bar&a=b", 52 "env-SCRIPT_FILENAME": os.Args[0], 53 "env-SCRIPT_NAME": "/test.go", 54 "env-SERVER_NAME": "example.com", 55 "env-SERVER_PORT": "80", 56 "env-SERVER_SOFTWARE": "go", 57 } 58 replay := runCgiTest(t, h, "GET /test.go?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap) 59 60 if expected, got := "text/html; charset=utf-8", replay.Header().Get("Content-Type"); got != expected { 61 t.Errorf("got a Content-Type of %q; expected %q", got, expected) 62 } 63 if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected { 64 t.Errorf("got a X-Test-Header of %q; expected %q", got, expected) 65 } 66 } 67 68 type customWriterRecorder struct { 69 w io.Writer 70 *httptest.ResponseRecorder 71 } 72 73 func (r *customWriterRecorder) Write(p []byte) (n int, err error) { 74 return r.w.Write(p) 75 } 76 77 type limitWriter struct { 78 w io.Writer 79 n int 80 } 81 82 func (w *limitWriter) Write(p []byte) (n int, err error) { 83 if len(p) > w.n { 84 p = p[:w.n] 85 } 86 if len(p) > 0 { 87 n, err = w.w.Write(p) 88 w.n -= n 89 } 90 if w.n == 0 { 91 err = errors.New("past write limit") 92 } 93 return 94 } 95 96 // If there's an error copying the child's output to the parent, test 97 // that we kill the child. 98 func TestKillChildAfterCopyError(t *testing.T) { 99 if runtime.GOOS == "nacl" || iOS { 100 t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH) 101 } 102 103 defer func() { testHookStartProcess = nil }() 104 proc := make(chan *os.Process, 1) 105 testHookStartProcess = func(p *os.Process) { 106 proc <- p 107 } 108 109 h := &Handler{ 110 Path: os.Args[0], 111 Root: "/test.go", 112 Args: []string{"-test.run=TestBeChildCGIProcess"}, 113 } 114 req, _ := http.NewRequest("GET", "http://example.com/test.cgi?write-forever=1", nil) 115 rec := httptest.NewRecorder() 116 var out bytes.Buffer 117 const writeLen = 50 << 10 118 rw := &customWriterRecorder{&limitWriter{&out, writeLen}, rec} 119 120 donec := make(chan bool, 1) 121 go func() { 122 h.ServeHTTP(rw, req) 123 donec <- true 124 }() 125 126 select { 127 case <-donec: 128 if out.Len() != writeLen || out.Bytes()[0] != 'a' { 129 t.Errorf("unexpected output: %q", out.Bytes()) 130 } 131 case <-time.After(5 * time.Second): 132 t.Errorf("timeout. ServeHTTP hung and didn't kill the child process?") 133 select { 134 case p := <-proc: 135 p.Kill() 136 t.Logf("killed process") 137 default: 138 t.Logf("didn't kill process") 139 } 140 } 141 } 142 143 // Test that a child handler writing only headers works. 144 // golang.org/issue/7196 145 func TestChildOnlyHeaders(t *testing.T) { 146 if runtime.GOOS == "nacl" || iOS { 147 t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH) 148 } 149 150 h := &Handler{ 151 Path: os.Args[0], 152 Root: "/test.go", 153 Args: []string{"-test.run=TestBeChildCGIProcess"}, 154 } 155 expectedMap := map[string]string{ 156 "_body": "", 157 } 158 replay := runCgiTest(t, h, "GET /test.go?no-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap) 159 if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected { 160 t.Errorf("got a X-Test-Header of %q; expected %q", got, expected) 161 } 162 } 163 164 // golang.org/issue/7198 165 func Test500WithNoHeaders(t *testing.T) { want500Test(t, "/immediate-disconnect") } 166 func Test500WithNoContentType(t *testing.T) { want500Test(t, "/no-content-type") } 167 func Test500WithEmptyHeaders(t *testing.T) { want500Test(t, "/empty-headers") } 168 169 func want500Test(t *testing.T, path string) { 170 h := &Handler{ 171 Path: os.Args[0], 172 Root: "/test.go", 173 Args: []string{"-test.run=TestBeChildCGIProcess"}, 174 } 175 expectedMap := map[string]string{ 176 "_body": "", 177 } 178 replay := runCgiTest(t, h, "GET "+path+" HTTP/1.0\nHost: example.com\n\n", expectedMap) 179 if replay.Code != 500 { 180 t.Errorf("Got code %d; want 500", replay.Code) 181 } 182 } 183 184 type neverEnding byte 185 186 func (b neverEnding) Read(p []byte) (n int, err error) { 187 for i := range p { 188 p[i] = byte(b) 189 } 190 return len(p), nil 191 } 192 193 // Note: not actually a test. 194 func TestBeChildCGIProcess(t *testing.T) { 195 if os.Getenv("REQUEST_METHOD") == "" { 196 // Not in a CGI environment; skipping test. 197 return 198 } 199 switch os.Getenv("REQUEST_URI") { 200 case "/immediate-disconnect": 201 os.Exit(0) 202 case "/no-content-type": 203 fmt.Printf("Content-Length: 6\n\nHello\n") 204 os.Exit(0) 205 case "/empty-headers": 206 fmt.Printf("\nHello") 207 os.Exit(0) 208 } 209 Serve(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { 210 rw.Header().Set("X-Test-Header", "X-Test-Value") 211 req.ParseForm() 212 if req.FormValue("no-body") == "1" { 213 return 214 } 215 if req.FormValue("write-forever") == "1" { 216 io.Copy(rw, neverEnding('a')) 217 for { 218 time.Sleep(5 * time.Second) // hang forever, until killed 219 } 220 } 221 fmt.Fprintf(rw, "test=Hello CGI-in-CGI\n") 222 for k, vv := range req.Form { 223 for _, v := range vv { 224 fmt.Fprintf(rw, "param-%s=%s\n", k, v) 225 } 226 } 227 for _, kv := range os.Environ() { 228 fmt.Fprintf(rw, "env-%s\n", kv) 229 } 230 })) 231 os.Exit(0) 232 }