github.com/fumiama/terasu@v0.0.0-20240507144117-547a591149c0/terasu_test.go (about)

     1  package terasu
     2  
     3  import (
     4  	"crypto/tls"
     5  	"io"
     6  	"net"
     7  	"net/http"
     8  	"testing"
     9  )
    10  
    11  func TestHTTPDialTLS13(t *testing.T) {
    12  	cli := http.Client{
    13  		Transport: &http.Transport{
    14  			DialTLS: func(network, addr string) (net.Conn, error) {
    15  				conn, err := net.Dial("tcp", "18.65.159.2:443")
    16  				if err != nil {
    17  					return nil, err
    18  				}
    19  				t.Log("net.Dial succeeded")
    20  				tlsConn := tls.Client(conn, &tls.Config{
    21  					ServerName:         "huggingface.co",
    22  					InsecureSkipVerify: true,
    23  				})
    24  				err = Use(tlsConn).Handshake(4)
    25  				if err != nil {
    26  					_ = tlsConn.Close()
    27  					return nil, err
    28  				}
    29  				return tlsConn, nil
    30  			},
    31  		},
    32  	}
    33  	resp, err := cli.Get("https://huggingface.co/")
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	defer resp.Body.Close()
    38  	if resp.StatusCode != http.StatusOK {
    39  		t.Fatal("status code:", resp.StatusCode)
    40  	}
    41  	data, err := io.ReadAll(resp.Body)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	t.Log(string(data))
    46  }
    47  
    48  func TestHTTPDialTLS12(t *testing.T) {
    49  	cli := http.Client{
    50  		Transport: &http.Transport{
    51  			DialTLS: func(network, addr string) (net.Conn, error) {
    52  				conn, err := net.Dial("tcp", "18.65.159.2:443")
    53  				if err != nil {
    54  					return nil, err
    55  				}
    56  				t.Log("net.Dial succeeded")
    57  				tlsConn := tls.Client(conn, &tls.Config{
    58  					ServerName:         "huggingface.co",
    59  					InsecureSkipVerify: true,
    60  					MaxVersion:         tls.VersionTLS12,
    61  				})
    62  				err = Use(tlsConn).Handshake(4)
    63  				if err != nil {
    64  					_ = tlsConn.Close()
    65  					return nil, err
    66  				}
    67  				return tlsConn, nil
    68  			},
    69  		},
    70  	}
    71  	resp, err := cli.Get("https://huggingface.co/")
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	defer resp.Body.Close()
    76  	if resp.StatusCode != http.StatusOK {
    77  		t.Fatal("status code:", resp.StatusCode)
    78  	}
    79  	data, err := io.ReadAll(resp.Body)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	t.Log(string(data))
    84  }