mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Merge pull request #1533 from jhadvig/upstream_k8_stream_new
Support for streaming
This commit is contained in:
commit
c5fa765926
@ -224,6 +224,27 @@ func (r *Request) Watch() (watch.Interface, error) {
|
|||||||
return watch.NewStreamWatcher(watchjson.NewDecoder(response.Body, r.c.Codec)), nil
|
return watch.NewStreamWatcher(watchjson.NewDecoder(response.Body, r.c.Codec)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stream formats and executes the request, and offers streaming of the response.
|
||||||
|
// Returns io.ReadCloser which could be used for streaming of the response, or an error
|
||||||
|
func (r *Request) Stream() (io.ReadCloser, error) {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest(r.verb, r.finalURL(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
client := r.c.Client
|
||||||
|
if client == nil {
|
||||||
|
client = http.DefaultClient
|
||||||
|
}
|
||||||
|
response, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.Body, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Do formats and executes the request. Returns the API object received, or an error.
|
// Do formats and executes the request. Returns the API object received, or an error.
|
||||||
func (r *Request) Do() Result {
|
func (r *Request) Do() Result {
|
||||||
for {
|
for {
|
||||||
|
@ -439,3 +439,42 @@ func TestWatch(t *testing.T) {
|
|||||||
t.Fatal("Unexpected non-close")
|
t.Fatal("Unexpected non-close")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStream(t *testing.T) {
|
||||||
|
auth := &Config{Username: "user", Password: "pass"}
|
||||||
|
expectedBody := "expected body"
|
||||||
|
|
||||||
|
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
checkAuth(t, auth, r)
|
||||||
|
flusher, ok := w.(http.Flusher)
|
||||||
|
if !ok {
|
||||||
|
panic("need flusher!")
|
||||||
|
}
|
||||||
|
w.Header().Set("Transfer-Encoding", "chunked")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte(expectedBody))
|
||||||
|
flusher.Flush()
|
||||||
|
}))
|
||||||
|
|
||||||
|
s, err := New(&Config{
|
||||||
|
Host: testServer.URL,
|
||||||
|
Version: "v1beta1",
|
||||||
|
Username: "user",
|
||||||
|
Password: "pass",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
readCloser, err := s.Get().Path("path/to/stream/thing").Stream()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
defer readCloser.Close()
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
buf.ReadFrom(readCloser)
|
||||||
|
resultBody := buf.String()
|
||||||
|
|
||||||
|
if expectedBody != resultBody {
|
||||||
|
t.Errorf("Expected %s, got %s", expectedBody, resultBody)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user