mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-06 09:30:56 +00:00
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:
committed by
Kubernetes Publisher
parent
88713b741f
commit
fd09dceb88
@@ -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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user