github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/src/net/http/lex_test.go (about)

     1  // Copyright 2009 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 http
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func isChar(c rune) bool { return c <= 127 }
    12  
    13  func isCtl(c rune) bool { return c <= 31 || c == 127 }
    14  
    15  func isSeparator(c rune) bool {
    16  	switch c {
    17  	case '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t':
    18  		return true
    19  	}
    20  	return false
    21  }
    22  
    23  func TestIsToken(t *testing.T) {
    24  	for i := 0; i <= 130; i++ {
    25  		r := rune(i)
    26  		expected := isChar(r) && !isCtl(r) && !isSeparator(r)
    27  		if isToken(r) != expected {
    28  			t.Errorf("isToken(0x%x) = %v", r, !expected)
    29  		}
    30  	}
    31  }
    32  
    33  func TestHeaderValuesContainsToken(t *testing.T) {
    34  	tests := []struct {
    35  		vals  []string
    36  		token string
    37  		want  bool
    38  	}{
    39  		{
    40  			vals:  []string{"foo"},
    41  			token: "foo",
    42  			want:  true,
    43  		},
    44  		{
    45  			vals:  []string{"bar", "foo"},
    46  			token: "foo",
    47  			want:  true,
    48  		},
    49  		{
    50  			vals:  []string{"foo"},
    51  			token: "FOO",
    52  			want:  true,
    53  		},
    54  		{
    55  			vals:  []string{"foo"},
    56  			token: "bar",
    57  			want:  false,
    58  		},
    59  		{
    60  			vals:  []string{" foo "},
    61  			token: "FOO",
    62  			want:  true,
    63  		},
    64  		{
    65  			vals:  []string{"foo,bar"},
    66  			token: "FOO",
    67  			want:  true,
    68  		},
    69  		{
    70  			vals:  []string{"bar,foo,bar"},
    71  			token: "FOO",
    72  			want:  true,
    73  		},
    74  		{
    75  			vals:  []string{"bar , foo"},
    76  			token: "FOO",
    77  			want:  true,
    78  		},
    79  		{
    80  			vals:  []string{"foo ,bar "},
    81  			token: "FOO",
    82  			want:  true,
    83  		},
    84  		{
    85  			vals:  []string{"bar, foo ,bar"},
    86  			token: "FOO",
    87  			want:  true,
    88  		},
    89  		{
    90  			vals:  []string{"bar , foo"},
    91  			token: "FOO",
    92  			want:  true,
    93  		},
    94  	}
    95  	for _, tt := range tests {
    96  		got := headerValuesContainsToken(tt.vals, tt.token)
    97  		if got != tt.want {
    98  			t.Errorf("headerValuesContainsToken(%q, %q) = %v; want %v", tt.vals, tt.token, got, tt.want)
    99  		}
   100  	}
   101  }