Skip to content

Commit 0a0d316

Browse files
authored
feat: add option to set custom http.Client on HTTPRequester (#335)
* feat: add option to set custom http.Client on HTTPRequester * incorporate updates * license formatting
1 parent 4313a44 commit 0a0d316

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

pkg/utils/requester.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019,2022 Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -21,12 +21,13 @@ import (
2121
"bytes"
2222
"errors"
2323
"fmt"
24-
"github.com/optimizely/go-sdk/pkg/logging"
2524
"io"
2625
"io/ioutil"
2726
"net/http"
2827
"time"
2928

29+
"github.com/optimizely/go-sdk/pkg/logging"
30+
3031
jsoniter "github.com/json-iterator/go"
3132
)
3233

@@ -50,10 +51,17 @@ type Header struct {
5051
Name, Value string
5152
}
5253

54+
// Client sets http client
55+
func Client(client http.Client) func(r *HTTPRequester) {
56+
return func(r *HTTPRequester) {
57+
r.client = client
58+
}
59+
}
60+
5361
// Timeout sets http client timeout
5462
func Timeout(timeout time.Duration) func(r *HTTPRequester) {
5563
return func(r *HTTPRequester) {
56-
r.client = http.Client{Timeout: timeout}
64+
r.client.Timeout = timeout
5765
}
5866
}
5967

pkg/utils/requester_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, 2021 Optimizely, Inc. and contributors *
2+
* Copyright 2019,2021-2022 Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -19,17 +19,36 @@ package utils
1919
import (
2020
"errors"
2121
"fmt"
22-
"github.com/optimizely/go-sdk/pkg/logging"
2322
"log"
2423
"net/http"
2524
"net/http/httptest"
2625
"net/url"
2726
"testing"
2827
"time"
2928

29+
"github.com/optimizely/go-sdk/pkg/logging"
30+
3031
"github.com/stretchr/testify/assert"
3132
)
3233

34+
func TestClientFunction(t *testing.T) {
35+
requester := &HTTPRequester{}
36+
fn := Client(http.Client{Timeout: 125})
37+
fn(requester)
38+
assert.Equal(t, time.Duration(125), requester.client.Timeout)
39+
}
40+
41+
func TestNewHTTPRequesterWithClient(t *testing.T) {
42+
fn := Client(http.Client{Timeout: 125})
43+
requester := NewHTTPRequester(logging.GetLogger("", ""), fn)
44+
assert.Equal(t, time.Duration(125), requester.client.Timeout)
45+
}
46+
47+
func TestNewHTTPRequesterWithoutClient(t *testing.T) {
48+
requester := NewHTTPRequester(logging.GetLogger("", ""))
49+
assert.Equal(t, defaultTTL, requester.client.Timeout)
50+
}
51+
3352
func TestHeaders(t *testing.T) {
3453
requester := &HTTPRequester{}
3554
fn := Headers(Header{"one", "1"})

0 commit comments

Comments
 (0)