| 1 | // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️ |
| 2 | // 🤖 Github Repository: https://github.com/gofiber/fiber |
| 3 | // 📌 API Documentation: https://docs.gofiber.io |
| 4 | |
| 5 | package utils |
| 6 | |
| 7 | import ( |
| 8 | "mime" |
| 9 | "net/http" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/stretchr/testify/require" |
| 13 | ) |
| 14 | |
| 15 | func Test_GetMIME(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | res := GetMIME(".json") |
| 18 | require.Equal(t, "application/json", res) |
| 19 | |
| 20 | res = GetMIME(".xml") |
| 21 | require.Equal(t, "application/xml", res) |
| 22 | |
| 23 | res = GetMIME("xml") |
| 24 | require.Equal(t, "application/xml", res) |
| 25 | |
| 26 | res = GetMIME("unknown") |
| 27 | require.Equal(t, MIMEOctetStream, res) |
| 28 | |
| 29 | res = GetMIME(".zst") |
| 30 | require.Equal(t, "application/zstd", res) |
| 31 | |
| 32 | res = GetMIME("zst") |
| 33 | require.Equal(t, "application/zstd", res) |
| 34 | |
| 35 | res = GetMIME("cbor") |
| 36 | require.Equal(t, "application/cbor", res) |
| 37 | |
| 38 | // upper-case extensions match case-insensitively |
| 39 | res = GetMIME("PNG") |
| 40 | require.Equal(t, "image/png", res) |
| 41 | |
| 42 | res = GetMIME(".JSON") |
| 43 | require.Equal(t, "application/json", res) |
| 44 | |
| 45 | // empty case |
| 46 | res = GetMIME("") |
| 47 | require.Empty(t, res) |
| 48 | |
| 49 | err := mime.AddExtensionType(".mjs", "application/javascript") |
| 50 | if err == nil { |
| 51 | res = GetMIME(".mjs") |
| 52 | require.Equal(t, "application/javascript", res) |
| 53 | } |
| 54 | require.NoError(t, err) |
| 55 | |
| 56 | require.Equal(t, "application/json", GetMIME(".JSON")) |
| 57 | require.Equal(t, MIMEOctetStream, GetMIME(" .json")) |
| 58 | } |
| 59 | |
| 60 | // go test -v -run=^$ -bench=Benchmark_GetMIME -benchmem -count=2 |
| 61 | func Benchmark_GetMIME(b *testing.B) { |
| 62 | var res string |
| 63 | b.Run("fiber", func(b *testing.B) { |
| 64 | for i := 0; i < b.N; i++ { |
| 65 | res = GetMIME(".xml") |
| 66 | res = GetMIME(".txt") |
| 67 | res = GetMIME(".png") |
| 68 | res = GetMIME(".exe") |
| 69 | res = GetMIME(".json") |
| 70 | } |
| 71 | require.Equal(b, "application/json", res) |
| 72 | }) |
| 73 | b.Run("default", func(b *testing.B) { |
| 74 | for i := 0; i < b.N; i++ { |
| 75 | res = mime.TypeByExtension(".xml") |
| 76 | res = mime.TypeByExtension(".txt") |
| 77 | res = mime.TypeByExtension(".png") |
| 78 | res = mime.TypeByExtension(".exe") |
| 79 | res = mime.TypeByExtension(".json") |
| 80 | } |
| 81 | require.Equal(b, "application/json", res) |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | func Test_ParseVendorSpecificContentType(t *testing.T) { |
| 86 | t.Parallel() |
| 87 | |
| 88 | cType := ParseVendorSpecificContentType("application/json") |
| 89 | require.Equal(t, "application/json", cType) |
| 90 | |
| 91 | // Test with parameters (semicolon IndexByte optimization) |
| 92 | cType = ParseVendorSpecificContentType("multipart/form-data; boundary=abc123") |
| 93 | require.Equal(t, "multipart/form-data; boundary=abc123", cType) |
| 94 | |
| 95 | // Test vendor-specific content types (plus IndexByte optimization) |
| 96 | cType = ParseVendorSpecificContentType("application/vnd.api+json; version=1") |
| 97 | require.Equal(t, "application/json", cType) |
| 98 | cType = ParseVendorSpecificContentType("application/vnd.dummy+x-www-form-urlencoded") |
| 99 | require.Equal(t, "application/x-www-form-urlencoded", cType) |
| 100 | |
| 101 | // Test invalid cases (slash IndexByte optimization) |
| 102 | cType = ParseVendorSpecificContentType("something invalid") |
| 103 | require.Equal(t, "something invalid", cType) |
| 104 | |
| 105 | // Additional edge cases for IndexByte optimization |
| 106 | cType = ParseVendorSpecificContentType("application/vnd.custom+xml; charset=utf-8") |
| 107 | require.Equal(t, "application/xml", cType) |
| 108 | |
| 109 | cType = ParseVendorSpecificContentType("text/vnd.example+plain") |
| 110 | require.Equal(t, "text/plain", cType) |
| 111 | |
| 112 | cType = ParseVendorSpecificContentType("aaaaaaaaaaa/vnd.api+json") |
| 113 | require.Equal(t, "aaaaaaaaaaa/json", cType) |
| 114 | |
| 115 | cType = ParseVendorSpecificContentType("application/vnd.test+json;boundary=test") |
| 116 | require.Equal(t, "application/json", cType) |
| 117 | |
| 118 | // Edge cases with multiple special characters |
| 119 | cType = ParseVendorSpecificContentType("application/vnd.api+json+extra; param=value") |
| 120 | require.Equal(t, "application/json+extra", cType) |
| 121 | |
| 122 | // Semicolon before plus |
| 123 | cType = ParseVendorSpecificContentType("application/json; charset=utf-8+extra") |
| 124 | require.Equal(t, "application/json", cType) |
| 125 | |
| 126 | // Empty and single-character inputs |
| 127 | require.Empty(t, ParseVendorSpecificContentType("")) |
| 128 | require.Equal(t, "+", ParseVendorSpecificContentType("+")) |
| 129 | require.Equal(t, ";", ParseVendorSpecificContentType(";")) |
| 130 | require.Equal(t, "/", ParseVendorSpecificContentType("/")) |
| 131 | |
| 132 | cType = ParseVendorSpecificContentType("multipart/form-data; boundary=dart-http-boundary-ZnVy.ICWq+7HOdsHqWxCFa8g3D.KAhy+Y0sYJ_lBADypu8po3_X") |
| 133 | require.Equal(t, "multipart/form-data", cType) |
| 134 | |
| 135 | cType = ParseVendorSpecificContentType("multipart/form-data") |
| 136 | require.Equal(t, "multipart/form-data", cType) |
| 137 | |
| 138 | cType = ParseVendorSpecificContentType("application/vnd.api+json; version=1") |
| 139 | require.Equal(t, "application/json", cType) |
| 140 | |
| 141 | cType = ParseVendorSpecificContentType("application/vnd.api+json") |
| 142 | require.Equal(t, "application/json", cType) |
| 143 | |
| 144 | // Uppercase vendor specific type should only be parsed when case-insensitive |
| 145 | // matching is enabled |
| 146 | cType = ParseVendorSpecificContentType("APPLICATION/VND.API+JSON") |
| 147 | require.Equal(t, "APPLICATION/JSON", cType) |
| 148 | |
| 149 | cType = ParseVendorSpecificContentType("APPLICATION/VND.API+JSON", true) |
| 150 | require.Equal(t, "application/json", cType) |
| 151 | |
| 152 | cType = ParseVendorSpecificContentType("application/vnd.dummy+x-www-form-urlencoded") |
| 153 | require.Equal(t, "application/x-www-form-urlencoded", cType) |
| 154 | |
| 155 | cType = ParseVendorSpecificContentType("something invalid") |
| 156 | require.Equal(t, "something invalid", cType) |
| 157 | |
| 158 | cType = ParseVendorSpecificContentType("invalid+withoutSlash") |
| 159 | require.Equal(t, "invalid+withoutSlash", cType) |
| 160 | } |
| 161 | |
| 162 | func Test_ParseVendorSpecificContentType_IndexByteOptimization(t *testing.T) { |
| 163 | t.Parallel() |
| 164 | testCases := []struct { |
| 165 | input string |
| 166 | expected string |
| 167 | desc string |
| 168 | }{ |
| 169 | {"application/vnd.api+json", "application/json", "plus in middle"}, |
| 170 | {"+json", "+json", "plus at start, no slash"}, |
| 171 | {"application/+json", "application/json", "plus after slash"}, |
| 172 | {"application/json;charset=utf-8", "application/json;charset=utf-8", "semicolon after content type"}, |
| 173 | {";charset=utf-8", ";charset=utf-8", "semicolon at start"}, |
| 174 | {"application/vnd.api+json;version=1", "application/json", "plus before semicolon"}, |
| 175 | {"application/json", "application/json", "normal content type with slash"}, |
| 176 | {"applicationjson", "applicationjson", "no slash in content type"}, |
| 177 | {"app/vnd.test+data/extra", "app/data/extra", "multiple slashes"}, |
| 178 | } |
| 179 | for _, tc := range testCases { |
| 180 | t.Run(tc.desc, func(t *testing.T) { |
| 181 | t.Parallel() |
| 182 | |
| 183 | require.Equal(t, tc.expected, ParseVendorSpecificContentType(tc.input), "input: %s", tc.input) |
| 184 | }) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | func Benchmark_ParseVendorSpecificContentType(b *testing.B) { |
| 189 | b.Run("vendorContentType", func(b *testing.B) { |
| 190 | for i := 0; i < b.N; i++ { |
| 191 | ParseVendorSpecificContentType("application/vnd.api+json; version=1") |
| 192 | } |
| 193 | }) |
| 194 | |
| 195 | b.Run("defaultContentType", func(b *testing.B) { |
| 196 | for i := 0; i < b.N; i++ { |
| 197 | ParseVendorSpecificContentType("application/json") |
| 198 | } |
| 199 | }) |
| 200 | } |
| 201 | |
| 202 | func Test_StatusMessage(t *testing.T) { |
| 203 | t.Parallel() |
| 204 | res := StatusMessage(204) |
| 205 | require.Equal(t, "No Content", res) |
| 206 | |
| 207 | res = StatusMessage(404) |
| 208 | require.Equal(t, "Not Found", res) |
| 209 | |
| 210 | res = StatusMessage(426) |
| 211 | require.Equal(t, "Upgrade Required", res) |
| 212 | |
| 213 | res = StatusMessage(511) |
| 214 | require.Equal(t, "Network Authentication Required", res) |
| 215 | |
| 216 | res = StatusMessage(1337) |
| 217 | require.Empty(t, res) |
| 218 | |
| 219 | res = StatusMessage(-1) |
| 220 | require.Empty(t, res) |
| 221 | |
| 222 | res = StatusMessage(0) |
| 223 | require.Empty(t, res) |
| 224 | |
| 225 | res = StatusMessage(600) |
| 226 | require.Empty(t, res) |
| 227 | |
| 228 | res = StatusMessage(100) |
| 229 | require.Equal(t, "Continue", res) |
| 230 | |
| 231 | res = StatusMessage(http.StatusTeapot) |
| 232 | require.Equal(t, "I'm a teapot", res) |
| 233 | } |
| 234 | |
| 235 | // go test -run=^$ -bench=Benchmark_StatusMessage -benchmem -count=2 |
| 236 | func Benchmark_StatusMessage(b *testing.B) { |
| 237 | b.Run("fiber", func(b *testing.B) { |
| 238 | for i := 0; i < b.N; i++ { |
| 239 | StatusMessage(http.StatusNotExtended) |
| 240 | } |
| 241 | }) |
| 242 | b.Run("default", func(b *testing.B) { |
| 243 | for i := 0; i < b.N; i++ { |
| 244 | http.StatusText(http.StatusNotExtended) |
| 245 | } |
| 246 | }) |
| 247 | } |