github.com/useflyent/fhttp@v0.0.0-20211004035111-333f430cfbbf/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 "github.com/useflyent/fhttp"
    23  	"github.com/useflyent/fhttp/httptest"
    24  	"github.com/useflyent/fhttp/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  func TestKillChildAfterCopyError(t *testing.T) {
    97  	testenv.MustHaveExec(t)
    98  
    99  	defer func() { testHookStartProcess = nil }()
   100  	proc := make(chan *os.Process, 1)
   101  	testHookStartProcess = func(p *os.Process) {
   102  		proc <- p
   103  	}
   104  
   105  	h := &Handler{
   106  		Path: os.Args[0],
   107  		Root: "/test.go",
   108  		Args: []string{"-test.run=TestBeChildCGIProcess"},
   109  	}
   110  	req, _ := http.NewRequest("GET", "http://example.com/test.cgi?write-forever=1", nil)
   111  	rec := httptest.NewRecorder()
   112  	var out bytes.Buffer
   113  	const writeLen = 50 << 10
   114  	rw := &customWriterRecorder{&limitWriter{&out, writeLen}, rec}
   115  
   116  	donec := make(chan bool, 1)
   117  	go func() {
   118  		h.ServeHTTP(rw, req)
   119  		donec <- true
   120  	}()
   121  
   122  	select {
   123  	case <-donec:
   124  		if out.Len() != writeLen || out.Bytes()[0] != 'a' {
   125  			t.Errorf("unexpected output: %q", out.Bytes())
   126  		}
   127  	case <-time.After(5 * time.Second):
   128  		t.Errorf("timeout. ServeHTTP hung and didn't kill the child process?")
   129  		select {
   130  		case p := <-proc:
   131  			p.Kill()
   132  			t.Logf("killed process")
   133  		default:
   134  			t.Logf("didn't kill process")
   135  		}
   136  	}
   137  }
   138  
   139  // Test that a child handler writing only headers works.
   140  // golang.org/issue/7196
   141  func TestChildOnlyHeaders(t *testing.T) {
   142  	testenv.MustHaveExec(t)
   143  
   144  	h := &Handler{
   145  		Path: os.Args[0],
   146  		Root: "/test.go",
   147  		Args: []string{"-test.run=TestBeChildCGIProcess"},
   148  	}
   149  	expectedMap := map[string]string{
   150  		"_body": "",
   151  	}
   152  	replay := runCgiTest(t, h, "GET /test.go?no-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap)
   153  	if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected {
   154  		t.Errorf("got a X-Test-Header of %q; expected %q", got, expected)
   155  	}
   156  }
   157  
   158  // Test that a child handler does not receive a nil Request Body.
   159  // golang.org/issue/39190
   160  func TestNilRequestBody(t *testing.T) {
   161  	testenv.MustHaveExec(t)
   162  
   163  	h := &Handler{
   164  		Path: os.Args[0],
   165  		Root: "/test.go",
   166  		Args: []string{"-test.run=TestBeChildCGIProcess"},
   167  	}
   168  	expectedMap := map[string]string{
   169  		"nil-request-body": "false",
   170  	}
   171  	_ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap)
   172  	_ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\nContent-Length: 0\n\n", expectedMap)
   173  }
   174  
   175  func TestChildContentType(t *testing.T) {
   176  	testenv.MustHaveExec(t)
   177  
   178  	h := &Handler{
   179  		Path: os.Args[0],
   180  		Root: "/test.go",
   181  		Args: []string{"-test.run=TestBeChildCGIProcess"},
   182  	}
   183  	var tests = []struct {
   184  		name   string
   185  		body   string
   186  		wantCT string
   187  	}{
   188  		{
   189  			name:   "no body",
   190  			wantCT: "text/plain; charset=utf-8",
   191  		},
   192  		{
   193  			name:   "html",
   194  			body:   "<html><head><title>test page</title></head><body>This is a body</body></html>",
   195  			wantCT: "text/html; charset=utf-8",
   196  		},
   197  		{
   198  			name:   "text",
   199  			body:   strings.Repeat("gopher", 86),
   200  			wantCT: "text/plain; charset=utf-8",
   201  		},
   202  		{
   203  			name:   "jpg",
   204  			body:   "\xFF\xD8\xFF" + strings.Repeat("B", 1024),
   205  			wantCT: "image/jpeg",
   206  		},
   207  	}
   208  	for _, tt := range tests {
   209  		t.Run(tt.name, func(t *testing.T) {
   210  			expectedMap := map[string]string{"_body": tt.body}
   211  			req := fmt.Sprintf("GET /test.go?exact-body=%s HTTP/1.0\nHost: example.com\n\n", url.QueryEscape(tt.body))
   212  			replay := runCgiTest(t, h, req, expectedMap)
   213  			if got := replay.Header().Get("Content-Type"); got != tt.wantCT {
   214  				t.Errorf("got a Content-Type of %q; expected it to start with %q", got, tt.wantCT)
   215  			}
   216  		})
   217  	}
   218  }
   219  
   220  // golang.org/issue/7198
   221  func Test500WithNoHeaders(t *testing.T)     { want500Test(t, "/immediate-disconnect") }
   222  func Test500WithNoContentType(t *testing.T) { want500Test(t, "/no-content-type") }
   223  func Test500WithEmptyHeaders(t *testing.T)  { want500Test(t, "/empty-headers") }
   224  
   225  func want500Test(t *testing.T, path string) {
   226  	h := &Handler{
   227  		Path: os.Args[0],
   228  		Root: "/test.go",
   229  		Args: []string{"-test.run=TestBeChildCGIProcess"},
   230  	}
   231  	expectedMap := map[string]string{
   232  		"_body": "",
   233  	}
   234  	replay := runCgiTest(t, h, "GET "+path+" HTTP/1.0\nHost: example.com\n\n", expectedMap)
   235  	if replay.Code != 500 {
   236  		t.Errorf("Got code %d; want 500", replay.Code)
   237  	}
   238  }
   239  
   240  type neverEnding byte
   241  
   242  func (b neverEnding) Read(p []byte) (n int, err error) {
   243  	for i := range p {
   244  		p[i] = byte(b)
   245  	}
   246  	return len(p), nil
   247  }
   248  
   249  // Note: not actually a test.
   250  func TestBeChildCGIProcess(t *testing.T) {
   251  	if os.Getenv("REQUEST_METHOD") == "" {
   252  		// Not in a CGI environment; skipping test.
   253  		return
   254  	}
   255  	switch os.Getenv("REQUEST_URI") {
   256  	case "/immediate-disconnect":
   257  		os.Exit(0)
   258  	case "/no-content-type":
   259  		fmt.Printf("Content-Length: 6\n\nHello\n")
   260  		os.Exit(0)
   261  	case "/empty-headers":
   262  		fmt.Printf("\nHello")
   263  		os.Exit(0)
   264  	}
   265  	Serve(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
   266  		if req.FormValue("nil-request-body") == "1" {
   267  			fmt.Fprintf(rw, "nil-request-body=%v\n", req.Body == nil)
   268  			return
   269  		}
   270  		rw.Header().Set("X-Test-Header", "X-Test-Value")
   271  		req.ParseForm()
   272  		if req.FormValue("no-body") == "1" {
   273  			return
   274  		}
   275  		if eb, ok := req.Form["exact-body"]; ok {
   276  			io.WriteString(rw, eb[0])
   277  			return
   278  		}
   279  		if req.FormValue("write-forever") == "1" {
   280  			io.Copy(rw, neverEnding('a'))
   281  			for {
   282  				time.Sleep(5 * time.Second) // hang forever, until killed
   283  			}
   284  		}
   285  		fmt.Fprintf(rw, "test=Hello CGI-in-CGI\n")
   286  		for k, vv := range req.Form {
   287  			for _, v := range vv {
   288  				fmt.Fprintf(rw, "param-%s=%s\n", k, v)
   289  			}
   290  		}
   291  		for _, kv := range os.Environ() {
   292  			fmt.Fprintf(rw, "env-%s\n", kv)
   293  		}
   294  	}))
   295  	os.Exit(0)
   296  }