OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / libgo / go / net / http / cgi / matryoshka_test.go
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 // 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.
58                 return
59         }
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")
63                 req.ParseForm()
64                 for k, vv := range req.Form {
65                         for _, v := range vv {
66                                 fmt.Fprintf(rw, "param-%s=%s\n", k, v)
67                         }
68                 }
69                 for _, kv := range os.Environ() {
70                         fmt.Fprintf(rw, "env-%s\n", kv)
71                 }
72         }))
73         os.Exit(0)
74 }