1 package httpencoding 2 3 import ( 4 "testing" 5 6 "vimagination.zapto.org/parser" 7 ) 8 9 func TestTokeniser(t *testing.T) { 10 for n, test := range [...]struct { 11 Accept string 12 Tokens []parser.Token 13 }{ 14 { 15 "", 16 []parser.Token{ 17 {Type: parser.TokenDone, Data: ""}, 18 }, 19 }, 20 { 21 "a", 22 []parser.Token{ 23 {Type: tokenCoding, Data: "a"}, 24 {Type: parser.TokenDone, Data: ""}, 25 }, 26 }, 27 { 28 "a,b", 29 []parser.Token{ 30 {Type: tokenCoding, Data: "a"}, 31 {Type: tokenCoding, Data: "b"}, 32 {Type: parser.TokenDone, Data: ""}, 33 }, 34 }, 35 { 36 "a\t , \tb", 37 []parser.Token{ 38 {Type: tokenCoding, Data: "a"}, 39 {Type: tokenCoding, Data: "b"}, 40 {Type: parser.TokenDone, Data: ""}, 41 }, 42 }, 43 { 44 "a;q=1", 45 []parser.Token{ 46 {Type: tokenCoding, Data: "a"}, 47 {Type: tokenWeight, Data: "1"}, 48 {Type: parser.TokenDone, Data: ""}, 49 }, 50 }, 51 { 52 "a ; q=1", 53 []parser.Token{ 54 {Type: tokenCoding, Data: "a"}, 55 {Type: tokenWeight, Data: "1"}, 56 {Type: parser.TokenDone, Data: ""}, 57 }, 58 }, 59 { 60 "a ; q=1.", 61 []parser.Token{ 62 {Type: tokenCoding, Data: "a"}, 63 {Type: tokenWeight, Data: "1."}, 64 {Type: parser.TokenDone, Data: ""}, 65 }, 66 }, 67 { 68 "a ; q=1.0", 69 []parser.Token{ 70 {Type: tokenCoding, Data: "a"}, 71 {Type: tokenWeight, Data: "1.0"}, 72 {Type: parser.TokenDone, Data: ""}, 73 }, 74 }, 75 { 76 "a ; q=1.000", 77 []parser.Token{ 78 {Type: tokenCoding, Data: "a"}, 79 {Type: tokenWeight, Data: "1.000"}, 80 {Type: parser.TokenDone, Data: ""}, 81 }, 82 }, 83 { 84 "a ; q=1.0000", 85 []parser.Token{ 86 {Type: tokenCoding, Data: "a"}, 87 {Type: tokenInvalidWeight, Data: ""}, 88 {Type: parser.TokenDone, Data: ""}, 89 }, 90 }, 91 { 92 "a ; q=1.100", 93 []parser.Token{ 94 {Type: tokenCoding, Data: "a"}, 95 {Type: tokenInvalidWeight, Data: ""}, 96 {Type: parser.TokenDone, Data: ""}, 97 }, 98 }, 99 { 100 "a ; q=2", 101 []parser.Token{ 102 {Type: tokenCoding, Data: "a"}, 103 {Type: tokenInvalidWeight, Data: "2"}, 104 {Type: parser.TokenDone, Data: ""}, 105 }, 106 }, 107 { 108 "a ; q=2", 109 []parser.Token{ 110 {Type: tokenCoding, Data: "a"}, 111 {Type: tokenInvalidWeight, Data: "2"}, 112 {Type: parser.TokenDone, Data: ""}, 113 }, 114 }, 115 { 116 "a ; q=0", 117 []parser.Token{ 118 {Type: tokenCoding, Data: "a"}, 119 {Type: tokenWeight, Data: "0"}, 120 {Type: parser.TokenDone, Data: ""}, 121 }, 122 }, 123 { 124 "a ; q=0.123", 125 []parser.Token{ 126 {Type: tokenCoding, Data: "a"}, 127 {Type: tokenWeight, Data: "0.123"}, 128 {Type: parser.TokenDone, Data: ""}, 129 }, 130 }, 131 { 132 "a ; q=0.1234", 133 []parser.Token{ 134 {Type: tokenCoding, Data: "a"}, 135 {Type: tokenInvalidWeight, Data: ""}, 136 {Type: parser.TokenDone, Data: ""}, 137 }, 138 }, 139 { 140 "a ; q=0.a", 141 []parser.Token{ 142 {Type: tokenCoding, Data: "a"}, 143 {Type: tokenInvalidWeight, Data: ""}, 144 {Type: parser.TokenDone, Data: ""}, 145 }, 146 }, 147 { 148 "*;q=0,identity;q=0.512", 149 []parser.Token{ 150 {Type: tokenCoding, Data: "*"}, 151 {Type: tokenWeight, Data: "0"}, 152 {Type: tokenCoding, Data: "identity"}, 153 {Type: tokenWeight, Data: "0.512"}, 154 {Type: parser.TokenDone, Data: ""}, 155 }, 156 }, 157 } { 158 p := parseAccept(test.Accept) 159 160 for m, tkn := range test.Tokens { 161 if tk, _ := p.GetToken(); tk.Type != tkn.Type { 162 if tk.Type == parser.TokenError { 163 t.Errorf("test %d.%d: unexpected error: %s", n+1, m+1, tk.Data) 164 } else { 165 t.Errorf("test %d.%d: Incorrect type, expecting %d, got %d", n+1, m+1, tkn.Type, tk.Type) 166 } 167 168 break 169 } else if tk.Data != tkn.Data { 170 t.Errorf("test %d.%d: Incorrect data, expecting %q, got %q", n+1, m+1, tkn.Data, tk.Data) 171 172 break 173 } 174 } 175 } 176 } 177