github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/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 "REMOTE_PORT": "54321", 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 nil TLS") 63 } 64 if e, g := "5.6.7.8:54321", req.RemoteAddr; e != g { 65 t.Errorf("RemoteAddr: got %q; want %q", g, e) 66 } 67 } 68 69 func TestRequestWithTLS(t *testing.T) { 70 env := map[string]string{ 71 "SERVER_PROTOCOL": "HTTP/1.1", 72 "REQUEST_METHOD": "GET", 73 "HTTP_HOST": "example.com", 74 "HTTP_REFERER": "elsewhere", 75 "REQUEST_URI": "/path?a=b", 76 "CONTENT_TYPE": "text/xml", 77 "HTTPS": "1", 78 "REMOTE_ADDR": "5.6.7.8", 79 } 80 req, err := RequestFromMap(env) 81 if err != nil { 82 t.Fatalf("RequestFromMap: %v", err) 83 } 84 if g, e := req.URL.String(), "https://example.com/path?a=b"; e != g { 85 t.Errorf("expected URL %q; got %q", e, g) 86 } 87 if req.TLS == nil { 88 t.Errorf("expected non-nil TLS") 89 } 90 } 91 92 func TestRequestWithoutHost(t *testing.T) { 93 env := map[string]string{ 94 "SERVER_PROTOCOL": "HTTP/1.1", 95 "HTTP_HOST": "", 96 "REQUEST_METHOD": "GET", 97 "REQUEST_URI": "/path?a=b", 98 "CONTENT_LENGTH": "123", 99 } 100 req, err := RequestFromMap(env) 101 if err != nil { 102 t.Fatalf("RequestFromMap: %v", err) 103 } 104 if req.URL == nil { 105 t.Fatalf("unexpected nil URL") 106 } 107 if g, e := req.URL.String(), "/path?a=b"; e != g { 108 t.Errorf("URL = %q; want %q", g, e) 109 } 110 } 111 112 func TestRequestWithoutRequestURI(t *testing.T) { 113 env := map[string]string{ 114 "SERVER_PROTOCOL": "HTTP/1.1", 115 "HTTP_HOST": "example.com", 116 "REQUEST_METHOD": "GET", 117 "SCRIPT_NAME": "/dir/scriptname", 118 "PATH_INFO": "/p1/p2", 119 "QUERY_STRING": "a=1&b=2", 120 "CONTENT_LENGTH": "123", 121 } 122 req, err := RequestFromMap(env) 123 if err != nil { 124 t.Fatalf("RequestFromMap: %v", err) 125 } 126 if req.URL == nil { 127 t.Fatalf("unexpected nil URL") 128 } 129 if g, e := req.URL.String(), "http://example.com/dir/scriptname/p1/p2?a=1&b=2"; e != g { 130 t.Errorf("URL = %q; want %q", g, e) 131 } 132 } 133 134 func TestRequestWithoutRemotePort(t *testing.T) { 135 env := map[string]string{ 136 "SERVER_PROTOCOL": "HTTP/1.1", 137 "HTTP_HOST": "example.com", 138 "REQUEST_METHOD": "GET", 139 "REQUEST_URI": "/path?a=b", 140 "CONTENT_LENGTH": "123", 141 "REMOTE_ADDR": "5.6.7.8", 142 } 143 req, err := RequestFromMap(env) 144 if err != nil { 145 t.Fatalf("RequestFromMap: %v", err) 146 } 147 if e, g := "5.6.7.8:0", req.RemoteAddr; e != g { 148 t.Errorf("RemoteAddr: got %q; want %q", g, e) 149 } 150 }