github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/gmhttp/header_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  package gmhttp
     6  
     7  import (
     8  	"bytes"
     9  	"reflect"
    10  	"runtime"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/hxx258456/ccgo/internal/race"
    15  )
    16  
    17  var headerWriteTests = []struct {
    18  	h        Header
    19  	exclude  map[string]bool
    20  	expected string
    21  }{
    22  	{Header{}, nil, ""},
    23  	{
    24  		Header{
    25  			"Content-Type":   {"text/html; charset=UTF-8"},
    26  			"Content-Length": {"0"},
    27  		},
    28  		nil,
    29  		"Content-Length: 0\r\nContent-Type: text/html; charset=UTF-8\r\n",
    30  	},
    31  	{
    32  		Header{
    33  			"Content-Length": {"0", "1", "2"},
    34  		},
    35  		nil,
    36  		"Content-Length: 0\r\nContent-Length: 1\r\nContent-Length: 2\r\n",
    37  	},
    38  	{
    39  		Header{
    40  			"Expires":          {"-1"},
    41  			"Content-Length":   {"0"},
    42  			"Content-Encoding": {"gzip"},
    43  		},
    44  		map[string]bool{"Content-Length": true},
    45  		"Content-Encoding: gzip\r\nExpires: -1\r\n",
    46  	},
    47  	{
    48  		Header{
    49  			"Expires":          {"-1"},
    50  			"Content-Length":   {"0", "1", "2"},
    51  			"Content-Encoding": {"gzip"},
    52  		},
    53  		map[string]bool{"Content-Length": true},
    54  		"Content-Encoding: gzip\r\nExpires: -1\r\n",
    55  	},
    56  	{
    57  		Header{
    58  			"Expires":          {"-1"},
    59  			"Content-Length":   {"0"},
    60  			"Content-Encoding": {"gzip"},
    61  		},
    62  		map[string]bool{"Content-Length": true, "Expires": true, "Content-Encoding": true},
    63  		"",
    64  	},
    65  	{
    66  		Header{
    67  			"Nil":          nil,
    68  			"Empty":        {},
    69  			"Blank":        {""},
    70  			"Double-Blank": {"", ""},
    71  		},
    72  		nil,
    73  		"Blank: \r\nDouble-Blank: \r\nDouble-Blank: \r\n",
    74  	},
    75  	// Tests header sorting when over the insertion sort threshold side:
    76  	{
    77  		Header{
    78  			"k1": {"1a", "1b"},
    79  			"k2": {"2a", "2b"},
    80  			"k3": {"3a", "3b"},
    81  			"k4": {"4a", "4b"},
    82  			"k5": {"5a", "5b"},
    83  			"k6": {"6a", "6b"},
    84  			"k7": {"7a", "7b"},
    85  			"k8": {"8a", "8b"},
    86  			"k9": {"9a", "9b"},
    87  		},
    88  		map[string]bool{"k5": true},
    89  		"k1: 1a\r\nk1: 1b\r\nk2: 2a\r\nk2: 2b\r\nk3: 3a\r\nk3: 3b\r\n" +
    90  			"k4: 4a\r\nk4: 4b\r\nk6: 6a\r\nk6: 6b\r\n" +
    91  			"k7: 7a\r\nk7: 7b\r\nk8: 8a\r\nk8: 8b\r\nk9: 9a\r\nk9: 9b\r\n",
    92  	},
    93  }
    94  
    95  func TestHeaderWrite(t *testing.T) {
    96  	var buf bytes.Buffer
    97  	for i, test := range headerWriteTests {
    98  		test.h.WriteSubset(&buf, test.exclude)
    99  		if buf.String() != test.expected {
   100  			t.Errorf("#%d:\n got: %q\nwant: %q", i, buf.String(), test.expected)
   101  		}
   102  		buf.Reset()
   103  	}
   104  }
   105  
   106  var parseTimeTests = []struct {
   107  	h   Header
   108  	err bool
   109  }{
   110  	{Header{"Date": {""}}, true},
   111  	{Header{"Date": {"invalid"}}, true},
   112  	{Header{"Date": {"1994-11-06T08:49:37Z00:00"}}, true},
   113  	{Header{"Date": {"Sun, 06 Nov 1994 08:49:37 GMT"}}, false},
   114  	{Header{"Date": {"Sunday, 06-Nov-94 08:49:37 GMT"}}, false},
   115  	{Header{"Date": {"Sun Nov  6 08:49:37 1994"}}, false},
   116  }
   117  
   118  func TestParseTime(t *testing.T) {
   119  	expect := time.Date(1994, 11, 6, 8, 49, 37, 0, time.UTC)
   120  	for i, test := range parseTimeTests {
   121  		d, err := ParseTime(test.h.Get("Date"))
   122  		if err != nil {
   123  			if !test.err {
   124  				t.Errorf("#%d:\n got err: %v", i, err)
   125  			}
   126  			continue
   127  		}
   128  		if test.err {
   129  			t.Errorf("#%d:\n  should err", i)
   130  			continue
   131  		}
   132  		if !expect.Equal(d) {
   133  			t.Errorf("#%d:\n got: %v\nwant: %v", i, d, expect)
   134  		}
   135  	}
   136  }
   137  
   138  type hasTokenTest struct {
   139  	header string
   140  	token  string
   141  	want   bool
   142  }
   143  
   144  var hasTokenTests = []hasTokenTest{
   145  	{"", "", false},
   146  	{"", "foo", false},
   147  	{"foo", "foo", true},
   148  	{"foo ", "foo", true},
   149  	{" foo", "foo", true},
   150  	{" foo ", "foo", true},
   151  	{"foo,bar", "foo", true},
   152  	{"bar,foo", "foo", true},
   153  	{"bar, foo", "foo", true},
   154  	{"bar,foo, baz", "foo", true},
   155  	{"bar, foo,baz", "foo", true},
   156  	{"bar,foo, baz", "foo", true},
   157  	{"bar, foo, baz", "foo", true},
   158  	{"FOO", "foo", true},
   159  	{"FOO ", "foo", true},
   160  	{" FOO", "foo", true},
   161  	{" FOO ", "foo", true},
   162  	{"FOO,BAR", "foo", true},
   163  	{"BAR,FOO", "foo", true},
   164  	{"BAR, FOO", "foo", true},
   165  	{"BAR,FOO, baz", "foo", true},
   166  	{"BAR, FOO,BAZ", "foo", true},
   167  	{"BAR,FOO, BAZ", "foo", true},
   168  	{"BAR, FOO, BAZ", "foo", true},
   169  	{"foobar", "foo", false},
   170  	{"barfoo ", "foo", false},
   171  }
   172  
   173  func TestHasToken(t *testing.T) {
   174  	for _, tt := range hasTokenTests {
   175  		if hasToken(tt.header, tt.token) != tt.want {
   176  			t.Errorf("hasToken(%q, %q) = %v; want %v", tt.header, tt.token, !tt.want, tt.want)
   177  		}
   178  	}
   179  }
   180  
   181  func TestNilHeaderClone(t *testing.T) {
   182  	t1 := Header(nil)
   183  	t2 := t1.Clone()
   184  	if t2 != nil {
   185  		t.Errorf("cloned header does not match original: got: %+v; want: %+v", t2, nil)
   186  	}
   187  }
   188  
   189  var testHeader = Header{
   190  	"Content-Length": {"123"},
   191  	"Content-Type":   {"text/plain"},
   192  	"Date":           {"some date at some time Z"},
   193  	"Server":         {DefaultUserAgent},
   194  }
   195  
   196  var buf bytes.Buffer
   197  
   198  func BenchmarkHeaderWriteSubset(b *testing.B) {
   199  	b.ReportAllocs()
   200  	for i := 0; i < b.N; i++ {
   201  		buf.Reset()
   202  		testHeader.WriteSubset(&buf, nil)
   203  	}
   204  }
   205  
   206  func TestHeaderWriteSubsetAllocs(t *testing.T) {
   207  	if testing.Short() {
   208  		t.Skip("skipping alloc test in short mode")
   209  	}
   210  	if race.Enabled {
   211  		t.Skip("skipping test under race detector")
   212  	}
   213  	if runtime.GOMAXPROCS(0) > 1 {
   214  		t.Skip("skipping; GOMAXPROCS>1")
   215  	}
   216  	n := testing.AllocsPerRun(100, func() {
   217  		buf.Reset()
   218  		testHeader.WriteSubset(&buf, nil)
   219  	})
   220  	if n > 0 {
   221  		t.Errorf("allocs = %g; want 0", n)
   222  	}
   223  }
   224  
   225  // Issue 34878: test that every call to
   226  // cloneOrMakeHeader never returns a nil Header.
   227  func TestCloneOrMakeHeader(t *testing.T) {
   228  	tests := []struct {
   229  		name     string
   230  		in, want Header
   231  	}{
   232  		{"nil", nil, Header{}},
   233  		{"empty", Header{}, Header{}},
   234  		{
   235  			name: "non-empty",
   236  			in:   Header{"foo": {"bar"}},
   237  			want: Header{"foo": {"bar"}},
   238  		},
   239  	}
   240  
   241  	for _, tt := range tests {
   242  		t.Run(tt.name, func(t *testing.T) {
   243  			got := cloneOrMakeHeader(tt.in)
   244  			if got == nil {
   245  				t.Fatal("unexpected nil Header")
   246  			}
   247  			if !reflect.DeepEqual(got, tt.want) {
   248  				t.Fatalf("Got:  %#v\nWant: %#v", got, tt.want)
   249  			}
   250  			got.Add("A", "B")
   251  			got.Get("A")
   252  		})
   253  	}
   254  }