@@ -15,6 +15,7 @@ import (
15
15
16
16
flagsmith "github.com/Flagsmith/flagsmith-go-client/v4"
17
17
"github.com/Flagsmith/flagsmith-go-client/v4/fixtures"
18
+ "github.com/go-resty/resty/v2"
18
19
"github.com/stretchr/testify/assert"
19
20
)
20
21
@@ -1019,3 +1020,160 @@ type writerFunc func(p []byte) (n int, err error)
1019
1020
func (f writerFunc ) Write (p []byte ) (n int , err error ) {
1020
1021
return f (p )
1021
1022
}
1023
+
1024
+ // Helper function to implement a header interceptor.
1025
+ func roundTripperWithHeader (key , value string ) http.RoundTripper {
1026
+ return & injectHeaderTransport {key : key , value : value }
1027
+ }
1028
+
1029
+ type injectHeaderTransport struct {
1030
+ key string
1031
+ value string
1032
+ }
1033
+
1034
+ func (t * injectHeaderTransport ) RoundTrip (req * http.Request ) (* http.Response , error ) {
1035
+ req .Header .Set (t .key , t .value )
1036
+ return http .DefaultTransport .RoundTrip (req )
1037
+ }
1038
+
1039
+ func TestCustomHTTPClientIsUsed (t * testing.T ) {
1040
+ ctx := context .Background ()
1041
+
1042
+ hasCustomHeader := false
1043
+ server := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
1044
+ assert .Equal (t , "/api/v1/flags/" , req .URL .Path )
1045
+ assert .Equal (t , fixtures .EnvironmentAPIKey , req .Header .Get ("x-Environment-Key" ))
1046
+ if req .Header .Get ("X-Test-Client" ) == "http" {
1047
+ hasCustomHeader = true
1048
+ }
1049
+ rw .Header ().Set ("Content-Type" , "application/json" )
1050
+ rw .WriteHeader (http .StatusOK )
1051
+ _ , err := io .WriteString (rw , fixtures .FlagsJson )
1052
+ assert .NoError (t , err )
1053
+ }))
1054
+ defer server .Close ()
1055
+
1056
+ customClient := & http.Client {
1057
+ Transport : roundTripperWithHeader ("X-Test-Client" , "http" ),
1058
+ }
1059
+
1060
+ client := flagsmith .NewClient (fixtures .EnvironmentAPIKey ,
1061
+ flagsmith .WithHTTPClient (customClient ),
1062
+ flagsmith .WithBaseURL (server .URL + "/api/v1/" ))
1063
+
1064
+ flags , err := client .GetFlags (ctx , nil )
1065
+ assert .Equal (t , 1 , len (flags .AllFlags ()))
1066
+ assert .NoError (t , err )
1067
+ assert .True (t , hasCustomHeader , "Expected http header" )
1068
+ flag , err := flags .GetFlag (fixtures .Feature1Name )
1069
+ assert .NoError (t , err )
1070
+ assert .Equal (t , fixtures .Feature1Value , flag .Value )
1071
+ }
1072
+
1073
+ func TestCustomRestyClientIsUsed (t * testing.T ) {
1074
+ ctx := context .Background ()
1075
+
1076
+ hasCustomHeader := false
1077
+ server := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
1078
+ if req .Header .Get ("X-Custom-Test-Header" ) == "resty" {
1079
+ hasCustomHeader = true
1080
+ }
1081
+ rw .Header ().Set ("Content-Type" , "application/json" )
1082
+ rw .WriteHeader (http .StatusOK )
1083
+ _ , err := io .WriteString (rw , fixtures .FlagsJson )
1084
+ assert .NoError (t , err )
1085
+ }))
1086
+ defer server .Close ()
1087
+
1088
+ restyClient := resty .New ().
1089
+ SetHeader ("X-Custom-Test-Header" , "resty" )
1090
+
1091
+ client := flagsmith .NewClient (fixtures .EnvironmentAPIKey ,
1092
+ flagsmith .WithRestyClient (restyClient ),
1093
+ flagsmith .WithBaseURL (server .URL + "/api/v1/" ))
1094
+
1095
+ flags , err := client .GetFlags (ctx , nil )
1096
+ assert .NoError (t , err )
1097
+ assert .Equal (t , 1 , len (flags .AllFlags ()))
1098
+ assert .True (t , hasCustomHeader , "Expected custom resty header" )
1099
+ }
1100
+
1101
+ func TestRestyClientOverridesHTTPClientShouldPanic (t * testing.T ) {
1102
+ httpClient := & http.Client {
1103
+ Transport : roundTripperWithHeader ("X-Test-Client" , "http" ),
1104
+ }
1105
+
1106
+ restyClient := resty .New ().
1107
+ SetHeader ("X-Test-Client" , "resty" )
1108
+
1109
+ assert .Panics (t , func () {
1110
+ _ = flagsmith .NewClient (fixtures .EnvironmentAPIKey ,
1111
+ flagsmith .WithHTTPClient (httpClient ),
1112
+ flagsmith .WithRestyClient (restyClient ),
1113
+ flagsmith .WithBaseURL ("http://example.com/api/v1/" ))
1114
+ }, "Expected panic when both HTTP and Resty clients are provided" )
1115
+ }
1116
+
1117
+ func TestDefaultRestyClientIsUsed (t * testing.T ) {
1118
+ ctx := context .Background ()
1119
+
1120
+ serverCalled := false
1121
+
1122
+ server := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
1123
+ serverCalled = true
1124
+
1125
+ assert .Equal (t , "/api/v1/flags/" , req .URL .Path )
1126
+ assert .Equal (t , fixtures .EnvironmentAPIKey , req .Header .Get ("x-Environment-Key" ))
1127
+
1128
+ rw .Header ().Set ("Content-Type" , "application/json" )
1129
+ rw .WriteHeader (http .StatusOK )
1130
+ _ , err := io .WriteString (rw , fixtures .FlagsJson )
1131
+ assert .NoError (t , err )
1132
+ }))
1133
+ defer server .Close ()
1134
+
1135
+ client := flagsmith .NewClient (fixtures .EnvironmentAPIKey ,
1136
+ flagsmith .WithBaseURL (server .URL + "/api/v1/" ))
1137
+
1138
+ flags , err := client .GetFlags (ctx , nil )
1139
+
1140
+ assert .NoError (t , err )
1141
+ assert .True (t , serverCalled , "Expected server to be" )
1142
+ assert .Equal (t , 1 , len (flags .AllFlags ()))
1143
+ }
1144
+
1145
+ func TestCustomClientOptionsShoudPanic (t * testing.T ) {
1146
+ restyClient := resty .New ()
1147
+
1148
+ testCases := []struct {
1149
+ name string
1150
+ option flagsmith.Option
1151
+ }{
1152
+ {
1153
+ name : "WithRequestTimeout" ,
1154
+ option : flagsmith .WithRequestTimeout (5 * time .Second ),
1155
+ },
1156
+ {
1157
+ name : "WithRetries" ,
1158
+ option : flagsmith .WithRetries (3 , time .Second ),
1159
+ },
1160
+ {
1161
+ name : "WithCustomHeaders" ,
1162
+ option : flagsmith .WithCustomHeaders (map [string ]string {"X-Custom" : "value" }),
1163
+ },
1164
+ {
1165
+ name : "WithProxy" ,
1166
+ option : flagsmith .WithProxy ("http://proxy.example.com" ),
1167
+ },
1168
+ }
1169
+
1170
+ for _ , test := range testCases {
1171
+ t .Run (test .name , func (t * testing.T ) {
1172
+ assert .Panics (t , func () {
1173
+ _ = flagsmith .NewClient (fixtures .EnvironmentAPIKey ,
1174
+ flagsmith .WithRestyClient (restyClient ),
1175
+ test .option )
1176
+ }, "Expected panic when using %s with custom resty client" , test .name )
1177
+ })
1178
+ }
1179
+ }
0 commit comments