github.com/blend/go-sdk@v1.20240719.1/proxyprotocol/create_listener_test.go (about) 1 /* 2 3 Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package proxyprotocol 9 10 import ( 11 "crypto/tls" 12 "net" 13 "testing" 14 "time" 15 16 "github.com/blend/go-sdk/assert" 17 "github.com/blend/go-sdk/webutil" 18 ) 19 20 func TestCreateListener(t *testing.T) { 21 assert := assert.New(t) 22 23 listener, err := CreateListener("tcp", "127.0.0.1:0", 24 OptKeepAlive(true), 25 OptUseProxyProtocol(true), 26 OptKeepAlivePeriod(30*time.Second), 27 ) 28 defer func() { _ = listener.Close() }() 29 30 assert.Nil(err) 31 assert.NotNil(listener) 32 33 typed, ok := listener.(*Listener) 34 assert.True(ok) 35 assert.NotNil(typed) 36 37 assert.NotNil(typed.Listener) 38 39 tcpListener, ok := typed.Listener.(webutil.TCPKeepAliveListener) 40 assert.True(ok) 41 assert.NotNil(tcpListener) 42 } 43 44 func TestCreateTLSListener(t *testing.T) { 45 assert := assert.New(t) 46 47 tlsConfig := &tls.Config{} 48 listener, err := CreateListener("tcp", "127.0.0.1:0", 49 OptKeepAlive(true), 50 OptUseProxyProtocol(true), 51 OptKeepAlivePeriod(30*time.Second), 52 OptTLSConfig(tlsConfig), 53 ) 54 defer func() { _ = listener.Close() }() 55 56 assert.Nil(err) 57 assert.NotNil(listener) 58 59 listenerAddress := listener.Addr().String() 60 61 dialErrors := make(chan error, 1) 62 go func() { 63 _, netErr := net.Dial("tcp", listenerAddress) 64 if netErr != nil { 65 dialErrors <- netErr 66 } 67 }() 68 69 conn, err := listener.Accept() 70 assert.Nil(err) 71 72 typed, ok := conn.(*tls.Conn) 73 assert.True(ok) 74 assert.NotNil(typed) 75 76 assert.Empty(dialErrors) 77 }