gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/gmhttp/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  	"bufio"
    11  	"bytes"
    12  	"strings"
    13  	"testing"
    14  
    15  	http "gitee.com/ks-custle/core-gm/gmhttp"
    16  	"gitee.com/ks-custle/core-gm/gmhttp/httptest"
    17  )
    18  
    19  //goland:noinspection HttpUrlsUsage
    20  func TestRequest(t *testing.T) {
    21  	env := map[string]string{
    22  		"SERVER_PROTOCOL": "HTTP/1.1",
    23  		"REQUEST_METHOD":  "GET",
    24  		"HTTP_HOST":       "example.com",
    25  		"HTTP_REFERER":    "elsewhere",
    26  		"HTTP_USER_AGENT": "goclient",
    27  		"HTTP_FOO_BAR":    "baz",
    28  		"REQUEST_URI":     "/path?a=b",
    29  		"CONTENT_LENGTH":  "123",
    30  		"CONTENT_TYPE":    "text/xml",
    31  		"REMOTE_ADDR":     "5.6.7.8",
    32  		"REMOTE_PORT":     "54321",
    33  	}
    34  	req, err := RequestFromMap(env)
    35  	if err != nil {
    36  		t.Fatalf("RequestFromMap: %v", err)
    37  	}
    38  	if g, e := req.UserAgent(), "goclient"; e != g {
    39  		t.Errorf("expected UserAgent %q; got %q", e, g)
    40  	}
    41  	if g, e := req.Method, "GET"; e != g {
    42  		t.Errorf("expected Method %q; got %q", e, g)
    43  	}
    44  	if g, e := req.Header.Get("Content-Type"), "text/xml"; e != g {
    45  		t.Errorf("expected Content-Type %q; got %q", e, g)
    46  	}
    47  	if g, e := req.ContentLength, int64(123); e != g {
    48  		t.Errorf("expected ContentLength %d; got %d", e, g)
    49  	}
    50  	if g, e := req.Referer(), "elsewhere"; e != g {
    51  		t.Errorf("expected Referer %q; got %q", e, g)
    52  	}
    53  	if req.Header == nil {
    54  		t.Fatalf("unexpected nil Header")
    55  	}
    56  	if g, e := req.Header.Get("Foo-Bar"), "baz"; e != g {
    57  		t.Errorf("expected Foo-Bar %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 nil TLS")
    70  	}
    71  	if e, g := "5.6.7.8:54321", req.RemoteAddr; e != g {
    72  		t.Errorf("RemoteAddr: got %q; want %q", g, e)
    73  	}
    74  }
    75  
    76  func TestRequestWithTLS(t *testing.T) {
    77  	env := map[string]string{
    78  		"SERVER_PROTOCOL": "HTTP/1.1",
    79  		"REQUEST_METHOD":  "GET",
    80  		"HTTP_HOST":       "example.com",
    81  		"HTTP_REFERER":    "elsewhere",
    82  		"REQUEST_URI":     "/path?a=b",
    83  		"CONTENT_TYPE":    "text/xml",
    84  		"HTTPS":           "1",
    85  		"REMOTE_ADDR":     "5.6.7.8",
    86  	}
    87  	req, err := RequestFromMap(env)
    88  	if err != nil {
    89  		t.Fatalf("RequestFromMap: %v", err)
    90  	}
    91  	if g, e := req.URL.String(), "https://example.com/path?a=b"; e != g {
    92  		t.Errorf("expected URL %q; got %q", e, g)
    93  	}
    94  	if req.TLS == nil {
    95  		t.Errorf("expected non-nil TLS")
    96  	}
    97  }
    98  
    99  func TestRequestWithoutHost(t *testing.T) {
   100  	env := map[string]string{
   101  		"SERVER_PROTOCOL": "HTTP/1.1",
   102  		"HTTP_HOST":       "",
   103  		"REQUEST_METHOD":  "GET",
   104  		"REQUEST_URI":     "/path?a=b",
   105  		"CONTENT_LENGTH":  "123",
   106  	}
   107  	req, err := RequestFromMap(env)
   108  	if err != nil {
   109  		t.Fatalf("RequestFromMap: %v", err)
   110  	}
   111  	if req.URL == nil {
   112  		t.Fatalf("unexpected nil URL")
   113  	}
   114  	if g, e := req.URL.String(), "/path?a=b"; e != g {
   115  		t.Errorf("URL = %q; want %q", g, e)
   116  	}
   117  }
   118  
   119  //goland:noinspection HttpUrlsUsage
   120  func TestRequestWithoutRequestURI(t *testing.T) {
   121  	env := map[string]string{
   122  		"SERVER_PROTOCOL": "HTTP/1.1",
   123  		"HTTP_HOST":       "example.com",
   124  		"REQUEST_METHOD":  "GET",
   125  		"SCRIPT_NAME":     "/dir/scriptname",
   126  		"PATH_INFO":       "/p1/p2",
   127  		"QUERY_STRING":    "a=1&b=2",
   128  		"CONTENT_LENGTH":  "123",
   129  	}
   130  	req, err := RequestFromMap(env)
   131  	if err != nil {
   132  		t.Fatalf("RequestFromMap: %v", err)
   133  	}
   134  	if req.URL == nil {
   135  		t.Fatalf("unexpected nil URL")
   136  	}
   137  	if g, e := req.URL.String(), "http://example.com/dir/scriptname/p1/p2?a=1&b=2"; e != g {
   138  		t.Errorf("URL = %q; want %q", g, e)
   139  	}
   140  }
   141  
   142  func TestRequestWithoutRemotePort(t *testing.T) {
   143  	env := map[string]string{
   144  		"SERVER_PROTOCOL": "HTTP/1.1",
   145  		"HTTP_HOST":       "example.com",
   146  		"REQUEST_METHOD":  "GET",
   147  		"REQUEST_URI":     "/path?a=b",
   148  		"CONTENT_LENGTH":  "123",
   149  		"REMOTE_ADDR":     "5.6.7.8",
   150  	}
   151  	req, err := RequestFromMap(env)
   152  	if err != nil {
   153  		t.Fatalf("RequestFromMap: %v", err)
   154  	}
   155  	if e, g := "5.6.7.8:0", req.RemoteAddr; e != g {
   156  		t.Errorf("RemoteAddr: got %q; want %q", g, e)
   157  	}
   158  }
   159  
   160  func TestResponse(t *testing.T) {
   161  	var tests = []struct {
   162  		name   string
   163  		body   string
   164  		wantCT string
   165  	}{
   166  		{
   167  			name:   "no body",
   168  			wantCT: "text/plain; charset=utf-8",
   169  		},
   170  		{
   171  			name:   "html",
   172  			body:   "<html><head><title>test page</title></head><body>This is a body</body></html>",
   173  			wantCT: "text/html; charset=utf-8",
   174  		},
   175  		{
   176  			name:   "text",
   177  			body:   strings.Repeat("gopher", 86),
   178  			wantCT: "text/plain; charset=utf-8",
   179  		},
   180  		{
   181  			name:   "jpg",
   182  			body:   "\xFF\xD8\xFF" + strings.Repeat("B", 1024),
   183  			wantCT: "image/jpeg",
   184  		},
   185  	}
   186  	for _, tt := range tests {
   187  		t.Run(tt.name, func(t *testing.T) {
   188  			var buf bytes.Buffer
   189  			resp := response{
   190  				req:    httptest.NewRequest("GET", "/", nil),
   191  				header: http.Header{},
   192  				bufw:   bufio.NewWriter(&buf),
   193  			}
   194  			n, err := resp.Write([]byte(tt.body))
   195  			if err != nil {
   196  				t.Errorf("Write: unexpected %v", err)
   197  			}
   198  			if want := len(tt.body); n != want {
   199  				t.Errorf("reported short Write: got %v want %v", n, want)
   200  			}
   201  			resp.writeCGIHeader(nil)
   202  			resp.Flush()
   203  			if got := resp.Header().Get("Content-Type"); got != tt.wantCT {
   204  				t.Errorf("wrong content-type: got %q, want %q", got, tt.wantCT)
   205  			}
   206  			if !bytes.HasSuffix(buf.Bytes(), []byte(tt.body)) {
   207  				t.Errorf("body was not correctly written")
   208  			}
   209  		})
   210  	}
   211  }