github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/transport/http/outbound_test.go (about)

     1  /*
     2  	Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  	SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package http
     8  
     9  import (
    10  	"crypto/tls"
    11  	"crypto/x509"
    12  	"fmt"
    13  	"testing"
    14  
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/hyperledger/aries-framework-go/pkg/common/model"
    18  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
    19  )
    20  
    21  func TestWithOutboundOpts(t *testing.T) {
    22  	opt := WithOutboundHTTPClient(nil)
    23  	require.NotNil(t, opt)
    24  
    25  	clOpts := &outboundCommHTTPOpts{}
    26  	opt(clOpts)
    27  
    28  	opt = WithOutboundTimeout(clientTimeout)
    29  	require.NotNil(t, opt)
    30  
    31  	clOpts = &outboundCommHTTPOpts{}
    32  	// opt.client is nil, so setting timeout should panic
    33  	require.Panics(t, func() { opt(clOpts) })
    34  
    35  	opt = WithOutboundTLSConfig(nil)
    36  	require.NotNil(t, opt)
    37  
    38  	clOpts = &outboundCommHTTPOpts{}
    39  	opt(clOpts)
    40  }
    41  
    42  func TestOutboundHTTPTransport(t *testing.T) {
    43  	// prepare http server
    44  	server := startMockServer(mockHTTPHandler{})
    45  
    46  	port := getServerPort(server)
    47  	serverURL := fmt.Sprintf("https://localhost:%d", port)
    48  
    49  	defer func() {
    50  		err := server.Close()
    51  		if err != nil {
    52  			t.Fatalf("Failed to stop server: %s", err)
    53  		}
    54  	}()
    55  
    56  	// build a mock cert pool
    57  	cp := x509.NewCertPool()
    58  	err := addCertsToCertPool(cp)
    59  	require.NoError(t, err)
    60  
    61  	// build a tls.Config instance to be used by the outbound transport
    62  	tlsConfig := &tls.Config{ //nolint:gosec
    63  
    64  		RootCAs:      cp,
    65  		Certificates: nil,
    66  	}
    67  	// create a new invalid Outbound transport instance
    68  	_, err = NewOutbound()
    69  	require.Error(t, err)
    70  	require.EqualError(t, err, "creation of outbound transport requires an HTTP client")
    71  
    72  	// now create a new valid Outbound transport instance and test its Send() call
    73  	ot, err := NewOutbound(WithOutboundTLSConfig(tlsConfig), WithOutboundTimeout(clientTimeout))
    74  	require.NoError(t, err)
    75  	require.NotNil(t, ot)
    76  
    77  	// test Outbound transport's api
    78  	// first with an empty url
    79  	r, e := ot.Send([]byte("Hello World"), prepareDestination("serverURL"))
    80  	require.Error(t, e)
    81  	require.Empty(t, r)
    82  
    83  	// now try a bad url
    84  	r, e = ot.Send([]byte("Hello World"), prepareDestination("https://badurl"))
    85  	require.Error(t, e)
    86  	require.Empty(t, r)
    87  
    88  	// and try with a 'bad' payload with a valid url..
    89  	r, e = ot.Send([]byte("bad"), prepareDestination(serverURL))
    90  	require.Error(t, e)
    91  	require.Empty(t, r)
    92  	require.Contains(t, e.Error(), "received unsuccessful POST HTTP status from agent")
    93  
    94  	// finally using a valid url
    95  	r, e = ot.Send([]byte("Hello World"), prepareDestination(serverURL))
    96  	require.NoError(t, e)
    97  	require.NotEmpty(t, r)
    98  
    99  	require.True(t, ot.Accept("http://example.com"))
   100  	require.False(t, ot.Accept("123:22"))
   101  }
   102  
   103  func prepareDestination(endPoint string) *service.Destination {
   104  	return &service.Destination{
   105  		ServiceEndpoint: model.NewDIDCommV1Endpoint(endPoint),
   106  	}
   107  }