github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/net/http/cgi/matryoshka_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  	"fmt"
    13  	"net/http"
    14  	"os"
    15  	"testing"
    16  )
    17  
    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) {
    21  	h := &Handler{
    22  		Path: os.Args[0],
    23  		Root: "/test.go",
    24  		Args: []string{"-test.run=TestBeChildCGIProcess"},
    25  	}
    26  	expectedMap := map[string]string{
    27  		"test":                  "Hello CGI-in-CGI",
    28  		"param-a":               "b",
    29  		"param-foo":             "bar",
    30  		"env-GATEWAY_INTERFACE": "CGI/1.1",
    31  		"env-HTTP_HOST":         "example.com",
    32  		"env-PATH_INFO":         "",
    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",
    43  	}
    44  	replay := runCgiTest(t, h, "GET /test.go?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
    45  
    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)
    48  	}
    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)
    51  	}
    52  }
    53  
    54  // Test that a child handler only writing headers works.
    55  func TestChildOnlyHeaders(t *testing.T) {
    56  	h := &Handler{
    57  		Path: os.Args[0],
    58  		Root: "/test.go",
    59  		Args: []string{"-test.run=TestBeChildCGIProcess"},
    60  	}
    61  	expectedMap := map[string]string{
    62  		"_body": "",
    63  	}
    64  	replay := runCgiTest(t, h, "GET /test.go?no-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap)
    65  	if expected, got := "X-Test-Value", replay.Header().Get("X-Test-Header"); got != expected {
    66  		t.Errorf("got a X-Test-Header of %q; expected %q", got, expected)
    67  	}
    68  }
    69  
    70  // Note: not actually a test.
    71  func TestBeChildCGIProcess(t *testing.T) {
    72  	if os.Getenv("REQUEST_METHOD") == "" {
    73  		// Not in a CGI environment; skipping test.
    74  		return
    75  	}
    76  	Serve(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
    77  		rw.Header().Set("X-Test-Header", "X-Test-Value")
    78  		req.ParseForm()
    79  		if req.FormValue("no-body") == "1" {
    80  			return
    81  		}
    82  		fmt.Fprintf(rw, "test=Hello CGI-in-CGI\n")
    83  		for k, vv := range req.Form {
    84  			for _, v := range vv {
    85  				fmt.Fprintf(rw, "param-%s=%s\n", k, v)
    86  			}
    87  		}
    88  		for _, kv := range os.Environ() {
    89  			fmt.Fprintf(rw, "env-%s\n", kv)
    90  		}
    91  	}))
    92  	os.Exit(0)
    93  }