gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/gmhttp/httptest/httptest_test.go (about)

     1  // Copyright 2016 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  package httptest
     6  
     7  import (
     8  	"io"
     9  	"net/url"
    10  	"reflect"
    11  	"strings"
    12  	"testing"
    13  
    14  	http "gitee.com/ks-custle/core-gm/gmhttp"
    15  	tls "gitee.com/ks-custle/core-gm/gmtls"
    16  )
    17  
    18  func TestNewRequest(t *testing.T) {
    19  	//goland:noinspection HttpUrlsUsage
    20  	for _, tt := range [...]struct {
    21  		name string
    22  
    23  		method, uri string
    24  		body        io.Reader
    25  
    26  		want     *http.Request
    27  		wantBody string
    28  	}{
    29  		{
    30  			name:   "Empty method means GET",
    31  			method: "",
    32  			uri:    "/",
    33  			body:   nil,
    34  			want: &http.Request{
    35  				Method:     "GET",
    36  				Host:       "example.com",
    37  				URL:        &url.URL{Path: "/"},
    38  				Header:     http.Header{},
    39  				Proto:      "HTTP/1.1",
    40  				ProtoMajor: 1,
    41  				ProtoMinor: 1,
    42  				RemoteAddr: "192.0.2.1:1234",
    43  				RequestURI: "/",
    44  			},
    45  			wantBody: "",
    46  		},
    47  
    48  		{
    49  			name:   "GET with full URL",
    50  			method: "GET",
    51  			uri:    "http://foo.com/path/%2f/bar/",
    52  			body:   nil,
    53  			want: &http.Request{
    54  				Method: "GET",
    55  				Host:   "foo.com",
    56  				URL: &url.URL{
    57  					Scheme:  "http",
    58  					Path:    "/path///bar/",
    59  					RawPath: "/path/%2f/bar/",
    60  					Host:    "foo.com",
    61  				},
    62  				Header:     http.Header{},
    63  				Proto:      "HTTP/1.1",
    64  				ProtoMajor: 1,
    65  				ProtoMinor: 1,
    66  				RemoteAddr: "192.0.2.1:1234",
    67  				RequestURI: "http://foo.com/path/%2f/bar/",
    68  			},
    69  			wantBody: "",
    70  		},
    71  
    72  		{
    73  			name:   "GET with full https URL",
    74  			method: "GET",
    75  			uri:    "https://foo.com/path/",
    76  			body:   nil,
    77  			want: &http.Request{
    78  				Method: "GET",
    79  				Host:   "foo.com",
    80  				URL: &url.URL{
    81  					Scheme: "https",
    82  					Path:   "/path/",
    83  					Host:   "foo.com",
    84  				},
    85  				Header:     http.Header{},
    86  				Proto:      "HTTP/1.1",
    87  				ProtoMajor: 1,
    88  				ProtoMinor: 1,
    89  				RemoteAddr: "192.0.2.1:1234",
    90  				RequestURI: "https://foo.com/path/",
    91  				TLS: &tls.ConnectionState{
    92  					Version:           tls.VersionTLS12,
    93  					HandshakeComplete: true,
    94  					ServerName:        "foo.com",
    95  				},
    96  			},
    97  			wantBody: "",
    98  		},
    99  
   100  		{
   101  			name:   "Post with known length",
   102  			method: "POST",
   103  			uri:    "/",
   104  			body:   strings.NewReader("foo"),
   105  			want: &http.Request{
   106  				Method:        "POST",
   107  				Host:          "example.com",
   108  				URL:           &url.URL{Path: "/"},
   109  				Header:        http.Header{},
   110  				Proto:         "HTTP/1.1",
   111  				ContentLength: 3,
   112  				ProtoMajor:    1,
   113  				ProtoMinor:    1,
   114  				RemoteAddr:    "192.0.2.1:1234",
   115  				RequestURI:    "/",
   116  			},
   117  			wantBody: "foo",
   118  		},
   119  
   120  		{
   121  			name:   "Post with unknown length",
   122  			method: "POST",
   123  			uri:    "/",
   124  			body:   struct{ io.Reader }{strings.NewReader("foo")},
   125  			want: &http.Request{
   126  				Method:        "POST",
   127  				Host:          "example.com",
   128  				URL:           &url.URL{Path: "/"},
   129  				Header:        http.Header{},
   130  				Proto:         "HTTP/1.1",
   131  				ContentLength: -1,
   132  				ProtoMajor:    1,
   133  				ProtoMinor:    1,
   134  				RemoteAddr:    "192.0.2.1:1234",
   135  				RequestURI:    "/",
   136  			},
   137  			wantBody: "foo",
   138  		},
   139  
   140  		{
   141  			name:   "OPTIONS *",
   142  			method: "OPTIONS",
   143  			uri:    "*",
   144  			want: &http.Request{
   145  				Method:     "OPTIONS",
   146  				Host:       "example.com",
   147  				URL:        &url.URL{Path: "*"},
   148  				Header:     http.Header{},
   149  				Proto:      "HTTP/1.1",
   150  				ProtoMajor: 1,
   151  				ProtoMinor: 1,
   152  				RemoteAddr: "192.0.2.1:1234",
   153  				RequestURI: "*",
   154  			},
   155  		},
   156  	} {
   157  		t.Run(tt.name, func(t *testing.T) {
   158  			got := NewRequest(tt.method, tt.uri, tt.body)
   159  			slurp, err := io.ReadAll(got.Body)
   160  			if err != nil {
   161  				t.Errorf("ReadAll: %v", err)
   162  			}
   163  			if string(slurp) != tt.wantBody {
   164  				t.Errorf("Body = %q; want %q", slurp, tt.wantBody)
   165  			}
   166  			got.Body = nil // before DeepEqual
   167  			if !reflect.DeepEqual(got.URL, tt.want.URL) {
   168  				t.Errorf("Request.URL mismatch:\n got: %#v\nwant: %#v", got.URL, tt.want.URL)
   169  			}
   170  			if !reflect.DeepEqual(got.Header, tt.want.Header) {
   171  				t.Errorf("Request.Header mismatch:\n got: %#v\nwant: %#v", got.Header, tt.want.Header)
   172  			}
   173  			if !reflect.DeepEqual(got.TLS, tt.want.TLS) {
   174  				t.Errorf("Request.TLS mismatch:\n got: %#v\nwant: %#v", got.TLS, tt.want.TLS)
   175  			}
   176  			if !reflect.DeepEqual(got, tt.want) {
   177  				t.Errorf("Request mismatch:\n got: %#v\nwant: %#v", got, tt.want)
   178  			}
   179  		})
   180  	}
   181  }