-
Notifications
You must be signed in to change notification settings - Fork 10
/
http_test.go
153 lines (121 loc) · 3.94 KB
/
http_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"mime"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func Test_GetMIME(t *testing.T) {
t.Parallel()
res := GetMIME(".json")
require.Equal(t, "application/json", res)
res = GetMIME(".xml")
require.Equal(t, "application/xml", res)
res = GetMIME("xml")
require.Equal(t, "application/xml", res)
res = GetMIME("unknown")
require.Equal(t, MIMEOctetStream, res)
res = GetMIME(".zst")
require.Equal(t, "application/zstd", res)
res = GetMIME("zst")
require.Equal(t, "application/zstd", res)
// empty case
res = GetMIME("")
require.Equal(t, "", res)
err := mime.AddExtensionType(".mjs", "application/javascript")
if err == nil {
res = GetMIME(".mjs")
require.Equal(t, "application/javascript", res)
}
require.NoError(t, err)
}
// go test -v -run=^$ -bench=Benchmark_GetMIME -benchmem -count=2
func Benchmark_GetMIME(b *testing.B) {
var res string
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = GetMIME(".xml")
res = GetMIME(".txt")
res = GetMIME(".png")
res = GetMIME(".exe")
res = GetMIME(".json")
}
require.Equal(b, "application/json", res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = mime.TypeByExtension(".xml")
res = mime.TypeByExtension(".txt")
res = mime.TypeByExtension(".png")
res = mime.TypeByExtension(".exe")
res = mime.TypeByExtension(".json")
}
require.Equal(b, "application/json", res)
})
}
func Test_ParseVendorSpecificContentType(t *testing.T) {
t.Parallel()
cType := ParseVendorSpecificContentType("application/json")
require.Equal(t, "application/json", cType)
cType = ParseVendorSpecificContentType("multipart/form-data; boundary=dart-http-boundary-ZnVy.ICWq+7HOdsHqWxCFa8g3D.KAhy+Y0sYJ_lBADypu8po3_X")
require.Equal(t, "multipart/form-data", cType)
cType = ParseVendorSpecificContentType("multipart/form-data")
require.Equal(t, "multipart/form-data", cType)
cType = ParseVendorSpecificContentType("application/vnd.api+json; version=1")
require.Equal(t, "application/json", cType)
cType = ParseVendorSpecificContentType("application/vnd.api+json")
require.Equal(t, "application/json", cType)
cType = ParseVendorSpecificContentType("application/vnd.dummy+x-www-form-urlencoded")
require.Equal(t, "application/x-www-form-urlencoded", cType)
cType = ParseVendorSpecificContentType("something invalid")
require.Equal(t, "something invalid", cType)
cType = ParseVendorSpecificContentType("invalid+withoutSlash")
require.Equal(t, "invalid+withoutSlash", cType)
}
func Benchmark_ParseVendorSpecificContentType(b *testing.B) {
b.Run("vendorContentType", func(b *testing.B) {
for n := 0; n < b.N; n++ {
ParseVendorSpecificContentType("application/vnd.api+json; version=1")
}
})
b.Run("defaultContentType", func(b *testing.B) {
for n := 0; n < b.N; n++ {
ParseVendorSpecificContentType("application/json")
}
})
}
func Test_StatusMessage(t *testing.T) {
t.Parallel()
res := StatusMessage(204)
require.Equal(t, "No Content", res)
res = StatusMessage(404)
require.Equal(t, "Not Found", res)
res = StatusMessage(426)
require.Equal(t, "Upgrade Required", res)
res = StatusMessage(511)
require.Equal(t, "Network Authentication Required", res)
res = StatusMessage(1337)
require.Equal(t, "", res)
res = StatusMessage(-1)
require.Equal(t, "", res)
res = StatusMessage(0)
require.Equal(t, "", res)
res = StatusMessage(600)
require.Equal(t, "", res)
}
// go test -run=^$ -bench=Benchmark_StatusMessage -benchmem -count=2
func Benchmark_StatusMessage(b *testing.B) {
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
StatusMessage(http.StatusNotExtended)
}
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
http.StatusText(http.StatusNotExtended)
}
})
}