go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/proxyproto/dialer_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package proxyproto
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  	"net"
    14  	"testing"
    15  
    16  	. "go.charczuk.com/sdk/assert"
    17  )
    18  
    19  func Test_Dialer(t *testing.T) {
    20  	listener, err := createListener()
    21  	ItsNil(t, err)
    22  	defer listener.Close()
    23  
    24  	sourceAddr := &net.TCPAddr{
    25  		IP:   net.ParseIP("192.168.0.7"),
    26  		Port: 31234,
    27  	}
    28  	dialer := NewDialer(
    29  		OptDialerConstSourceAdddr(sourceAddr),
    30  	)
    31  
    32  	go func() {
    33  		conn, err := dialer.DialContext(context.Background(), "tcp4", listener.Addr().String())
    34  		if err != nil {
    35  			panic(err)
    36  		}
    37  		defer conn.Close()
    38  	}()
    39  
    40  	conn, err := listener.Accept()
    41  	ItsNil(t, err)
    42  	ItsEqual(t, "192.168.0.7:31234", conn.RemoteAddr().String(), fmt.Sprintf("listener addr: %v", listener.Addr()))
    43  }