github.com/blend/go-sdk@v1.20220411.3/r2/opt_tls_client_cert_file_pair_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - 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 r2
     9  
    10  import (
    11  	"bytes"
    12  	"io"
    13  	"net/http"
    14  	"os"
    15  	"testing"
    16  
    17  	"github.com/blend/go-sdk/assert"
    18  )
    19  
    20  func TestOptTLSClientCertFilePair(t *testing.T) {
    21  	assert := assert.New(t)
    22  
    23  	certFile, err := writeTempFile(clientCert)
    24  	assert.Nil(err)
    25  	defer os.Remove(certFile)
    26  	keyFile, err := writeTempFile(clientKey)
    27  	assert.Nil(err)
    28  	defer os.Remove(keyFile)
    29  
    30  	r := New("https://foo.com", OptTLSClientCertFilePair(certFile, keyFile))
    31  	assert.NotNil(r.Client)
    32  	assert.NotNil(r.Client.Transport)
    33  	assert.NotNil(r.Client.Transport.(*http.Transport).TLSClientConfig)
    34  	assert.NotEmpty(r.Client.Transport.(*http.Transport).TLSClientConfig.Certificates)
    35  }
    36  
    37  func TestOptTLSClientCertFilePairErrors(t *testing.T) {
    38  	assert := assert.New(t)
    39  
    40  	r := New("https://foo.com", OptTLSClientCertFilePair("", ""))
    41  	assert.NotNil(r.Err)
    42  }
    43  
    44  func writeTempFile(contents []byte) (string, error) {
    45  	tf, err := os.CreateTemp("", "r2_opt_tls_client_cert_file_pair")
    46  	if err != nil {
    47  		return "", err
    48  	}
    49  	defer tf.Close()
    50  	_, err = io.Copy(tf, bytes.NewReader(contents))
    51  	if err != nil {
    52  		return "", err
    53  	}
    54  	return tf.Name(), nil
    55  }