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