gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/gmhttp/cgi/integration_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/url" 17 "os" 18 "strings" 19 "testing" 20 "time" 21 22 http "gitee.com/ks-custle/core-gm/gmhttp" 23 "gitee.com/ks-custle/core-gm/gmhttp/httptest" 24 "gitee.com/ks-custle/core-gm/internal/testenv" 25 ) 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 testenv.MustHaveExec(t) 31 32 h := &Handler{ 33 Path: os.Args[0], 34 Root: "/test.go", 35 Args: []string{"-test.run=TestBeChildCGIProcess"}, 36 } 37 expectedMap := map[string]string{ 38 "test": "Hello CGI-in-CGI", 39 "param-a": "b", 40 "param-foo": "bar", 41 "env-GATEWAY_INTERFACE": "CGI/1.1", 42 "env-HTTP_HOST": "example.com", 43 "env-PATH_INFO": "", 44 "env-QUERY_STRING": "foo=bar&a=b", 45 "env-REMOTE_ADDR": "1.2.3.4", 46 "env-REMOTE_HOST": "1.2.3.4", 47 "env-REMOTE_PORT": "1234", 48 "env-REQUEST_METHOD": "GET", 49 "env-REQUEST_URI": "/test.go?foo=bar&a=b", 50 "env-SCRIPT_FILENAME": os.Args[0], 51 "env-SCRIPT_NAME": "/test.go", 52 "env-SERVER_NAME": "example.com", 53 "env-SERVER_PORT": "80", 54 "env-SERVER_SOFTWARE": "go", 55 } 56 replay := runCgiTest(t, h, "GET /test.go?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap) 57 58 if expected, got := "text/plain; charset=utf-8", replay.Header().Get("Content-Type"); got != expected { 59 t.Errorf("got a Content-Type of %q; expected %q", got, expected) 60 } 61 if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected { 62 t.Errorf("got a X-Test-Header of %q; expected %q", got, expected) 63 } 64 } 65 66 type customWriterRecorder struct { 67 w io.Writer 68 *httptest.ResponseRecorder 69 } 70 71 func (r *customWriterRecorder) Write(p []byte) (n int, err error) { 72 return r.w.Write(p) 73 } 74 75 type limitWriter struct { 76 w io.Writer 77 n int 78 } 79 80 func (w *limitWriter) Write(p []byte) (n int, err error) { 81 if len(p) > w.n { 82 p = p[:w.n] 83 } 84 if len(p) > 0 { 85 n, err = w.w.Write(p) 86 w.n -= n 87 } 88 if w.n == 0 { 89 err = errors.New("past write limit") 90 } 91 return 92 } 93 94 // If there's an error copying the child's output to the parent, test 95 // that we kill the child. 96 // 97 //goland:noinspection HttpUrlsUsage 98 func TestKillChildAfterCopyError(t *testing.T) { 99 testenv.MustHaveExec(t) 100 101 h := &Handler{ 102 Path: os.Args[0], 103 Root: "/test.go", 104 Args: []string{"-test.run=TestBeChildCGIProcess"}, 105 } 106 req, _ := http.NewRequest("GET", "http://example.com/test.cgi?write-forever=1", nil) 107 rec := httptest.NewRecorder() 108 var out bytes.Buffer 109 const writeLen = 50 << 10 110 rw := &customWriterRecorder{&limitWriter{&out, writeLen}, rec} 111 112 h.ServeHTTP(rw, req) 113 if out.Len() != writeLen || out.Bytes()[0] != 'a' { 114 t.Errorf("unexpected output: %q", out.Bytes()) 115 } 116 } 117 118 // Test that a child handler writing only headers works. 119 // golang.org/issue/7196 120 func TestChildOnlyHeaders(t *testing.T) { 121 testenv.MustHaveExec(t) 122 123 h := &Handler{ 124 Path: os.Args[0], 125 Root: "/test.go", 126 Args: []string{"-test.run=TestBeChildCGIProcess"}, 127 } 128 expectedMap := map[string]string{ 129 "_body": "", 130 } 131 replay := runCgiTest(t, h, "GET /test.go?no-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap) 132 if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected { 133 t.Errorf("got a X-Test-Header of %q; expected %q", got, expected) 134 } 135 } 136 137 // Test that a child handler does not receive a nil Request Body. 138 // golang.org/issue/39190 139 func TestNilRequestBody(t *testing.T) { 140 testenv.MustHaveExec(t) 141 142 h := &Handler{ 143 Path: os.Args[0], 144 Root: "/test.go", 145 Args: []string{"-test.run=TestBeChildCGIProcess"}, 146 } 147 expectedMap := map[string]string{ 148 "nil-request-body": "false", 149 } 150 _ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap) 151 _ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\nContent-Length: 0\n\n", expectedMap) 152 } 153 154 func TestChildContentType(t *testing.T) { 155 testenv.MustHaveExec(t) 156 157 h := &Handler{ 158 Path: os.Args[0], 159 Root: "/test.go", 160 Args: []string{"-test.run=TestBeChildCGIProcess"}, 161 } 162 var tests = []struct { 163 name string 164 body string 165 wantCT string 166 }{ 167 { 168 name: "no body", 169 wantCT: "text/plain; charset=utf-8", 170 }, 171 { 172 name: "html", 173 body: "<html><head><title>test page</title></head><body>This is a body</body></html>", 174 wantCT: "text/html; charset=utf-8", 175 }, 176 { 177 name: "text", 178 body: strings.Repeat("gopher", 86), 179 wantCT: "text/plain; charset=utf-8", 180 }, 181 { 182 name: "jpg", 183 body: "\xFF\xD8\xFF" + strings.Repeat("B", 1024), 184 wantCT: "image/jpeg", 185 }, 186 } 187 for _, tt := range tests { 188 t.Run(tt.name, func(t *testing.T) { 189 expectedMap := map[string]string{"_body": tt.body} 190 req := fmt.Sprintf("GET /test.go?exact-body=%s HTTP/1.0\nHost: example.com\n\n", url.QueryEscape(tt.body)) 191 replay := runCgiTest(t, h, req, expectedMap) 192 if got := replay.Header().Get("Content-Type"); got != tt.wantCT { 193 t.Errorf("got a Content-Type of %q; expected it to start with %q", got, tt.wantCT) 194 } 195 }) 196 } 197 } 198 199 // golang.org/issue/7198 200 func Test500WithNoHeaders(t *testing.T) { want500Test(t, "/immediate-disconnect") } 201 func Test500WithNoContentType(t *testing.T) { want500Test(t, "/no-content-type") } 202 func Test500WithEmptyHeaders(t *testing.T) { want500Test(t, "/empty-headers") } 203 204 func want500Test(t *testing.T, path string) { 205 h := &Handler{ 206 Path: os.Args[0], 207 Root: "/test.go", 208 Args: []string{"-test.run=TestBeChildCGIProcess"}, 209 } 210 expectedMap := map[string]string{ 211 "_body": "", 212 } 213 replay := runCgiTest(t, h, "GET "+path+" HTTP/1.0\nHost: example.com\n\n", expectedMap) 214 if replay.Code != 500 { 215 t.Errorf("Got code %d; want 500", replay.Code) 216 } 217 } 218 219 type neverEnding byte 220 221 func (b neverEnding) Read(p []byte) (n int, err error) { 222 for i := range p { 223 p[i] = byte(b) 224 } 225 return len(p), nil 226 } 227 228 // Note: not actually a test. 229 func TestBeChildCGIProcess(t *testing.T) { 230 if os.Getenv("REQUEST_METHOD") == "" { 231 // Not in a CGI environment; skipping test. 232 return 233 } 234 switch os.Getenv("REQUEST_URI") { 235 case "/immediate-disconnect": 236 os.Exit(0) 237 case "/no-content-type": 238 fmt.Printf("Content-Length: 6\n\nHello\n") 239 os.Exit(0) 240 case "/empty-headers": 241 fmt.Printf("\nHello") 242 os.Exit(0) 243 } 244 _ = Serve(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { 245 if req.FormValue("nil-request-body") == "1" { 246 _, _ = fmt.Fprintf(rw, "nil-request-body=%v\n", req.Body == nil) 247 return 248 } 249 rw.Header().Set("X-Test-Header", "X-Test-Value") 250 _ = req.ParseForm() 251 if req.FormValue("no-body") == "1" { 252 return 253 } 254 if eb, ok := req.Form["exact-body"]; ok { 255 _, _ = io.WriteString(rw, eb[0]) 256 return 257 } 258 if req.FormValue("write-forever") == "1" { 259 _, _ = io.Copy(rw, neverEnding('a')) 260 for { 261 time.Sleep(5 * time.Second) // hang forever, until killed 262 } 263 } 264 _, _ = fmt.Fprintf(rw, "test=Hello CGI-in-CGI\n") 265 for k, vv := range req.Form { 266 for _, v := range vv { 267 _, _ = fmt.Fprintf(rw, "param-%s=%s\n", k, v) 268 } 269 } 270 for _, kv := range os.Environ() { 271 _, _ = fmt.Fprintf(rw, "env-%s\n", kv) 272 } 273 })) 274 os.Exit(0) 275 }