github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/net/http/cgi/child_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 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("Content-Type"), "text/xml"; e != g {
    38  		t.Errorf("expected Content-Type %q; got %q", e, g)
    39  	}
    40  	if g, e := req.ContentLength, int64(123); e != g {
    41  		t.Errorf("expected ContentLength %d; got %d", e, g)
    42  	}
    43  	if g, e := req.Referer(), "elsewhere"; e != g {
    44  		t.Errorf("expected Referer %q; got %q", e, g)
    45  	}
    46  	if req.Header == nil {
    47  		t.Fatalf("unexpected nil Header")
    48  	}
    49  	if g, e := req.Header.Get("Foo-Bar"), "baz"; e != g {
    50  		t.Errorf("expected Foo-Bar %q; got %q", e, g)
    51  	}
    52  	if g, e := req.URL.String(), "http://example.com/path?a=b"; e != g {
    53  		t.Errorf("expected URL %q; got %q", e, g)
    54  	}
    55  	if g, e := req.FormValue("a"), "b"; e != g {
    56  		t.Errorf("expected FormValue(a) %q; got %q", e, g)
    57  	}
    58  	if req.Trailer == nil {
    59  		t.Errorf("unexpected nil Trailer")
    60  	}
    61  	if req.TLS == nil {
    62  		t.Errorf("expected non-nil TLS")
    63  	}
    64  	if e, g := "5.6.7.8:0", req.RemoteAddr; e != g {
    65  		t.Errorf("RemoteAddr: got %q; want %q", g, e)
    66  	}
    67  }
    68  
    69  func TestRequestWithoutHost(t *testing.T) {
    70  	env := map[string]string{
    71  		"SERVER_PROTOCOL": "HTTP/1.1",
    72  		"HTTP_HOST":       "",
    73  		"REQUEST_METHOD":  "GET",
    74  		"REQUEST_URI":     "/path?a=b",
    75  		"CONTENT_LENGTH":  "123",
    76  	}
    77  	req, err := RequestFromMap(env)
    78  	if err != nil {
    79  		t.Fatalf("RequestFromMap: %v", err)
    80  	}
    81  	if req.URL == nil {
    82  		t.Fatalf("unexpected nil URL")
    83  	}
    84  	if g, e := req.URL.String(), "/path?a=b"; e != g {
    85  		t.Errorf("URL = %q; want %q", g, e)
    86  	}
    87  }
    88  
    89  func TestRequestWithoutRequestURI(t *testing.T) {
    90  	env := map[string]string{
    91  		"SERVER_PROTOCOL": "HTTP/1.1",
    92  		"HTTP_HOST":       "example.com",
    93  		"REQUEST_METHOD":  "GET",
    94  		"SCRIPT_NAME":     "/dir/scriptname",
    95  		"PATH_INFO":       "/p1/p2",
    96  		"QUERY_STRING":    "a=1&b=2",
    97  		"CONTENT_LENGTH":  "123",
    98  	}
    99  	req, err := RequestFromMap(env)
   100  	if err != nil {
   101  		t.Fatalf("RequestFromMap: %v", err)
   102  	}
   103  	if req.URL == nil {
   104  		t.Fatalf("unexpected nil URL")
   105  	}
   106  	if g, e := req.URL.String(), "http://example.com/dir/scriptname/p1/p2?a=1&b=2"; e != g {
   107  		t.Errorf("URL = %q; want %q", g, e)
   108  	}
   109  }