github.com/cloudwego/hertz@v0.9.3/pkg/protocol/args_test.go (about) 1 /* 2 * Copyright 2022 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * The MIT License (MIT) 17 * 18 * Copyright (c) 2015-present Aliaksandr Valialkin, VertaMedia, Kirill Danshin, Erik Dubbelboer, FastHTTP Authors 19 * 20 * Permission is hereby granted, free of charge, to any person obtaining a copy 21 * of this software and associated documentation files (the "Software"), to deal 22 * in the Software without restriction, including without limitation the rights 23 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 24 * copies of the Software, and to permit persons to whom the Software is 25 * furnished to do so, subject to the following conditions: 26 * 27 * The above copyright notice and this permission notice shall be included in 28 * all copies or substantial portions of the Software. 29 * 30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 33 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 35 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 36 * THE SOFTWARE. 37 * 38 * This file may have been modified by CloudWeGo authors. All CloudWeGo 39 * Modifications are Copyright 2022 CloudWeGo Authors. 40 */ 41 42 package protocol 43 44 import ( 45 "testing" 46 47 "github.com/cloudwego/hertz/pkg/common/test/assert" 48 ) 49 50 func TestArgsDeleteAll(t *testing.T) { 51 t.Parallel() 52 var a Args 53 a.Add("q1", "foo") 54 a.Add("q1", "bar") 55 a.Add("q1", "baz") 56 a.Add("q1", "quux") 57 a.Add("q2", "1234") 58 a.Del("q1") 59 if a.Len() != 1 || a.Has("q1") { 60 t.Fatalf("Expected q1 arg to be completely deleted. Current Args: %s", a.String()) 61 } 62 } 63 64 func TestArgsBytesOperation(t *testing.T) { 65 var a Args 66 a.Add("q1", "foo") 67 a.Add("q2", "bar") 68 setArgBytes(a.args, a.args[0].key, a.args[0].value, false) 69 assert.DeepEqual(t, []byte("foo"), peekArgBytes(a.args, []byte("q1"))) 70 setArgBytes(a.args, a.args[1].key, a.args[1].value, true) 71 assert.DeepEqual(t, []byte(""), peekArgBytes(a.args, []byte("q2"))) 72 } 73 74 func TestArgsPeekExists(t *testing.T) { 75 var a Args 76 a.Add("q1", "foo") 77 a.Add("", "") 78 a.Add("?", "=") 79 v1, b1 := a.PeekExists("q1") 80 assert.DeepEqual(t, []byte("foo"), []byte(v1)) 81 assert.True(t, b1) 82 v2, b2 := a.PeekExists("") 83 assert.DeepEqual(t, []byte(""), []byte(v2)) 84 assert.True(t, b2) 85 v3, b3 := a.PeekExists("q3") 86 assert.DeepEqual(t, "", v3) 87 assert.False(t, b3) 88 v4, b4 := a.PeekExists("?") 89 assert.DeepEqual(t, "=", v4) 90 assert.True(t, b4) 91 } 92 93 func TestSetArg(t *testing.T) { 94 a := Args{args: setArg(nil, "q1", "foo", true)} 95 a.Add("", "") 96 setArgBytes(a.args, []byte("q3"), []byte("bar"), false) 97 s := a.String() 98 assert.DeepEqual(t, []byte("q1&="), []byte(s)) 99 } 100 101 // Test the encoding of special parameters 102 func TestArgsParseBytes(t *testing.T) { 103 var ta1 Args 104 ta1.Add("q1", "foo") 105 ta1.Add("q1", "bar") 106 ta1.Add("q2", "123") 107 ta1.Add("q3", "") 108 var a1 Args 109 a1.ParseBytes([]byte("q1=foo&q1=bar&q2=123&q3=")) 110 assert.DeepEqual(t, &ta1, &a1) 111 112 var ta2 Args 113 ta2.Add("?", "foo") 114 ta2.Add("&", "bar") 115 ta2.Add("&", "?") 116 ta2.Add("=", "=") 117 var a2 Args 118 a2.ParseBytes([]byte("%3F=foo&%26=bar&%26=%3F&%3D=%3D")) 119 assert.DeepEqual(t, &ta2, &a2) 120 } 121 122 func TestArgsVisitAll(t *testing.T) { 123 var a Args 124 var s []string 125 a.Add("cloudwego", "hertz") 126 a.Add("hello", "world") 127 a.VisitAll(func(key, value []byte) { 128 s = append(s, string(key), string(value)) 129 }) 130 assert.DeepEqual(t, []string{"cloudwego", "hertz", "hello", "world"}, s) 131 } 132 133 func TestArgsPeekMulti(t *testing.T) { 134 var a Args 135 a.Add("cloudwego", "hertz") 136 a.Add("cloudwego", "kitex") 137 a.Add("cloudwego", "") 138 a.Add("hello", "world") 139 140 vv := a.PeekAll("cloudwego") 141 expectedVV := [][]byte{ 142 []byte("hertz"), 143 []byte("kitex"), 144 []byte(nil), 145 } 146 assert.DeepEqual(t, expectedVV, vv) 147 148 vv = a.PeekAll("aaaa") 149 assert.DeepEqual(t, 0, len(vv)) 150 151 vv = a.PeekAll("hello") 152 expectedVV = [][]byte{[]byte("world")} 153 assert.DeepEqual(t, expectedVV, vv) 154 }