github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/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  		"REMOTE_ADDR":     "5.6.7.8",
    25  	}
    26  	req, err := RequestFromMap(env)
    27  	if err != nil {
    28  		t.Fatalf("RequestFromMap: %v", err)
    29  	}
    30  	if g, e := req.UserAgent(), "goclient"; e != g {
    31  		t.Errorf("expected UserAgent %q; got %q", e, g)
    32  	}
    33  	if g, e := req.Method, "GET"; e != g {
    34  		t.Errorf("expected Method %q; got %q", e, g)
    35  	}
    36  	if g, e := req.Header.Get("Content-Type"), "text/xml"; e != g {
    37  		t.Errorf("expected Content-Type %q; got %q", e, g)
    38  	}
    39  	if g, e := req.ContentLength, int64(123); e != g {
    40  		t.Errorf("expected ContentLength %d; got %d", e, g)
    41  	}
    42  	if g, e := req.Referer(), "elsewhere"; e != g {
    43  		t.Errorf("expected Referer %q; got %q", e, g)
    44  	}
    45  	if req.Header == nil {
    46  		t.Fatalf("unexpected nil Header")
    47  	}
    48  	if g, e := req.Header.Get("Foo-Bar"), "baz"; e != g {
    49  		t.Errorf("expected Foo-Bar %q; got %q", e, g)
    50  	}
    51  	if g, e := req.URL.String(), "http://example.com/path?a=b"; e != g {
    52  		t.Errorf("expected URL %q; got %q", e, g)
    53  	}
    54  	if g, e := req.FormValue("a"), "b"; e != g {
    55  		t.Errorf("expected FormValue(a) %q; got %q", e, g)
    56  	}
    57  	if req.Trailer == nil {
    58  		t.Errorf("unexpected nil Trailer")
    59  	}
    60  	if req.TLS != nil {
    61  		t.Errorf("expected nil TLS")
    62  	}
    63  	if e, g := "5.6.7.8:0", req.RemoteAddr; e != g {
    64  		t.Errorf("RemoteAddr: got %q; want %q", g, e)
    65  	}
    66  }
    67  
    68  func TestRequestWithTLS(t *testing.T) {
    69  	env := map[string]string{
    70  		"SERVER_PROTOCOL": "HTTP/1.1",
    71  		"REQUEST_METHOD":  "GET",
    72  		"HTTP_HOST":       "example.com",
    73  		"HTTP_REFERER":    "elsewhere",
    74  		"REQUEST_URI":     "/path?a=b",
    75  		"CONTENT_TYPE":    "text/xml",
    76  		"HTTPS":           "1",
    77  		"REMOTE_ADDR":     "5.6.7.8",
    78  	}
    79  	req, err := RequestFromMap(env)
    80  	if err != nil {
    81  		t.Fatalf("RequestFromMap: %v", err)
    82  	}
    83  	if g, e := req.URL.String(), "https://example.com/path?a=b"; e != g {
    84  		t.Errorf("expected URL %q; got %q", e, g)
    85  	}
    86  	if req.TLS == nil {
    87  		t.Errorf("expected non-nil TLS")
    88  	}
    89  }
    90  
    91  func TestRequestWithoutHost(t *testing.T) {
    92  	env := map[string]string{
    93  		"SERVER_PROTOCOL": "HTTP/1.1",
    94  		"HTTP_HOST":       "",
    95  		"REQUEST_METHOD":  "GET",
    96  		"REQUEST_URI":     "/path?a=b",
    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(), "/path?a=b"; e != g {
   107  		t.Errorf("URL = %q; want %q", g, e)
   108  	}
   109  }
   110  
   111  func TestRequestWithoutRequestURI(t *testing.T) {
   112  	env := map[string]string{
   113  		"SERVER_PROTOCOL": "HTTP/1.1",
   114  		"HTTP_HOST":       "example.com",
   115  		"REQUEST_METHOD":  "GET",
   116  		"SCRIPT_NAME":     "/dir/scriptname",
   117  		"PATH_INFO":       "/p1/p2",
   118  		"QUERY_STRING":    "a=1&b=2",
   119  		"CONTENT_LENGTH":  "123",
   120  	}
   121  	req, err := RequestFromMap(env)
   122  	if err != nil {
   123  		t.Fatalf("RequestFromMap: %v", err)
   124  	}
   125  	if req.URL == nil {
   126  		t.Fatalf("unexpected nil URL")
   127  	}
   128  	if g, e := req.URL.String(), "http://example.com/dir/scriptname/p1/p2?a=1&b=2"; e != g {
   129  		t.Errorf("URL = %q; want %q", g, e)
   130  	}
   131  }