...

Source file src/github.com/lestrrat-go/httpcc/httpcc_test.go

Documentation: github.com/lestrrat-go/httpcc

     1  package httpcc_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	httpcc "github.com/lestrrat-go/httpcc"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func compareRequestDirective(t *testing.T, expected map[string]interface{}, dir *httpcc.RequestDirective) bool {
    11  	t.Helper()
    12  
    13  	for k, v := range expected {
    14  		switch k {
    15  		case httpcc.MaxAge:
    16  			got, ok := dir.MaxAge()
    17  			if !assert.True(t, ok, `dir.MaxAge() should return true`) {
    18  				return false
    19  			}
    20  
    21  			if !assert.Equal(t, v, got) {
    22  				return false
    23  			}
    24  		case httpcc.NoStore:
    25  			got := dir.NoStore()
    26  			if !assert.Equal(t, v, got) {
    27  				return false
    28  			}
    29  		case "extensions":
    30  			got := dir.Extensions()
    31  			if !assert.Equal(t, v, got) {
    32  				return false
    33  			}
    34  		default:
    35  			assert.Fail(t, `unhandled field %s`, k)
    36  			return false
    37  		}
    38  	}
    39  	return true
    40  }
    41  
    42  func compareResponseDirective(t *testing.T, expected map[string]interface{}, dir *httpcc.ResponseDirective) bool {
    43  	t.Helper()
    44  
    45  	for k, v := range expected {
    46  		switch k {
    47  		case httpcc.MaxAge:
    48  			got, ok := dir.MaxAge()
    49  			if !assert.True(t, ok, `dir.MaxAge() should return true`) {
    50  				return false
    51  			}
    52  
    53  			if !assert.Equal(t, v, got) {
    54  				return false
    55  			}
    56  		case httpcc.NoStore:
    57  			got := dir.NoStore()
    58  			if !assert.Equal(t, v, got) {
    59  				return false
    60  			}
    61  		case "extensions":
    62  			got := dir.Extensions()
    63  			if !assert.Equal(t, v, got) {
    64  				return false
    65  			}
    66  		default:
    67  			assert.Fail(t, `unhandled field %s`, k)
    68  			return false
    69  		}
    70  	}
    71  	return true
    72  }
    73  
    74  func TestParseDirective(t *testing.T) {
    75  	testcases := []struct {
    76  		Source    string
    77  		Error     bool
    78  		Expected  *httpcc.TokenPair
    79  		IsRequest bool
    80  	}{
    81  		{
    82  			Source: `no-store="foo"`,
    83  			Error:  true,
    84  		},
    85  		{
    86  			Source:   `s-maxage=4649`,
    87  			Expected: &httpcc.TokenPair{Name: `s-maxage`, Value: `4649`},
    88  		},
    89  		{
    90  			Source:    `s-maxage=4649`,
    91  			Expected:  &httpcc.TokenPair{Name: `s-maxage`, Value: `4649`},
    92  			IsRequest: true,
    93  		},
    94  		{
    95  			Source:   "no-store",
    96  			Expected: &httpcc.TokenPair{Name: "no-store"},
    97  		},
    98  		{
    99  			Source:    "max-age=4649",
   100  			Expected:  &httpcc.TokenPair{Name: "max-age", Value: "4649"},
   101  			IsRequest: true,
   102  		},
   103  		{
   104  			Source:    `max-age="4649"`,
   105  			Error:     true,
   106  			IsRequest: true,
   107  		},
   108  		{
   109  			Source:    `max-age="4649`,
   110  			Error:     true,
   111  			IsRequest: true,
   112  		},
   113  	}
   114  
   115  	for _, tc := range testcases {
   116  		tc := tc
   117  		t.Run(tc.Source, func(t *testing.T) {
   118  			var pair *httpcc.TokenPair
   119  			var err error
   120  			if tc.IsRequest {
   121  				pair, err = httpcc.ParseRequestDirective(tc.Source)
   122  			} else {
   123  				pair, err = httpcc.ParseResponseDirective(tc.Source)
   124  			}
   125  			if tc.Error {
   126  				if !assert.Error(t, err, `expected to return an error`) {
   127  					return
   128  				}
   129  			} else {
   130  				if !assert.NoError(t, err, `expected to succeed`) {
   131  					return
   132  				}
   133  				if !assert.Equal(t, tc.Expected, pair, `expected to return pair`) {
   134  					return
   135  				}
   136  			}
   137  		})
   138  	}
   139  }
   140  
   141  func TestParseDirectives(t *testing.T) {
   142  	testcases := []struct {
   143  		Source    string
   144  		Error     bool
   145  		Expected  []*httpcc.TokenPair
   146  		IsRequest bool
   147  	}{
   148  		{
   149  			Source:    `max-age=4649, no-store`,
   150  			IsRequest: true,
   151  			Expected: []*httpcc.TokenPair{
   152  				{Name: `max-age`, Value: `4649`},
   153  				{Name: `no-store`},
   154  			},
   155  		},
   156  		{
   157  			Source:    `   max-age=4649    , no-store     `,
   158  			IsRequest: true,
   159  			Expected: []*httpcc.TokenPair{
   160  				{Name: `max-age`, Value: `4649`},
   161  				{Name: `no-store`},
   162  			},
   163  		},
   164  	}
   165  	for _, tc := range testcases {
   166  		tc := tc
   167  		t.Run(tc.Source, func(t *testing.T) {
   168  			var tokens []*httpcc.TokenPair
   169  			var err error
   170  			if tc.IsRequest {
   171  				tokens, err = httpcc.ParseRequestDirectives(tc.Source)
   172  			} else {
   173  				tokens, err = httpcc.ParseResponseDirectives(tc.Source)
   174  			}
   175  			if tc.Error {
   176  				if !assert.Error(t, err, `expected to return an error`) {
   177  					return
   178  				}
   179  			} else {
   180  				if !assert.NoError(t, err, `expected to succeed`) {
   181  					return
   182  				}
   183  				if !assert.Equal(t, tc.Expected, tokens, `expected to return list of tokens`) {
   184  					return
   185  				}
   186  			}
   187  		})
   188  	}
   189  }
   190  
   191  func TestParseRequest(t *testing.T) {
   192  	testcases := []struct {
   193  		Source   string
   194  		Error    bool
   195  		Expected map[string]interface{}
   196  	}{
   197  		{
   198  			Source: `max-age=4649, no-store`,
   199  			Expected: map[string]interface{}{
   200  				httpcc.MaxAge:  uint64(4649),
   201  				httpcc.NoStore: true,
   202  			},
   203  		},
   204  		{
   205  			Source: `max-age="4649"`,
   206  			Error:  true,
   207  		},
   208  	}
   209  	for _, tc := range testcases {
   210  		tc := tc
   211  		t.Run(tc.Source, func(t *testing.T) {
   212  			dir, err := httpcc.ParseRequest(tc.Source)
   213  			if tc.Error {
   214  				if !assert.Error(t, err, `expected to return an error`) {
   215  					return
   216  				}
   217  			} else {
   218  				if !assert.NoError(t, err, `expected to succeed`) {
   219  					return
   220  				}
   221  				if !compareRequestDirective(t, tc.Expected, dir) {
   222  					return
   223  				}
   224  			}
   225  		})
   226  	}
   227  }
   228  
   229  func TestParseResponse(t *testing.T) {
   230  	testcases := []struct {
   231  		Source   string
   232  		Error    bool
   233  		Expected map[string]interface{}
   234  	}{
   235  		{
   236  			Source: `max-age=4649, no-store, community="UCI"`,
   237  			Expected: map[string]interface{}{
   238  				httpcc.MaxAge:  uint64(4649),
   239  				httpcc.NoStore: true,
   240  				"extensions": map[string]string{
   241  					"community": "UCI",
   242  				},
   243  			},
   244  		},
   245  		{
   246  			Source: `max-age="4649"`,
   247  			Error:  true,
   248  		},
   249  	}
   250  	for _, tc := range testcases {
   251  		tc := tc
   252  		t.Run(tc.Source, func(t *testing.T) {
   253  			dir, err := httpcc.ParseResponse(tc.Source)
   254  			if tc.Error {
   255  				if !assert.Error(t, err, `expected to return an error`) {
   256  					return
   257  				}
   258  			} else {
   259  				if !assert.NoError(t, err, `expected to succeed`) {
   260  					return
   261  				}
   262  				if !compareResponseDirective(t, tc.Expected, dir) {
   263  					return
   264  				}
   265  			}
   266  		})
   267  	}
   268  }
   269  

View as plain text