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.
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.
24 // This test is a CGI host (testing host.go) that runs its own binary
25 // as a child process testing the other half of CGI (child.go).
26 func TestHostingOurselves(t *testing.T) {
27 testenv.MustHaveExec(t)
32 Args: []string{"-test.run=TestBeChildCGIProcess"},
34 expectedMap := map[string]string{
35 "test": "Hello CGI-in-CGI",
38 "env-GATEWAY_INTERFACE": "CGI/1.1",
39 "env-HTTP_HOST": "example.com",
41 "env-QUERY_STRING": "foo=bar&a=b",
42 "env-REMOTE_ADDR": "1.2.3.4",
43 "env-REMOTE_HOST": "1.2.3.4",
44 "env-REMOTE_PORT": "1234",
45 "env-REQUEST_METHOD": "GET",
46 "env-REQUEST_URI": "/test.go?foo=bar&a=b",
47 "env-SCRIPT_FILENAME": os.Args[0],
48 "env-SCRIPT_NAME": "/test.go",
49 "env-SERVER_NAME": "example.com",
50 "env-SERVER_PORT": "80",
51 "env-SERVER_SOFTWARE": "go",
53 replay := runCgiTest(t, h, "GET /test.go?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
55 if expected, got := "text/html; charset=utf-8", replay.Header().Get("Content-Type"); got != expected {
56 t.Errorf("got a Content-Type of %q; expected %q", got, expected)
58 if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected {
59 t.Errorf("got a X-Test-Header of %q; expected %q", got, expected)
63 type customWriterRecorder struct {
65 *httptest.ResponseRecorder
68 func (r *customWriterRecorder) Write(p []byte) (n int, err error) {
72 type limitWriter struct {
77 func (w *limitWriter) Write(p []byte) (n int, err error) {
86 err = errors.New("past write limit")
91 // If there's an error copying the child's output to the parent, test
92 // that we kill the child.
93 func TestKillChildAfterCopyError(t *testing.T) {
94 testenv.MustHaveExec(t)
96 defer func() { testHookStartProcess = nil }()
97 proc := make(chan *os.Process, 1)
98 testHookStartProcess = func(p *os.Process) {
105 Args: []string{"-test.run=TestBeChildCGIProcess"},
107 req, _ := http.NewRequest("GET", "http://example.com/test.cgi?write-forever=1", nil)
108 rec := httptest.NewRecorder()
110 const writeLen = 50 << 10
111 rw := &customWriterRecorder{&limitWriter{&out, writeLen}, rec}
113 donec := make(chan bool, 1)
121 if out.Len() != writeLen || out.Bytes()[0] != 'a' {
122 t.Errorf("unexpected output: %q", out.Bytes())
124 case <-time.After(5 * time.Second):
125 t.Errorf("timeout. ServeHTTP hung and didn't kill the child process?")
129 t.Logf("killed process")
131 t.Logf("didn't kill process")
136 // Test that a child handler writing only headers works.
137 // golang.org/issue/7196
138 func TestChildOnlyHeaders(t *testing.T) {
139 testenv.MustHaveExec(t)
144 Args: []string{"-test.run=TestBeChildCGIProcess"},
146 expectedMap := map[string]string{
149 replay := runCgiTest(t, h, "GET /test.go?no-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap)
150 if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected {
151 t.Errorf("got a X-Test-Header of %q; expected %q", got, expected)
155 // golang.org/issue/7198
156 func Test500WithNoHeaders(t *testing.T) { want500Test(t, "/immediate-disconnect") }
157 func Test500WithNoContentType(t *testing.T) { want500Test(t, "/no-content-type") }
158 func Test500WithEmptyHeaders(t *testing.T) { want500Test(t, "/empty-headers") }
160 func want500Test(t *testing.T, path string) {
164 Args: []string{"-test.run=TestBeChildCGIProcess"},
166 expectedMap := map[string]string{
169 replay := runCgiTest(t, h, "GET "+path+" HTTP/1.0\nHost: example.com\n\n", expectedMap)
170 if replay.Code != 500 {
171 t.Errorf("Got code %d; want 500", replay.Code)
175 type neverEnding byte
177 func (b neverEnding) Read(p []byte) (n int, err error) {
184 // Note: not actually a test.
185 func TestBeChildCGIProcess(t *testing.T) {
186 if os.Getenv("REQUEST_METHOD") == "" {
187 // Not in a CGI environment; skipping test.
190 switch os.Getenv("REQUEST_URI") {
191 case "/immediate-disconnect":
193 case "/no-content-type":
194 fmt.Printf("Content-Length: 6\n\nHello\n")
196 case "/empty-headers":
197 fmt.Printf("\nHello")
200 Serve(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
201 rw.Header().Set("X-Test-Header", "X-Test-Value")
203 if req.FormValue("no-body") == "1" {
206 if req.FormValue("write-forever") == "1" {
207 io.Copy(rw, neverEnding('a'))
209 time.Sleep(5 * time.Second) // hang forever, until killed
212 fmt.Fprintf(rw, "test=Hello CGI-in-CGI\n")
213 for k, vv := range req.Form {
214 for _, v := range vv {
215 fmt.Fprintf(rw, "param-%s=%s\n", k, v)
218 for _, kv := range os.Environ() {
219 fmt.Fprintf(rw, "env-%s\n", kv)