github.com/gofiber/fiber/v2@v2.47.0/utils/http_test.go (about) 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 13 func Test_GetMIME(t *testing.T) { 14 t.Parallel() 15 res := GetMIME(".json") 16 AssertEqual(t, "application/json", res) 17 18 res = GetMIME(".xml") 19 AssertEqual(t, "application/xml", res) 20 21 res = GetMIME("xml") 22 AssertEqual(t, "application/xml", res) 23 24 res = GetMIME("unknown") 25 AssertEqual(t, MIMEOctetStream, res) 26 27 err := mime.AddExtensionType(".mjs", "application/javascript") 28 if err == nil { 29 res = GetMIME(".mjs") 30 AssertEqual(t, "application/javascript", res) 31 } 32 AssertEqual(t, nil, err) 33 34 // empty case 35 res = GetMIME("") 36 AssertEqual(t, "", res) 37 } 38 39 // go test -v -run=^$ -bench=Benchmark_GetMIME -benchmem -count=2 40 func Benchmark_GetMIME(b *testing.B) { 41 var res string 42 b.Run("fiber", func(b *testing.B) { 43 for n := 0; n < b.N; n++ { 44 res = GetMIME(".xml") 45 res = GetMIME(".txt") 46 res = GetMIME(".png") 47 res = GetMIME(".exe") 48 res = GetMIME(".json") 49 } 50 AssertEqual(b, "application/json", res) 51 }) 52 b.Run("default", func(b *testing.B) { 53 for n := 0; n < b.N; n++ { 54 res = mime.TypeByExtension(".xml") 55 res = mime.TypeByExtension(".txt") 56 res = mime.TypeByExtension(".png") 57 res = mime.TypeByExtension(".exe") 58 res = mime.TypeByExtension(".json") 59 } 60 AssertEqual(b, "application/json", res) 61 }) 62 } 63 64 func Test_ParseVendorSpecificContentType(t *testing.T) { 65 t.Parallel() 66 67 cType := ParseVendorSpecificContentType("application/json") 68 AssertEqual(t, "application/json", cType) 69 70 cType = ParseVendorSpecificContentType("multipart/form-data; boundary=dart-http-boundary-ZnVy.ICWq+7HOdsHqWxCFa8g3D.KAhy+Y0sYJ_lBADypu8po3_X") 71 AssertEqual(t, "multipart/form-data", cType) 72 73 cType = ParseVendorSpecificContentType("multipart/form-data") 74 AssertEqual(t, "multipart/form-data", cType) 75 76 cType = ParseVendorSpecificContentType("application/vnd.api+json; version=1") 77 AssertEqual(t, "application/json", cType) 78 79 cType = ParseVendorSpecificContentType("application/vnd.api+json") 80 AssertEqual(t, "application/json", cType) 81 82 cType = ParseVendorSpecificContentType("application/vnd.dummy+x-www-form-urlencoded") 83 AssertEqual(t, "application/x-www-form-urlencoded", cType) 84 85 cType = ParseVendorSpecificContentType("something invalid") 86 AssertEqual(t, "something invalid", cType) 87 } 88 89 func Benchmark_ParseVendorSpecificContentType(b *testing.B) { 90 var cType string 91 b.Run("vendorContentType", func(b *testing.B) { 92 for n := 0; n < b.N; n++ { 93 cType = ParseVendorSpecificContentType("application/vnd.api+json; version=1") 94 } 95 AssertEqual(b, "application/json", cType) 96 }) 97 98 b.Run("defaultContentType", func(b *testing.B) { 99 for n := 0; n < b.N; n++ { 100 cType = ParseVendorSpecificContentType("application/json") 101 } 102 AssertEqual(b, "application/json", cType) 103 }) 104 } 105 106 func Test_StatusMessage(t *testing.T) { 107 t.Parallel() 108 res := StatusMessage(204) 109 AssertEqual(t, "No Content", res) 110 111 res = StatusMessage(404) 112 AssertEqual(t, "Not Found", res) 113 114 res = StatusMessage(426) 115 AssertEqual(t, "Upgrade Required", res) 116 117 res = StatusMessage(511) 118 AssertEqual(t, "Network Authentication Required", res) 119 120 res = StatusMessage(1337) 121 AssertEqual(t, "", res) 122 123 res = StatusMessage(-1) 124 AssertEqual(t, "", res) 125 126 res = StatusMessage(0) 127 AssertEqual(t, "", res) 128 129 res = StatusMessage(600) 130 AssertEqual(t, "", res) 131 } 132 133 // go test -run=^$ -bench=Benchmark_StatusMessage -benchmem -count=2 134 func Benchmark_StatusMessage(b *testing.B) { 135 var res string 136 b.Run("fiber", func(b *testing.B) { 137 for n := 0; n < b.N; n++ { 138 res = StatusMessage(http.StatusNotExtended) 139 } 140 AssertEqual(b, "Not Extended", res) 141 }) 142 b.Run("default", func(b *testing.B) { 143 for n := 0; n < b.N; n++ { 144 res = http.StatusText(http.StatusNotExtended) 145 } 146 AssertEqual(b, "Not Extended", res) 147 }) 148 }