mirror of https://github.com/gofiber/fiber.git
👷 Add network type constant
parent
0ec0f50cfd
commit
00dbdd5fb3
4
app.go
4
app.go
|
@ -284,7 +284,7 @@ type Config struct {
|
|||
// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only)
|
||||
// WARNING: When prefork is set to true, only "tcp4" and "tcp6" can be chose.
|
||||
//
|
||||
// Default: "tcp4"
|
||||
// Default: NetworkTCP4
|
||||
Network string
|
||||
}
|
||||
|
||||
|
@ -403,7 +403,7 @@ func New(config ...Config) *App {
|
|||
app.config.JSONEncoder = json.Marshal
|
||||
}
|
||||
if app.config.Network == "" {
|
||||
app.config.Network = "tcp4"
|
||||
app.config.Network = NetworkTCP4
|
||||
}
|
||||
|
||||
// Init app
|
||||
|
|
|
@ -392,7 +392,7 @@ func Test_App_Listener_TLS(t *testing.T) {
|
|||
}
|
||||
config := &tls.Config{Certificates: []tls.Certificate{cer}}
|
||||
|
||||
ln, err := net.Listen("tcp4", ":3078")
|
||||
ln, err := net.Listen(NetworkTCP4, ":3078")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
ln = tls.NewListener(ln, config)
|
||||
|
@ -1151,7 +1151,7 @@ func Test_App_ReadTimeout(t *testing.T) {
|
|||
go func() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
conn, err := net.Dial("tcp4", "127.0.0.1:4004")
|
||||
conn, err := net.Dial(NetworkTCP4, "127.0.0.1:4004")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
defer conn.Close()
|
||||
|
||||
|
@ -1183,7 +1183,7 @@ func Test_App_BadRequest(t *testing.T) {
|
|||
|
||||
go func() {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
conn, err := net.Dial("tcp4", "127.0.0.1:4005")
|
||||
conn, err := net.Dial(NetworkTCP4, "127.0.0.1:4005")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
defer conn.Close()
|
||||
|
||||
|
|
|
@ -670,3 +670,10 @@ const (
|
|||
HeaderXRobotsTag = "X-Robots-Tag"
|
||||
HeaderXUACompatible = "X-UA-Compatible"
|
||||
)
|
||||
|
||||
// Network types that are commonly used
|
||||
const (
|
||||
NetworkTCP = "tcp"
|
||||
NetworkTCP4 = "tcp4"
|
||||
NetworkTCP6 = "tcp6"
|
||||
)
|
||||
|
|
|
@ -262,23 +262,23 @@ func Benchmark_Utils_IsNoCache(b *testing.B) {
|
|||
|
||||
func Test_Utils_lnMetadata(t *testing.T) {
|
||||
t.Run("closed listen", func(t *testing.T) {
|
||||
ln, err := net.Listen("tcp", ":0")
|
||||
ln, err := net.Listen(NetworkTCP, ":0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
utils.AssertEqual(t, nil, ln.Close())
|
||||
|
||||
addr, config := lnMetadata("tcp", ln)
|
||||
addr, config := lnMetadata(NetworkTCP, ln)
|
||||
|
||||
utils.AssertEqual(t, ln.Addr().String(), addr)
|
||||
utils.AssertEqual(t, true, config == nil)
|
||||
})
|
||||
|
||||
t.Run("non tls", func(t *testing.T) {
|
||||
ln, err := net.Listen("tcp", ":0")
|
||||
ln, err := net.Listen(NetworkTCP, ":0")
|
||||
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
addr, config := lnMetadata("tcp4", ln)
|
||||
addr, config := lnMetadata(NetworkTCP4, ln)
|
||||
|
||||
utils.AssertEqual(t, ln.Addr().String(), addr)
|
||||
utils.AssertEqual(t, true, config == nil)
|
||||
|
@ -290,12 +290,12 @@ func Test_Utils_lnMetadata(t *testing.T) {
|
|||
|
||||
config := &tls.Config{Certificates: []tls.Certificate{cer}}
|
||||
|
||||
ln, err := net.Listen("tcp4", ":0")
|
||||
ln, err := net.Listen(NetworkTCP4, ":0")
|
||||
utils.AssertEqual(t, nil, err)
|
||||
|
||||
ln = tls.NewListener(ln, config)
|
||||
|
||||
addr, config := lnMetadata("tcp4", ln)
|
||||
addr, config := lnMetadata(NetworkTCP4, ln)
|
||||
|
||||
utils.AssertEqual(t, ln.Addr().String(), addr)
|
||||
utils.AssertEqual(t, true, config != nil)
|
||||
|
|
|
@ -23,7 +23,7 @@ func Test_App_Prefork_Child_Process(t *testing.T) {
|
|||
|
||||
app := New()
|
||||
|
||||
err := app.prefork("tcp4", "invalid", nil)
|
||||
err := app.prefork(NetworkTCP4, "invalid", nil)
|
||||
utils.AssertEqual(t, false, err == nil)
|
||||
|
||||
go func() {
|
||||
|
@ -31,7 +31,7 @@ func Test_App_Prefork_Child_Process(t *testing.T) {
|
|||
utils.AssertEqual(t, nil, app.Shutdown())
|
||||
}()
|
||||
|
||||
utils.AssertEqual(t, nil, app.prefork("tcp6", "[::]:", nil))
|
||||
utils.AssertEqual(t, nil, app.prefork(NetworkTCP6, "[::]:", nil))
|
||||
|
||||
// Create tls certificate
|
||||
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
|
||||
|
@ -45,7 +45,7 @@ func Test_App_Prefork_Child_Process(t *testing.T) {
|
|||
utils.AssertEqual(t, nil, app.Shutdown())
|
||||
}()
|
||||
|
||||
utils.AssertEqual(t, nil, app.prefork("tcp4", "127.0.0.1:", config))
|
||||
utils.AssertEqual(t, nil, app.prefork(NetworkTCP4, "127.0.0.1:", config))
|
||||
}
|
||||
|
||||
func Test_App_Prefork_Master_Process(t *testing.T) {
|
||||
|
@ -59,11 +59,11 @@ func Test_App_Prefork_Master_Process(t *testing.T) {
|
|||
utils.AssertEqual(t, nil, app.Shutdown())
|
||||
}()
|
||||
|
||||
utils.AssertEqual(t, nil, app.prefork("tcp4", ":3000", nil))
|
||||
utils.AssertEqual(t, nil, app.prefork(NetworkTCP4, ":3000", nil))
|
||||
|
||||
dummyChildCmd = "invalid"
|
||||
|
||||
err := app.prefork("tcp4", "127.0.0.1:", nil)
|
||||
err := app.prefork(NetworkTCP4, "127.0.0.1:", nil)
|
||||
utils.AssertEqual(t, false, err == nil)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue