@@ -147,3 +147,170 @@ func (r mockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
147147 r .t .Logf ("error wriging bytes: %v" , err )
148148 }
149149}
150+
151+ func TestWithFromEnv (t * testing.T ) {
152+ tests := []struct {
153+ name string
154+ envVars map [string ]string
155+ initialConfig outbound.Configuration
156+ wantBaseURI string
157+ wantTimeout time.Duration
158+ wantHeaders map [string ]string
159+ }{
160+ {
161+ name : "configure endpoint from env" ,
162+ envVars : map [string ]string {
163+ "OFREP_ENDPOINT" : "http://test.example.com" ,
164+ },
165+ initialConfig : outbound.Configuration {},
166+ wantBaseURI : "http://test.example.com" ,
167+ },
168+ {
169+ name : "configure timeout from env" ,
170+ envVars : map [string ]string {
171+ "OFREP_TIMEOUT" : "5s" ,
172+ },
173+ initialConfig : outbound.Configuration {},
174+ wantTimeout : 5 * time .Second ,
175+ },
176+ {
177+ name : "configure timeout from env with raw milliseconds" ,
178+ envVars : map [string ]string {
179+ "OFREP_TIMEOUT" : "3000" ,
180+ },
181+ initialConfig : outbound.Configuration {},
182+ wantTimeout : 3 * time .Second ,
183+ },
184+ {
185+ name : "configure timeout from env with invalid data" ,
186+ envVars : map [string ]string {
187+ "OFREP_TIMEOUT" : "s5s" ,
188+ },
189+ initialConfig : outbound.Configuration {Timeout : 33 * time .Second },
190+ wantTimeout : 33 * time .Second ,
191+ },
192+ {
193+ name : "configure timeout from env with negative duration" ,
194+ envVars : map [string ]string {
195+ "OFREP_TIMEOUT" : "-5s" ,
196+ },
197+ initialConfig : outbound.Configuration {Timeout : 33 * time .Second },
198+ wantTimeout : 33 * time .Second ,
199+ },
200+ {
201+ name : "configure timeout from env with negative milliseconds" ,
202+ envVars : map [string ]string {
203+ "OFREP_TIMEOUT" : "-5000" ,
204+ },
205+ initialConfig : outbound.Configuration {Timeout : 33 * time .Second },
206+ wantTimeout : 33 * time .Second ,
207+ },
208+ {
209+ name : "ignore invalid timeout" ,
210+ envVars : map [string ]string {
211+ "OFREP_TIMEOUT" : "invalid" ,
212+ },
213+ initialConfig : outbound.Configuration {Timeout : 10 * time .Second },
214+ wantTimeout : 10 * time .Second ,
215+ },
216+ {
217+ name : "configure api key from env" ,
218+ envVars : map [string ]string {
219+ "OFREP_API_KEY" : "test-api-key" ,
220+ },
221+ initialConfig : outbound.Configuration {},
222+ wantHeaders : map [string ]string {
223+ "X-API-Key" : "test-api-key" ,
224+ },
225+ },
226+ {
227+ name : "configure bearer token from env" ,
228+ envVars : map [string ]string {
229+ "OFREP_BEARER_TOKEN" : "test-token" ,
230+ },
231+ initialConfig : outbound.Configuration {},
232+ wantHeaders : map [string ]string {
233+ "Authorization" : "Bearer test-token" ,
234+ },
235+ },
236+ {
237+ name : "configure custom headers from env" ,
238+ envVars : map [string ]string {
239+ "OFREP_HEADERS" : "X-Custom-1=Value1,X-Custom-2=Value2" ,
240+ },
241+ initialConfig : outbound.Configuration {},
242+ wantHeaders : map [string ]string {
243+ "X-Custom-1" : "Value1" ,
244+ "X-Custom-2" : "Value2" ,
245+ },
246+ },
247+ {
248+ name : "configure all options from env" ,
249+ envVars : map [string ]string {
250+ "OFREP_ENDPOINT" : "http://all.example.com" ,
251+ "OFREP_TIMEOUT" : "3s" ,
252+ "OFREP_API_KEY" : "all-key" ,
253+ "OFREP_HEADERS" : "X-Test=TestValue" ,
254+ },
255+ initialConfig : outbound.Configuration {},
256+ wantBaseURI : "http://all.example.com" ,
257+ wantTimeout : 3 * time .Second ,
258+ wantHeaders : map [string ]string {
259+ "X-API-Key" : "all-key" ,
260+ "X-Test" : "TestValue" ,
261+ },
262+ },
263+ {
264+ name : "empty env variables do not override defaults" ,
265+ envVars : map [string ]string {
266+ "OFREP_ENDPOINT" : "" ,
267+ "OFREP_TIMEOUT" : "" ,
268+ },
269+ initialConfig : outbound.Configuration {
270+ BaseURI : "http://default.example.com" ,
271+ Timeout : 15 * time .Second ,
272+ },
273+ wantBaseURI : "http://default.example.com" ,
274+ wantTimeout : 15 * time .Second ,
275+ },
276+ }
277+
278+ for _ , tt := range tests {
279+ t .Run (tt .name , func (t * testing.T ) {
280+ for key , value := range tt .envVars {
281+ t .Setenv (key , value )
282+ }
283+
284+ c := tt .initialConfig
285+ WithFromEnv ()(& c )
286+
287+ if tt .wantBaseURI != "" && c .BaseURI != tt .wantBaseURI {
288+ t .Errorf ("expected BaseURI %s, but got %s" , tt .wantBaseURI , c .BaseURI )
289+ }
290+
291+ if tt .wantTimeout != 0 && c .Timeout != tt .wantTimeout {
292+ t .Errorf ("expected Timeout %v, but got %v" , tt .wantTimeout , c .Timeout )
293+ }
294+
295+ actualHeaders := make (map [string ]string )
296+ for _ , cb := range c .Callbacks {
297+ k , v := cb ()
298+ actualHeaders [k ] = v
299+ }
300+
301+ if tt .wantHeaders != nil {
302+ for expectedKey , expectedValue := range tt .wantHeaders {
303+ if actualValue , ok := actualHeaders [expectedKey ]; ! ok {
304+ t .Errorf ("expected header %s not found" , expectedKey )
305+ } else if actualValue != expectedValue {
306+ t .Errorf ("expected %s=%s, but got %s=%s" , expectedKey , expectedValue , expectedKey , actualValue )
307+ }
308+ }
309+ }
310+
311+ if len (tt .wantHeaders ) == 0 && len (actualHeaders ) != 0 {
312+ t .Errorf ("expected no headers, but got %v" , actualHeaders )
313+ }
314+ })
315+ }
316+ }
0 commit comments