...
Run Format

Source file src/net/http/httptest/httptest_test.go

Documentation: net/http/httptest

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

View as plain text