...

Source file src/github.com/aws/smithy-go/transport/http/headerlist_test.go

Documentation: github.com/aws/smithy-go/transport/http

     1  package http
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestSplitHeaderListValues(t *testing.T) {
    10  	cases := map[string]struct {
    11  		Values    []string
    12  		Expect    []string
    13  		ExpectErr string
    14  	}{
    15  		"no split": {
    16  			Values: []string{
    17  				"abc", "123", "hello",
    18  			},
    19  			Expect: []string{
    20  				"abc", "123", "hello",
    21  			},
    22  		},
    23  		"with split": {
    24  			Values: []string{
    25  				"a, b, c, 1, 2, 3",
    26  			},
    27  			Expect: []string{
    28  				"a", "b", "c", "1", "2", "3",
    29  			},
    30  		},
    31  		"mixed with split": {
    32  			Values: []string{
    33  				"abc", "1, 23", "hello,world",
    34  			},
    35  			Expect: []string{
    36  				"abc", "1", "23", "hello", "world",
    37  			},
    38  		},
    39  		"empty values": {
    40  			Values: []string{
    41  				"",
    42  				", 1, 23, hello,world",
    43  			},
    44  			Expect: []string{
    45  				"", "", "1", "23", "hello", "world",
    46  			},
    47  		},
    48  		"quoted values": {
    49  			Values: []string{
    50  				`abc, 123, "abc,123", "456,efg"`,
    51  			},
    52  			Expect: []string{
    53  				"abc",
    54  				"123",
    55  				"abc,123",
    56  				"456,efg",
    57  			},
    58  		},
    59  		"quoted escaped values": {
    60  			Values: []string{
    61  				`abc,123, "abc,123"   ,   "   \"abc , 123\"  " , "\\456,efg\\b"  ,`,
    62  			},
    63  			Expect: []string{
    64  				"abc",
    65  				"123",
    66  				"abc,123",
    67  				"   \"abc , 123\"  ",
    68  				"\\456,efg\\b",
    69  				"",
    70  			},
    71  		},
    72  		"wrapping space": {
    73  			Values: []string{
    74  				`   abc,123, "abc,123"   ,   "   \"abc , 123\"  " , "\\456,efg\\b"  ,`,
    75  			},
    76  			Expect: []string{
    77  				"abc",
    78  				"123",
    79  				"abc,123",
    80  				"   \"abc , 123\"  ",
    81  				"\\456,efg\\b",
    82  				"",
    83  			},
    84  		},
    85  		"trailing empty value": {
    86  			Values: []string{
    87  				`, , `,
    88  			},
    89  			Expect: []string{
    90  				"", "", "",
    91  			},
    92  		},
    93  	}
    94  
    95  	for name, c := range cases {
    96  		t.Run(name, func(t *testing.T) {
    97  			actual, err := SplitHeaderListValues(c.Values)
    98  			if err != nil {
    99  				t.Fatalf("expect no error, %v", err)
   100  			}
   101  
   102  			if !reflect.DeepEqual(c.Expect, actual) {
   103  				t.Errorf("%v != %v", c.Expect, actual)
   104  			}
   105  		})
   106  	}
   107  }
   108  
   109  func TestSplitHTTPDateTimestampHeaderListValues(t *testing.T) {
   110  	cases := map[string]struct {
   111  		Values    []string
   112  		Expect    []string
   113  		ExpectErr string
   114  	}{
   115  		"no split": {
   116  			Values: []string{
   117  				"Mon, 16 Dec 2019 23:48:18 GMT",
   118  			},
   119  			Expect: []string{
   120  				"Mon, 16 Dec 2019 23:48:18 GMT",
   121  			},
   122  		},
   123  		"with split": {
   124  			Values: []string{
   125  				"Mon, 16 Dec 2019 23:48:18 GMT, Tue, 17 Dec 2019 23:48:18 GMT",
   126  			},
   127  			Expect: []string{
   128  				"Mon, 16 Dec 2019 23:48:18 GMT",
   129  				"Tue, 17 Dec 2019 23:48:18 GMT",
   130  			},
   131  		},
   132  		"mixed with split": {
   133  			Values: []string{
   134  				"Sun, 15 Dec 2019 23:48:18 GMT",
   135  				"Mon, 16 Dec 2019 23:48:18 GMT, Tue, 17 Dec 2019 23:48:18 GMT",
   136  				"Wed, 18 Dec 2019 23:48:18 GMT",
   137  			},
   138  			Expect: []string{
   139  				"Sun, 15 Dec 2019 23:48:18 GMT",
   140  				"Mon, 16 Dec 2019 23:48:18 GMT",
   141  				"Tue, 17 Dec 2019 23:48:18 GMT",
   142  				"Wed, 18 Dec 2019 23:48:18 GMT",
   143  			},
   144  		},
   145  		"empty values": {
   146  			Values: []string{
   147  				"",
   148  				"Mon, 16 Dec 2019 23:48:18 GMT, Tue, 17 Dec 2019 23:48:18 GMT",
   149  				"Wed, 18 Dec 2019 23:48:18 GMT",
   150  			},
   151  			Expect: []string{
   152  				"",
   153  				"Mon, 16 Dec 2019 23:48:18 GMT",
   154  				"Tue, 17 Dec 2019 23:48:18 GMT",
   155  				"Wed, 18 Dec 2019 23:48:18 GMT",
   156  			},
   157  		},
   158  		"bad format": {
   159  			Values: []string{
   160  				"Mon, 16 Dec 2019 23:48:18 GMT, , Tue, 17 Dec 2019 23:48:18 GMT",
   161  			},
   162  			ExpectErr: "invalid timestamp",
   163  		},
   164  	}
   165  
   166  	for name, c := range cases {
   167  		t.Run(name, func(t *testing.T) {
   168  			actual, err := SplitHTTPDateTimestampHeaderListValues(c.Values)
   169  			if len(c.ExpectErr) != 0 {
   170  				if err == nil {
   171  					t.Fatalf("expect error, got none")
   172  				}
   173  				if e, a := c.ExpectErr, err.Error(); !strings.Contains(a, e) {
   174  					t.Fatalf("expect error to contain %v, got %v", e, a)
   175  				}
   176  				return
   177  			}
   178  			if err != nil {
   179  				t.Fatalf("expect no error, got %v", err)
   180  			}
   181  
   182  			if !reflect.DeepEqual(c.Expect, actual) {
   183  				t.Errorf("%v != %v", c.Expect, actual)
   184  			}
   185  		})
   186  	}
   187  }
   188  

View as plain text