OSDN Git Service

Update to current version of Go library.
[pf3gnuchains/gcc-fork.git] / libgo / go / http / cgi / child_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 for CGI (the child process perspective)
6
7 package cgi
8
9 import (
10         "testing"
11 )
12
13 func TestRequest(t *testing.T) {
14         env := map[string]string{
15                 "SERVER_PROTOCOL": "HTTP/1.1",
16                 "REQUEST_METHOD":  "GET",
17                 "HTTP_HOST":       "example.com",
18                 "HTTP_REFERER":    "elsewhere",
19                 "HTTP_USER_AGENT": "goclient",
20                 "HTTP_FOO_BAR":    "baz",
21                 "REQUEST_URI":     "/path?a=b",
22                 "CONTENT_LENGTH":  "123",
23                 "CONTENT_TYPE":    "text/xml",
24                 "HTTPS":           "1",
25                 "REMOTE_ADDR":     "5.6.7.8",
26         }
27         req, err := RequestFromMap(env)
28         if err != nil {
29                 t.Fatalf("RequestFromMap: %v", err)
30         }
31         if g, e := req.UserAgent, "goclient"; e != g {
32                 t.Errorf("expected UserAgent %q; got %q", e, g)
33         }
34         if g, e := req.Method, "GET"; e != g {
35                 t.Errorf("expected Method %q; got %q", e, g)
36         }
37         if g, e := req.Header.Get("User-Agent"), ""; e != g {
38                 // Tests that we don't put recognized headers in the map
39                 t.Errorf("expected User-Agent %q; got %q", e, g)
40         }
41         if g, e := req.Header.Get("Content-Type"), "text/xml"; e != g {
42                 t.Errorf("expected Content-Type %q; got %q", e, g)
43         }
44         if g, e := req.ContentLength, int64(123); e != g {
45                 t.Errorf("expected ContentLength %d; got %d", e, g)
46         }
47         if g, e := req.Referer, "elsewhere"; e != g {
48                 t.Errorf("expected Referer %q; got %q", e, g)
49         }
50         if req.Header == nil {
51                 t.Fatalf("unexpected nil Header")
52         }
53         if g, e := req.Header.Get("Foo-Bar"), "baz"; e != g {
54                 t.Errorf("expected Foo-Bar %q; got %q", e, g)
55         }
56         if g, e := req.RawURL, "http://example.com/path?a=b"; e != g {
57                 t.Errorf("expected RawURL %q; got %q", e, g)
58         }
59         if g, e := req.URL.String(), "http://example.com/path?a=b"; e != g {
60                 t.Errorf("expected URL %q; got %q", e, g)
61         }
62         if g, e := req.FormValue("a"), "b"; e != g {
63                 t.Errorf("expected FormValue(a) %q; got %q", e, g)
64         }
65         if req.Trailer == nil {
66                 t.Errorf("unexpected nil Trailer")
67         }
68         if req.TLS == nil {
69                 t.Errorf("expected non-nil TLS")
70         }
71         if e, g := "5.6.7.8:0", req.RemoteAddr; e != g {
72                 t.Errorf("RemoteAddr: got %q; want %q", g, e)
73         }
74 }
75
76 func TestRequestWithoutHost(t *testing.T) {
77         env := map[string]string{
78                 "SERVER_PROTOCOL": "HTTP/1.1",
79                 "HTTP_HOST":       "",
80                 "REQUEST_METHOD":  "GET",
81                 "REQUEST_URI":     "/path?a=b",
82                 "CONTENT_LENGTH":  "123",
83         }
84         req, err := RequestFromMap(env)
85         if err != nil {
86                 t.Fatalf("RequestFromMap: %v", err)
87         }
88         if g, e := req.RawURL, "/path?a=b"; e != g {
89                 t.Errorf("expected RawURL %q; got %q", e, g)
90         }
91         if req.URL == nil {
92                 t.Fatalf("unexpected nil URL")
93         }
94         if g, e := req.URL.String(), "/path?a=b"; e != g {
95                 t.Errorf("expected URL %q; got %q", e, g)
96         }
97 }