RESTClient contructors for config and http client

Add two new constructors for versioned and unversioned RESTClients.

These new constructors allow to pass an http.Client to the RESTClient,
taking precence over the transport Config parameters.

Add a new helper function to generate an http.Client from the RESTClient
Config object.

Co-authored-by: Jordan Liggitt <liggitt@google.com>

Kubernetes-commit: 80fbc817263de1c46e1493819aca35b1ef8e3d09
This commit is contained in:
Antonio Ojea
2021-10-09 00:17:04 +02:00
committed by Kubernetes Publisher
parent 88713b741f
commit fd09dceb88
3 changed files with 105 additions and 26 deletions

View File

@@ -26,6 +26,7 @@ import (
"net/url"
"os"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
@@ -162,3 +163,33 @@ func TestReconnectBrokenTCP(t *testing.T) {
t.Fatalf("expected %d dials, got %d", 2, dials)
}
}
func TestRestClientTimeout(t *testing.T) {
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
fmt.Fprintf(w, "Hello, %s", r.Proto)
}))
ts.Start()
defer ts.Close()
config := &Config{
Host: ts.URL,
Timeout: 1 * time.Second,
// These fields are required to create a REST client.
ContentConfig: ContentConfig{
GroupVersion: &schema.GroupVersion{},
NegotiatedSerializer: &serializer.CodecFactory{},
},
}
client, err := RESTClientFor(config)
if err != nil {
t.Fatalf("failed to create REST client: %v", err)
}
_, err = client.Get().AbsPath("/").DoRaw(context.TODO())
if err == nil {
t.Fatalf("timeout error expected")
}
if !strings.Contains(err.Error(), "deadline exceeded") {
t.Fatalf("timeout error expected, received %v", err)
}
}