@@ -12,6 +12,7 @@ import (
1212 "time"
1313
1414 "github.com/google/go-cmp/cmp"
15+ "github.com/pires/go-proxyproto"
1516 "github.com/stretchr/testify/assert"
1617 "github.com/stretchr/testify/require"
1718)
@@ -600,6 +601,40 @@ func TestNewRequestHTTPClient_SetCaCertsPool(t *testing.T) {
600601 }
601602}
602603
604+ func TestNewRequestHTTPClient_SetCaCertsPool_Error (t * testing.T ) {
605+ t .Run ("nilClient" , func (t * testing.T ) {
606+ t .Parallel ()
607+
608+ var c RequestHTTPClient
609+
610+ _ , err := c .SetCACertsPool (caCertPool )
611+
612+ require .Error (t , err )
613+ assert .Equal (t ,
614+ "*RequestHTTPClient.client is nil. Use NewRequestHTTPClient to initialize" ,
615+ err .Error (),
616+ )
617+ })
618+
619+ t .Run ("malformedClient" , func (t * testing.T ) {
620+ t .Parallel ()
621+
622+ incompleteClient := http.Client {}
623+ c := NewRequestHTTPClient ()
624+
625+ c .client = & incompleteClient
626+
627+ _ , err := c .SetCACertsPool (caCertPool )
628+
629+ require .Error (t , err )
630+
631+ assert .Equal (t ,
632+ "expected *http.Transport, got <nil>" ,
633+ err .Error (),
634+ )
635+ })
636+ }
637+
603638func TestNewRequestHTTPClient_SetInsecureSkipVerify_struct (t * testing.T ) {
604639 tests := []bool {true , false }
605640 for _ , tc := range tests {
@@ -662,6 +697,40 @@ func TestNewRequestHTTPClient_SetInsecureSkipVerify_tlsServer(t *testing.T) {
662697 }
663698}
664699
700+ func TestNewRequestHTTPClient_SetInsecureSkipVerify_Error (t * testing.T ) {
701+ t .Run ("nilClient" , func (t * testing.T ) {
702+ t .Parallel ()
703+
704+ var c RequestHTTPClient
705+
706+ _ , err := c .SetInsecureSkipVerify (true )
707+
708+ require .Error (t , err )
709+ assert .Equal (t ,
710+ "*RequestHTTPClient.client is nil. Use NewRequestHTTPClient to initialize" ,
711+ err .Error (),
712+ )
713+ })
714+
715+ t .Run ("malformedClient" , func (t * testing.T ) {
716+ t .Parallel ()
717+
718+ incompleteClient := http.Client {}
719+ c := NewRequestHTTPClient ()
720+
721+ c .client = & incompleteClient
722+
723+ _ , err := c .SetInsecureSkipVerify (true )
724+
725+ require .Error (t , err )
726+
727+ assert .Equal (t ,
728+ "expected *http.Transport, got <nil>" ,
729+ err .Error (),
730+ )
731+ })
732+ }
733+
665734func TestNewRequestHTTPClient_SetMethod (t * testing.T ) {
666735 tests := []struct {
667736 got string
@@ -724,6 +793,39 @@ func TestRequestHTTPClient_SetTransportOverride_transportAddress_struc(t *testin
724793 }
725794}
726795
796+ func TestRequestHTTPClient_SetTransportOverride_Error (t * testing.T ) {
797+ t .Run ("nilClient" , func (t * testing.T ) {
798+ t .Parallel ()
799+
800+ var c RequestHTTPClient
801+
802+ _ , err := c .SetTransportOverride ("http://localhost" )
803+ require .Error (t , err )
804+ assert .Equal (t ,
805+ "*RequestHTTPClient.client is nil. Use NewRequestHTTPClient to initialize" ,
806+ err .Error (),
807+ )
808+ })
809+
810+ t .Run ("malformedClient" , func (t * testing.T ) {
811+ t .Parallel ()
812+
813+ incompleteClient := http.Client {}
814+ c := NewRequestHTTPClient ()
815+
816+ c .client = & incompleteClient
817+
818+ _ , err := c .SetTransportOverride ("http://localhost" )
819+
820+ require .Error (t , err )
821+
822+ assert .Equal (t ,
823+ "expected *http.Transport, got <nil>" ,
824+ err .Error (),
825+ )
826+ })
827+ }
828+
727829// Test SetTransportOverride method for RequestHTTPClient.
728830// Use case: we want our client to redirect HTTPS request meant for https://hostname to a
729831// third party proxy. This in order to test the settings of that proxy before pointing to it the DNS
@@ -927,6 +1029,57 @@ func TestRequestHTTPClient_SetProxyProtocolV2_server(t *testing.T) {
9271029 }
9281030}
9291031
1032+ func TestRequestHTTPClient_SetProxyProtocolHeader_Error (t * testing.T ) {
1033+ t .Run ("nilClient" , func (t * testing.T ) {
1034+ t .Parallel ()
1035+
1036+ c := RequestHTTPClient {transportAddress : "127.0.0.1:443" }
1037+ header := proxyproto.Header {}
1038+ _ , err := c .SetProxyProtocolHeader (header )
1039+
1040+ require .Error (t , err )
1041+ assert .Equal (t ,
1042+ "*RequestHTTPClient.client is nil. Use NewRequestHTTPClient to initialize" ,
1043+ err .Error (),
1044+ )
1045+ })
1046+
1047+ t .Run ("malformedClient" , func (t * testing.T ) {
1048+ t .Parallel ()
1049+
1050+ incompleteClient := http.Client {}
1051+ c := NewRequestHTTPClient ()
1052+ c .transportAddress = "127.0.0.1:443"
1053+ c .client = & incompleteClient
1054+
1055+ header := proxyproto.Header {}
1056+ _ , err := c .SetProxyProtocolHeader (header )
1057+ require .Error (t , err )
1058+
1059+ assert .Equal (t ,
1060+ "expected *http.Transport, got <nil>" ,
1061+ err .Error (),
1062+ )
1063+ })
1064+
1065+ t .Run ("noTransportAddress" , func (t * testing.T ) {
1066+ t .Parallel ()
1067+
1068+ incompleteClient := http.Client {}
1069+ c := NewRequestHTTPClient ()
1070+ c .client = & incompleteClient
1071+
1072+ header := proxyproto.Header {}
1073+ _ , err := c .SetProxyProtocolHeader (header )
1074+ require .Error (t , err )
1075+
1076+ assert .Equal (t ,
1077+ "SetProxyProtocolHeader failed: transportOverrideURL not set" ,
1078+ err .Error (),
1079+ )
1080+ })
1081+ }
1082+
9301083func TestPrintCmd (t * testing.T ) {
9311084 tests := []bool {true , false }
9321085
0 commit comments