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.
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)
34 Args: []string{"-test.run=TestBeChildCGIProcess"},
36 expectedMap := map[string]string{
37 "test": "Hello CGI-in-CGI",
40 "env-GATEWAY_INTERFACE": "CGI/1.1",
41 "env-HTTP_HOST": "example.com",
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",
55 replay := runCgiTest(t, h, "GET /test.go?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
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)
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)
65 type customWriterRecorder struct {
67 *httptest.ResponseRecorder
70 func (r *customWriterRecorder) Write(p []byte) (n int, err error) {
74 type limitWriter struct {
79 func (w *limitWriter) Write(p []byte) (n int, err error) {
88 err = errors.New("past write limit")
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)
98 defer func() { testHookStartProcess = nil }()
99 proc := make(chan *os.Process, 1)
100 testHookStartProcess = func(p *os.Process) {
107 Args: []string{"-test.run=TestBeChildCGIProcess"},
109 req, _ := http.NewRequest("GET", "http://example.com/test.cgi?write-forever=1", nil)
110 rec := httptest.NewRecorder()
112 const writeLen = 50 << 10
113 rw := &customWriterRecorder{&limitWriter{&out, writeLen}, rec}
115 donec := make(chan bool, 1)
123 if out.Len() != writeLen || out.Bytes()[0] != 'a' {
124 t.Errorf("unexpected output: %q", out.Bytes())
126 case <-time.After(5 * time.Second):
127 t.Errorf("timeout. ServeHTTP hung and didn't kill the child process?")
131 t.Logf("killed process")
133 t.Logf("didn't kill process")
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)
146 Args: []string{"-test.run=TestBeChildCGIProcess"},
148 expectedMap := map[string]string{
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)
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)
165 Args: []string{"-test.run=TestBeChildCGIProcess"},
167 expectedMap := map[string]string{
168 "nil-request-body": "false",
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)
174 func TestChildContentType(t *testing.T) {
175 testenv.MustHaveExec(t)
180 Args: []string{"-test.run=TestBeChildCGIProcess"},
182 var tests = []struct {
189 wantCT: "text/plain; charset=utf-8",
193 body: "<html><head><title>test page</title></head><body>This is a body</body></html>",
194 wantCT: "text/html; charset=utf-8",
198 body: strings.Repeat("gopher", 86),
199 wantCT: "text/plain; charset=utf-8",
203 body: "\xFF\xD8\xFF" + strings.Repeat("B", 1024),
204 wantCT: "image/jpeg",
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)
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") }
224 func want500Test(t *testing.T, path string) {
228 Args: []string{"-test.run=TestBeChildCGIProcess"},
230 expectedMap := map[string]string{
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)
239 type neverEnding byte
241 func (b neverEnding) Read(p []byte) (n int, err error) {
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.
254 switch os.Getenv("REQUEST_URI") {
255 case "/immediate-disconnect":
257 case "/no-content-type":
258 fmt.Printf("Content-Length: 6\n\nHello\n")
260 case "/empty-headers":
261 fmt.Printf("\nHello")
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)
269 rw.Header().Set("X-Test-Header", "X-Test-Value")
271 if req.FormValue("no-body") == "1" {
274 if eb, ok := req.Form["exact-body"]; ok {
275 io.WriteString(rw, eb[0])
278 if req.FormValue("write-forever") == "1" {
279 io.Copy(rw, neverEnding('a'))
281 time.Sleep(5 * time.Second) // hang forever, until killed
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)
290 for _, kv := range os.Environ() {
291 fmt.Fprintf(rw, "env-%s\n", kv)