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.
18 // This test is a CGI host (testing host.go) that runs its own binary
19 // as a child process testing the other half of CGI (child.go).
20 func TestHostingOurselves(t *testing.T) {
24 Args: []string{"-test.run=TestBeChildCGIProcess"},
26 expectedMap := map[string]string{
27 "test": "Hello CGI-in-CGI",
30 "env-GATEWAY_INTERFACE": "CGI/1.1",
31 "env-HTTP_HOST": "example.com",
33 "env-QUERY_STRING": "foo=bar&a=b",
34 "env-REMOTE_ADDR": "1.2.3.4",
35 "env-REMOTE_HOST": "1.2.3.4",
36 "env-REQUEST_METHOD": "GET",
37 "env-REQUEST_URI": "/test.go?foo=bar&a=b",
38 "env-SCRIPT_FILENAME": os.Args[0],
39 "env-SCRIPT_NAME": "/test.go",
40 "env-SERVER_NAME": "example.com",
41 "env-SERVER_PORT": "80",
42 "env-SERVER_SOFTWARE": "go",
44 replay := runCgiTest(t, h, "GET /test.go?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
46 if expected, got := "text/html; charset=utf-8", replay.Header().Get("Content-Type"); got != expected {
47 t.Errorf("got a Content-Type of %q; expected %q", got, expected)
49 if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected {
50 t.Errorf("got a X-Test-Header of %q; expected %q", got, expected)
54 // Note: not actually a test.
55 func TestBeChildCGIProcess(t *testing.T) {
56 if os.Getenv("REQUEST_METHOD") == "" {
57 // Not in a CGI environment; skipping test.
60 Serve(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
61 rw.Header().Set("X-Test-Header", "X-Test-Value")
62 fmt.Fprintf(rw, "test=Hello CGI-in-CGI\n")
64 for k, vv := range req.Form {
65 for _, v := range vv {
66 fmt.Fprintf(rw, "param-%s=%s\n", k, v)
69 for _, kv := range os.Environ() {
70 fmt.Fprintf(rw, "env-%s\n", kv)