github.com/blend/go-sdk@v1.20240719.1/proxyprotocol/dialer_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  	"context"
    12  	"fmt"
    13  	"net"
    14  	"testing"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  )
    18  
    19  func Test_Dialer(t *testing.T) {
    20  	its := assert.New(t)
    21  
    22  	listener, err := CreateListener("tcp4", "127.0.0.1:0",
    23  		OptUseProxyProtocol(true),
    24  	)
    25  	its.Nil(err)
    26  	defer listener.Close()
    27  
    28  	sourceAddr := &net.TCPAddr{
    29  		IP:   net.ParseIP("192.168.0.7"),
    30  		Port: 31234,
    31  	}
    32  	dialer := NewDialer(
    33  		OptDialerConstSourceAdddr(sourceAddr),
    34  	)
    35  
    36  	go func() {
    37  		conn, err := dialer.DialContext(context.Background(), "tcp4", listener.Addr().String())
    38  		if err != nil {
    39  			panic(err)
    40  		}
    41  		defer conn.Close()
    42  	}()
    43  
    44  	conn, err := listener.Accept()
    45  	its.Nil(err)
    46  	its.Equal("192.168.0.7:31234", conn.RemoteAddr().String(), fmt.Sprintf("listener addr: %v", listener.Addr()))
    47  }