istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/echo/common/dialer.go (about)

     1  //  Copyright Istio Authors
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package common
    16  
    17  import (
    18  	"context"
    19  	"net"
    20  	"net/http"
    21  
    22  	"github.com/gorilla/websocket"
    23  	"google.golang.org/grpc"
    24  )
    25  
    26  var (
    27  	// DefaultGRPCDialFunc just calls grpc.Dial directly, with no alterations to the arguments.
    28  	DefaultGRPCDialFunc = grpc.DialContext
    29  	// DefaultWebsocketDialFunc just calls dialer.Dial, with no alterations to the arguments.
    30  	DefaultWebsocketDialFunc = func(dialer *websocket.Dialer, urlStr string, requestHeader http.Header) (*websocket.Conn, *http.Response, error) {
    31  		return dialer.Dial(urlStr, requestHeader)
    32  	}
    33  	// DefaultHTTPDoFunc just calls client.Do with no alterations to the arguments.
    34  	DefaultHTTPDoFunc = func(client *http.Client, req *http.Request) (*http.Response, error) {
    35  		return client.Do(req)
    36  	}
    37  	// DefaultTCPDialFunc just calls dialer.Dial, with no alterations to the arguments.
    38  	DefaultTCPDialFunc = func(dialer net.Dialer, ctx context.Context, address string) (net.Conn, error) {
    39  		return dialer.DialContext(ctx, "tcp", address)
    40  	}
    41  	// DefaultDialer is provides defaults for all dial functions.
    42  	DefaultDialer = Dialer{
    43  		GRPC:      DefaultGRPCDialFunc,
    44  		Websocket: DefaultWebsocketDialFunc,
    45  		HTTP:      DefaultHTTPDoFunc,
    46  		TCP:       DefaultTCPDialFunc,
    47  	}
    48  )
    49  
    50  // GRPCDialFunc a function for establishing a GRPC connection.
    51  type GRPCDialFunc func(ctx context.Context, address string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
    52  
    53  // WebsocketDialFunc a function for establishing a Websocket connection.
    54  type WebsocketDialFunc func(dialer *websocket.Dialer, urlStr string, requestHeader http.Header) (*websocket.Conn, *http.Response, error)
    55  
    56  // HTTPDoFunc a function for executing an HTTP request.
    57  type HTTPDoFunc func(client *http.Client, req *http.Request) (*http.Response, error)
    58  
    59  // TCPDialFunc a function for establishing a TCP connection.
    60  type TCPDialFunc func(dialer net.Dialer, ctx context.Context, address string) (net.Conn, error)
    61  
    62  // Dialer is a replaceable set of functions for creating client-side connections for various protocols, allowing a test
    63  // application to intercept the connection creation.
    64  type Dialer struct {
    65  	GRPC      GRPCDialFunc
    66  	Websocket WebsocketDialFunc
    67  	HTTP      HTTPDoFunc
    68  	TCP       TCPDialFunc
    69  }
    70  
    71  // FillInDefaults fills in any missing dial functions with defaults
    72  func (d Dialer) FillInDefaults() Dialer {
    73  	ret := DefaultDialer
    74  
    75  	if d.GRPC != nil {
    76  		ret.GRPC = d.GRPC
    77  	}
    78  	if d.Websocket != nil {
    79  		ret.Websocket = d.Websocket
    80  	}
    81  	if d.HTTP != nil {
    82  		ret.HTTP = d.HTTP
    83  	}
    84  	if d.TCP != nil {
    85  		ret.TCP = d.TCP
    86  	}
    87  	return ret
    88  }