| 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 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
) |
| 15 |
|
| 16 |
func Test_GetMIME(t *testing.T) { |
| 17 |
t.Parallel() |
| 18 |
res := GetMIME(".json") |
| 19 |
require.Equal(t, "application/json", res) |
| 20 |
|
| 21 |
res = GetMIME(".xml") |
| 22 |
require.Equal(t, "application/xml", res) |
| 23 |
|
| 24 |
res = GetMIME("xml") |
| 25 |
require.Equal(t, "application/xml", res) |
| 26 |
|
| 27 |
res = GetMIME("unknown") |
| 28 |
require.Equal(t, MIMEOctetStream, res) |
| 29 |
|
| 30 |
res = GetMIME(".zst") |
| 31 |
require.Equal(t, "application/zstd", res) |
| 32 |
|
| 33 |
res = GetMIME("zst") |
| 34 |
require.Equal(t, "application/zstd", res) |
| 35 |
|
| 36 |
res = GetMIME("cbor") |
| 37 |
require.Equal(t, "application/cbor", res) |
| 38 |
|
| 39 |
// upper-case extensions match case-insensitively |
| 40 |
res = GetMIME("PNG") |
| 41 |
require.Equal(t, "image/png", res) |
| 42 |
|
| 43 |
res = GetMIME(".JSON") |
| 44 |
require.Equal(t, "application/json", res) |
| 45 |
|
| 46 |
// empty case |
| 47 |
res = GetMIME("") |
| 48 |
require.Empty(t, res) |
| 49 |
|
| 50 |
err := mime.AddExtensionType(".mjs", "application/javascript") |
| 51 |
if err == nil { |
| 52 |
res = GetMIME(".mjs") |
| 53 |
require.Equal(t, "application/javascript", res) |
| 54 |
} |
| 55 |
require.NoError(t, err) |
| 56 |
|
| 57 |
require.Equal(t, "application/json", GetMIME(".JSON")) |
| 58 |
require.Equal(t, MIMEOctetStream, GetMIME(" .json")) |
| 59 |
} |
| 60 |
|
| 61 |
// go test -v -run=^$ -bench=Benchmark_GetMIME -benchmem -count=2 |
| 62 |
func Benchmark_GetMIME(b *testing.B) { |
| 63 |
var res string |
| 64 |
b.Run("fiber", func(b *testing.B) { |
| 65 |
for i := 0; i < b.N; i++ { |
| 66 |
res = GetMIME(".xml") |
| 67 |
res = GetMIME(".txt") |
| 68 |
res = GetMIME(".png") |
| 69 |
res = GetMIME(".exe") |
| 70 |
res = GetMIME(".json") |
| 71 |
} |
| 72 |
require.Equal(b, "application/json", res) |
| 73 |
}) |
| 74 |
b.Run("default", func(b *testing.B) { |
| 75 |
for i := 0; i < b.N; i++ { |
| 76 |
res = mime.TypeByExtension(".xml") |
| 77 |
res = mime.TypeByExtension(".txt") |
| 78 |
res = mime.TypeByExtension(".png") |
| 79 |
res = mime.TypeByExtension(".exe") |
| 80 |
res = mime.TypeByExtension(".json") |
| 81 |
} |
| 82 |
require.Equal(b, "application/json", res) |
| 83 |
}) |
| 84 |
} |
| 85 |
|
| 86 |
func Test_ParseVendorSpecificContentType(t *testing.T) { |
| 87 |
t.Parallel() |
| 88 |
|
| 89 |
cType := ParseVendorSpecificContentType("application/json") |
| 90 |
require.Equal(t, "application/json", cType) |
| 91 |
|
| 92 |
// Test with parameters (semicolon IndexByte optimization) |
| 93 |
cType = ParseVendorSpecificContentType("multipart/form-data; boundary=abc123") |
| 94 |
require.Equal(t, "multipart/form-data; boundary=abc123", cType) |
| 95 |
|
| 96 |
// Test vendor-specific content types (plus IndexByte optimization) |
| 97 |
cType = ParseVendorSpecificContentType("application/vnd.api+json; version=1") |
| 98 |
require.Equal(t, "application/json", cType) |
| 99 |
cType = ParseVendorSpecificContentType("application/vnd.dummy+x-www-form-urlencoded") |
| 100 |
require.Equal(t, "application/x-www-form-urlencoded", cType) |
| 101 |
|
| 102 |
// Test invalid cases (slash IndexByte optimization) |
| 103 |
cType = ParseVendorSpecificContentType("something invalid") |
| 104 |
require.Equal(t, "something invalid", cType) |
| 105 |
|
| 106 |
// Additional edge cases for IndexByte optimization |
| 107 |
cType = ParseVendorSpecificContentType("application/vnd.custom+xml; charset=utf-8") |
| 108 |
require.Equal(t, "application/xml", cType) |
| 109 |
|
| 110 |
cType = ParseVendorSpecificContentType("text/vnd.example+plain") |
| 111 |
require.Equal(t, "text/plain", cType) |
| 112 |
|
| 113 |
cType = ParseVendorSpecificContentType("aaaaaaaaaaa/vnd.api+json") |
| 114 |
require.Equal(t, "aaaaaaaaaaa/json", cType) |
| 115 |
|
| 116 |
cType = ParseVendorSpecificContentType("application/vnd.test+json;boundary=test") |
| 117 |
require.Equal(t, "application/json", cType) |
| 118 |
|
| 119 |
// Edge cases with multiple special characters |
| 120 |
cType = ParseVendorSpecificContentType("application/vnd.api+json+extra; param=value") |
| 121 |
require.Equal(t, "application/json+extra", cType) |
| 122 |
|
| 123 |
// Semicolon before plus |
| 124 |
cType = ParseVendorSpecificContentType("application/json; charset=utf-8+extra") |
| 125 |
require.Equal(t, "application/json", cType) |
| 126 |
|
| 127 |
// Empty and single-character inputs |
| 128 |
require.Empty(t, ParseVendorSpecificContentType("")) |
| 129 |
require.Equal(t, "+", ParseVendorSpecificContentType("+")) |
| 130 |
require.Equal(t, ";", ParseVendorSpecificContentType(";")) |
| 131 |
require.Equal(t, "/", ParseVendorSpecificContentType("/")) |
| 132 |
|
| 133 |
cType = ParseVendorSpecificContentType("multipart/form-data; boundary=dart-http-boundary-ZnVy.ICWq+7HOdsHqWxCFa8g3D.KAhy+Y0sYJ_lBADypu8po3_X") |
| 134 |
require.Equal(t, "multipart/form-data", cType) |
| 135 |
|
| 136 |
cType = ParseVendorSpecificContentType("multipart/form-data") |
| 137 |
require.Equal(t, "multipart/form-data", cType) |
| 138 |
|
| 139 |
cType = ParseVendorSpecificContentType("application/vnd.api+json; version=1") |
| 140 |
require.Equal(t, "application/json", cType) |
| 141 |
|
| 142 |
cType = ParseVendorSpecificContentType("application/vnd.api+json") |
| 143 |
require.Equal(t, "application/json", cType) |
| 144 |
|
| 145 |
// Uppercase vendor specific type should only be parsed when case-insensitive |
| 146 |
// matching is enabled |
| 147 |
cType = ParseVendorSpecificContentType("APPLICATION/VND.API+JSON") |
| 148 |
require.Equal(t, "APPLICATION/JSON", cType) |
| 149 |
|
| 150 |
cType = ParseVendorSpecificContentType("APPLICATION/VND.API+JSON", true) |
| 151 |
require.Equal(t, "application/json", cType) |
| 152 |
|
| 153 |
cType = ParseVendorSpecificContentType("application/vnd.dummy+x-www-form-urlencoded") |
| 154 |
require.Equal(t, "application/x-www-form-urlencoded", cType) |
| 155 |
|
| 156 |
cType = ParseVendorSpecificContentType("something invalid") |
| 157 |
require.Equal(t, "something invalid", cType) |
| 158 |
|
| 159 |
cType = ParseVendorSpecificContentType("invalid+withoutSlash") |
| 160 |
require.Equal(t, "invalid+withoutSlash", cType) |
| 161 |
} |
| 162 |
|
| 163 |
func Test_ParseVendorSpecificContentType_IndexByteOptimization(t *testing.T) { |
| 164 |
t.Parallel() |
| 165 |
testCases := []struct { |
| 166 |
input string |
| 167 |
expected string |
| 168 |
desc string |
| 169 |
}{ |
| 170 |
{"application/vnd.api+json", "application/json", "plus in middle"}, |
| 171 |
{"+json", "+json", "plus at start, no slash"}, |
| 172 |
{"application/+json", "application/json", "plus after slash"}, |
| 173 |
{"application/json;charset=utf-8", "application/json;charset=utf-8", "semicolon after content type"}, |
| 174 |
{";charset=utf-8", ";charset=utf-8", "semicolon at start"}, |
| 175 |
{"application/vnd.api+json;version=1", "application/json", "plus before semicolon"}, |
| 176 |
{"application/json", "application/json", "normal content type with slash"}, |
| 177 |
{"applicationjson", "applicationjson", "no slash in content type"}, |
| 178 |
{"app/vnd.test+data/extra", "app/data/extra", "multiple slashes"}, |
| 179 |
} |
| 180 |
for _, tc := range testCases { |
| 181 |
t.Run(tc.desc, func(t *testing.T) { |
| 182 |
t.Parallel() |
| 183 |
|
| 184 |
require.Equal(t, tc.expected, ParseVendorSpecificContentType(tc.input), "input: %s", tc.input) |
| 185 |
}) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func Benchmark_ParseVendorSpecificContentType(b *testing.B) { |
| 190 |
b.Run("vendorContentType", func(b *testing.B) { |
| 191 |
for i := 0; i < b.N; i++ { |
| 192 |
ParseVendorSpecificContentType("application/vnd.api+json; version=1") |
| 193 |
} |
| 194 |
}) |
| 195 |
|
| 196 |
b.Run("defaultContentType", func(b *testing.B) { |
| 197 |
for i := 0; i < b.N; i++ { |
| 198 |
ParseVendorSpecificContentType("application/json") |
| 199 |
} |
| 200 |
}) |
| 201 |
} |
| 202 |
|
| 203 |
func Test_StatusMessage(t *testing.T) { |
| 204 |
t.Parallel() |
| 205 |
res := StatusMessage(204) |
| 206 |
require.Equal(t, "No Content", res) |
| 207 |
|
| 208 |
res = StatusMessage(404) |
| 209 |
require.Equal(t, "Not Found", res) |
| 210 |
|
| 211 |
res = StatusMessage(426) |
| 212 |
require.Equal(t, "Upgrade Required", res) |
| 213 |
|
| 214 |
res = StatusMessage(511) |
| 215 |
require.Equal(t, "Network Authentication Required", res) |
| 216 |
|
| 217 |
res = StatusMessage(1337) |
| 218 |
require.Empty(t, res) |
| 219 |
|
| 220 |
res = StatusMessage(-1) |
| 221 |
require.Empty(t, res) |
| 222 |
|
| 223 |
res = StatusMessage(0) |
| 224 |
require.Empty(t, res) |
| 225 |
|
| 226 |
res = StatusMessage(600) |
| 227 |
require.Empty(t, res) |
| 228 |
|
| 229 |
res = StatusMessage(100) |
| 230 |
require.Equal(t, "Continue", res) |
| 231 |
|
| 232 |
res = StatusMessage(http.StatusTeapot) |
| 233 |
require.Equal(t, "I'm a teapot", res) |
| 234 |
} |
| 235 |
|
| 236 |
// go test -run=^$ -bench=Benchmark_StatusMessage -benchmem -count=2 |
| 237 |
func Benchmark_StatusMessage(b *testing.B) { |
| 238 |
b.Run("fiber", func(b *testing.B) { |
| 239 |
for i := 0; i < b.N; i++ { |
| 240 |
StatusMessage(http.StatusNotExtended) |
| 241 |
} |
| 242 |
}) |
| 243 |
b.Run("default", func(b *testing.B) { |
| 244 |
for i := 0; i < b.N; i++ { |
| 245 |
http.StatusText(http.StatusNotExtended) |
| 246 |
} |
| 247 |
}) |
| 248 |
} |
| 249 |
|
| 250 |
// Test_GetMIME_TableCoverage checks every mimeExtensions entry is reachable through the packed-key table. |
| 251 |
func Test_GetMIME_TableCoverage(t *testing.T) { |
| 252 |
t.Parallel() |
| 253 |
for ext, want := range mimeExtensions { |
| 254 |
require.Equal(t, want, GetMIME(ext), "extension %q", ext) |
| 255 |
require.Equal(t, want, GetMIME("."+ext), "extension .%q", ext) |
| 256 |
require.Equal(t, want, GetMIME(strings.ToUpper(ext)), "extension %q upper-cased", ext) |
| 257 |
// A trailing NUL packs like the key's zero padding but is a different extension. |
| 258 |
require.Equal(t, MIMEOctetStream, GetMIME(ext+"\x00"), "extension %q with a trailing NUL", ext) |
| 259 |
} |
| 260 |
// Distinct extensions must never share a packed key. |
| 261 |
seen := make(map[uint64]string, len(mimeExtensions)) |
| 262 |
for ext := range mimeExtensions { |
| 263 |
key := packExtension(ext) |
| 264 |
prev, dup := seen[key] |
| 265 |
require.False(t, dup, "extensions %q and %q pack to the same key", prev, ext) |
| 266 |
seen[key] = ext |
| 267 |
} |
| 268 |
// Entries the table cannot hold are build-time mistakes, not silent fallbacks or hangs. |
| 269 |
require.Panics(t, func() { buildMIMETable(map[string]string{"": "a"}) }) |
| 270 |
require.Panics(t, func() { buildMIMETable(map[string]string{"toolongext": "b"}) }) |
| 271 |
tooMany := make(map[string]string, mimeTableMaxEntries+1) |
| 272 |
for i := range mimeTableMaxEntries + 1 { |
| 273 |
tooMany["e"+FormatInt(int64(i))] = "x" |
| 274 |
} |
| 275 |
require.Panics(t, func() { buildMIMETable(tooMany) }) |
| 276 |
|
| 277 |
// Misses of every length; NUL and non-ASCII bytes are in no mime database, so these are octet-stream everywhere. |
| 278 |
for _, ext := range []string{"\x00", "ht\x00l", "j\xf3on", "1234567\x00", "toolongext\x01", ".\x00\x00\x00\x00\x00\x00\x00\x00"} { |
| 279 |
require.Equal(t, MIMEOctetStream, GetMIME(ext), "extension %q", ext) |
| 280 |
} |
| 281 |
} |