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.
25 // This test is a CGI host (testing host.go) that runs its own binary
26 // as a child process testing the other half of CGI (child.go).
27 func TestHostingOurselves(t *testing.T) {
28 testenv.MustHaveExec(t)
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/plain; 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)
100 req, _ := http.NewRequest("GET", "http://example.com/test.go?write-forever=1", nil)
101 rec := httptest.NewRecorder()
103 const writeLen = 50 << 10
104 rw := &customWriterRecorder{&limitWriter{&out, writeLen}, rec}
107 if out.Len() != writeLen || out.Bytes()[0] != 'a' {
108 t.Errorf("unexpected output: %q", out.Bytes())
112 // Test that a child handler writing only headers works.
113 // golang.org/issue/7196
114 func TestChildOnlyHeaders(t *testing.T) {
115 testenv.MustHaveExec(t)
121 expectedMap := map[string]string{
124 replay := runCgiTest(t, h, "GET /test.go?no-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap)
125 if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected {
126 t.Errorf("got a X-Test-Header of %q; expected %q", got, expected)
130 // Test that a child handler does not receive a nil Request Body.
131 // golang.org/issue/39190
132 func TestNilRequestBody(t *testing.T) {
133 testenv.MustHaveExec(t)
139 expectedMap := map[string]string{
140 "nil-request-body": "false",
142 _ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap)
143 _ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\nContent-Length: 0\n\n", expectedMap)
146 func TestChildContentType(t *testing.T) {
147 testenv.MustHaveExec(t)
153 var tests = []struct {
160 wantCT: "text/plain; charset=utf-8",
164 body: "<html><head><title>test page</title></head><body>This is a body</body></html>",
165 wantCT: "text/html; charset=utf-8",
169 body: strings.Repeat("gopher", 86),
170 wantCT: "text/plain; charset=utf-8",
174 body: "\xFF\xD8\xFF" + strings.Repeat("B", 1024),
175 wantCT: "image/jpeg",
178 for _, tt := range tests {
179 t.Run(tt.name, func(t *testing.T) {
180 expectedMap := map[string]string{"_body": tt.body}
181 req := fmt.Sprintf("GET /test.go?exact-body=%s HTTP/1.0\nHost: example.com\n\n", url.QueryEscape(tt.body))
182 replay := runCgiTest(t, h, req, expectedMap)
183 if got := replay.Header().Get("Content-Type"); got != tt.wantCT {
184 t.Errorf("got a Content-Type of %q; expected it to start with %q", got, tt.wantCT)
190 // golang.org/issue/7198
191 func Test500WithNoHeaders(t *testing.T) { want500Test(t, "/immediate-disconnect") }
192 func Test500WithNoContentType(t *testing.T) { want500Test(t, "/no-content-type") }
193 func Test500WithEmptyHeaders(t *testing.T) { want500Test(t, "/empty-headers") }
195 func want500Test(t *testing.T, path string) {
200 expectedMap := map[string]string{
203 replay := runCgiTest(t, h, "GET "+path+" HTTP/1.0\nHost: example.com\n\n", expectedMap)
204 if replay.Code != 500 {
205 t.Errorf("Got code %d; want 500", replay.Code)