github.com/cloudwego/hertz@v0.9.3/pkg/common/utils/utils_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 utils 43 44 import ( 45 "testing" 46 47 "github.com/cloudwego/hertz/pkg/common/test/assert" 48 ) 49 50 // test assert func 51 func TestUtilsAssert(t *testing.T) { 52 assertPanic := func() (panicked bool) { 53 defer func() { 54 if r := recover(); r != nil { 55 panicked = true 56 } 57 }() 58 Assert(false, "should panic") 59 return false 60 } 61 62 // Checking if the assertPanic function results in a panic as expected. 63 // We expect a true value because it should panic. 64 assert.DeepEqual(t, true, assertPanic()) 65 66 // Checking if a true assertion does not result in a panic. 67 // We create a wrapper around Assert to capture if it panics when it should not. 68 noPanic := func() (panicked bool) { 69 defer func() { 70 if r := recover(); r != nil { 71 panicked = true 72 } 73 }() 74 Assert(true, "should not panic") 75 return false 76 } 77 78 // We expect a false value because it should not panic. 79 assert.DeepEqual(t, false, noPanic()) 80 } 81 82 func TestUtilsIsTrueString(t *testing.T) { 83 normalTrueStr := "true" 84 upperTrueStr := "trUe" 85 otherStr := "hertz" 86 87 assert.DeepEqual(t, true, IsTrueString(normalTrueStr)) 88 assert.DeepEqual(t, true, IsTrueString(upperTrueStr)) 89 assert.DeepEqual(t, false, IsTrueString(otherStr)) 90 } 91 92 // used for TestUtilsNameOfFunction 93 func testName(a int) { 94 } 95 96 // return the relative path for the function 97 func TestUtilsNameOfFunction(t *testing.T) { 98 pathOfTestName := "github.com/cloudwego/hertz/pkg/common/utils.testName" 99 pathOfIsTrueString := "github.com/cloudwego/hertz/pkg/common/utils.IsTrueString" 100 nameOfTestName := NameOfFunction(testName) 101 nameOfIsTrueString := NameOfFunction(IsTrueString) 102 103 assert.DeepEqual(t, pathOfTestName, nameOfTestName) 104 assert.DeepEqual(t, pathOfIsTrueString, nameOfIsTrueString) 105 } 106 107 func TestUtilsCaseInsensitiveCompare(t *testing.T) { 108 lowerStr := []byte("content-length") 109 upperStr := []byte("Content-Length") 110 assert.DeepEqual(t, true, CaseInsensitiveCompare(lowerStr, upperStr)) 111 112 lessStr := []byte("content-type") 113 moreStr := []byte("content-length") 114 assert.DeepEqual(t, false, CaseInsensitiveCompare(lessStr, moreStr)) 115 116 firstStr := []byte("content-type") 117 secondStr := []byte("contant-type") 118 assert.DeepEqual(t, false, CaseInsensitiveCompare(firstStr, secondStr)) 119 } 120 121 // NormalizeHeaderKey can upper the first letter and lower the other letter in 122 // HTTP header, invervaled by '-'. 123 // Example: "content-type" -> "Content-Type" 124 func TestUtilsNormalizeHeaderKey(t *testing.T) { 125 contentTypeStr := []byte("Content-Type") 126 lowerContentTypeStr := []byte("content-type") 127 mixedContentTypeStr := []byte("conTENt-tYpE") 128 mixedContertTypeStrWithoutNormalizing := []byte("Content-type") 129 NormalizeHeaderKey(contentTypeStr, false) 130 NormalizeHeaderKey(lowerContentTypeStr, false) 131 NormalizeHeaderKey(mixedContentTypeStr, false) 132 NormalizeHeaderKey(lowerContentTypeStr, true) 133 134 assert.DeepEqual(t, "Content-Type", string(contentTypeStr)) 135 assert.DeepEqual(t, "Content-Type", string(lowerContentTypeStr)) 136 assert.DeepEqual(t, "Content-Type", string(mixedContentTypeStr)) 137 assert.DeepEqual(t, "Content-type", string(mixedContertTypeStrWithoutNormalizing)) 138 } 139 140 // Cutting up the header Type. 141 // Example: "Content-Type: application/x-www-form-urlencoded\r\nDate: Fri, 6 Aug 2021 11:00:31 GMT" 142 // ->"Content-Type: application/x-www-form-urlencoded" and "Date: Fri, 6 Aug 2021 11:00:31 GMT" 143 func TestUtilsNextLine(t *testing.T) { 144 multiHeaderStr := []byte("Content-Type: application/x-www-form-urlencoded\r\nDate: Fri, 6 Aug 2021 11:00:31 GMT") 145 contentTypeStr, dateStr, hErr := NextLine(multiHeaderStr) 146 assert.DeepEqual(t, nil, hErr) 147 assert.DeepEqual(t, "Content-Type: application/x-www-form-urlencoded", string(contentTypeStr)) 148 assert.DeepEqual(t, "Date: Fri, 6 Aug 2021 11:00:31 GMT", string(dateStr)) 149 150 multiHeaderStrWithoutReturn := []byte("Content-Type: application/x-www-form-urlencoded\nDate: Fri, 6 Aug 2021 11:00:31 GMT") 151 contentTypeStr, dateStr, hErr = NextLine(multiHeaderStrWithoutReturn) 152 assert.DeepEqual(t, nil, hErr) 153 assert.DeepEqual(t, "Content-Type: application/x-www-form-urlencoded", string(contentTypeStr)) 154 assert.DeepEqual(t, "Date: Fri, 6 Aug 2021 11:00:31 GMT", string(dateStr)) 155 156 singleHeaderStrWithFirstNewLine := []byte("\nContent-Type: application/x-www-form-urlencoded") 157 firstStr, secondStr, sErr := NextLine(singleHeaderStrWithFirstNewLine) 158 assert.DeepEqual(t, nil, sErr) 159 assert.DeepEqual(t, string(""), string(firstStr)) 160 assert.DeepEqual(t, "Content-Type: application/x-www-form-urlencoded", string(secondStr)) 161 162 singleHeaderStr := []byte("Content-Type: application/x-www-form-urlencoded") 163 _, _, sErr = NextLine(singleHeaderStr) 164 assert.DeepEqual(t, errNeedMore, sErr) 165 } 166 167 func TestFilterContentType(t *testing.T) { 168 contentType := "text/plain; charset=utf-8" 169 contentType = FilterContentType(contentType) 170 assert.DeepEqual(t, "text/plain", contentType) 171 } 172 173 func TestNormalizeHeaderKeyEdgeCases(t *testing.T) { 174 empty := []byte("") 175 NormalizeHeaderKey(empty, false) 176 assert.DeepEqual(t, []byte(""), empty) 177 NormalizeHeaderKey(empty, true) 178 assert.DeepEqual(t, []byte(""), empty) 179 } 180 181 func TestFilterContentTypeEdgeCases(t *testing.T) { 182 simpleContentType := "text/plain" 183 assert.DeepEqual(t, "text/plain", FilterContentType(simpleContentType)) 184 complexContentType := "text/html; charset=utf-8; format=flowed" 185 assert.DeepEqual(t, "text/html", FilterContentType(complexContentType)) 186 }