diff --git a/pkg/client/client.go b/pkg/client/client.go index 3a67d898cdb..a53101fdbdb 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -106,7 +106,11 @@ func (c *Client) doRequest(request *http.Request) ([]byte, error) { if response.StatusCode == http.StatusAccepted { var status api.Status if err := api.DecodeInto(body, &status); err == nil { - return nil, &StatusErr{status} + if status.Status == api.StatusSuccess { + return body, nil + } else { + return nil, &StatusErr{status} + } } // Sometimes the server returns 202 even though it completely handled the request. } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 080c83d5d57..0fc4b172a4b 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -437,3 +437,32 @@ func TestDoRequestAccepted(t *testing.T) { } fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil) } + +func TestDoRequestAcceptedSuccess(t *testing.T) { + status := api.Status{Status: api.StatusSuccess} + expectedBody, _ := api.Encode(status) + fakeHandler := util.FakeHandler{ + StatusCode: 202, + ResponseBody: string(expectedBody), + T: t, + } + testServer := httptest.NewTLSServer(&fakeHandler) + request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil) + auth := AuthInfo{User: "user", Password: "pass"} + c := New(testServer.URL, &auth) + body, err := c.doRequest(request) + if request.Header["Authorization"] == nil { + t.Errorf("Request is missing authorization header: %#v", *request) + } + if err != nil { + t.Errorf("Unexpected error %#v", err) + } + statusOut, err := api.Decode(body) + if err != nil { + t.Errorf("Unexpected error %#v", err) + } + if !reflect.DeepEqual(&status, statusOut) { + t.Errorf("Unexpected mis-match. Expected %#v. Saw %#v", status, statusOut) + } + fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil) +} diff --git a/pkg/client/request.go b/pkg/client/request.go index 30bd13ccb83..fde4d914e08 100644 --- a/pkg/client/request.go +++ b/pkg/client/request.go @@ -91,6 +91,15 @@ func (r *Request) Path(item string) *Request { return r } +// Overwrite an existing path with the path parameter. +func (r *Request) AbsPath(path string) *Request { + if r.err != nil { + return r + } + r.path = path + return r +} + // Parse the given string as a resource label selector. Optional. func (r *Request) ParseSelector(item string) *Request { if r.err != nil { @@ -136,6 +145,8 @@ func (r *Request) Body(obj interface{}) *Request { r.body = bytes.NewBuffer(data) case []byte: r.body = bytes.NewBuffer(t) + case io.Reader: + r.body = obj.(io.Reader) default: data, err := api.Encode(obj) if err != nil { diff --git a/pkg/client/request_test.go b/pkg/client/request_test.go index a9bc8c1c39b..41c8ae62e32 100644 --- a/pkg/client/request_test.go +++ b/pkg/client/request_test.go @@ -17,6 +17,7 @@ limitations under the License. package client import ( + "bytes" "io/ioutil" "net/http/httptest" "reflect" @@ -65,6 +66,45 @@ func TestDoRequestNewWay(t *testing.T) { } } +func TestDoRequestNewWayReader(t *testing.T) { + reqObj := &api.Pod{JSONBase: api.JSONBase{ID: "foo"}} + reqBodyExpected, _ := api.Encode(reqObj) + expectedObj := &api.Service{Port: 12345} + expectedBody, _ := api.Encode(expectedObj) + fakeHandler := util.FakeHandler{ + StatusCode: 200, + ResponseBody: string(expectedBody), + T: t, + } + testServer := httptest.NewTLSServer(&fakeHandler) + auth := AuthInfo{User: "user", Password: "pass"} + s := New(testServer.URL, &auth) + obj, err := s.Verb("POST"). + Path("foo/bar"). + Path("baz"). + Selector(labels.Set{"name": "foo"}.AsSelector()). + Timeout(time.Second). + Body(bytes.NewBuffer(reqBodyExpected)). + Do().Get() + if err != nil { + t.Errorf("Unexpected error: %v %#v", err, err) + return + } + if obj == nil { + t.Error("nil obj") + } else if !reflect.DeepEqual(obj, expectedObj) { + t.Errorf("Expected: %#v, got %#v", expectedObj, obj) + } + tmpStr := string(reqBodyExpected) + fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz", "POST", &tmpStr) + if fakeHandler.RequestReceived.URL.RawQuery != "labels=name%3Dfoo&timeout=1s" { + t.Errorf("Unexpected query: %v", fakeHandler.RequestReceived.URL.RawQuery) + } + if fakeHandler.RequestReceived.Header["Authorization"] == nil { + t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived) + } +} + func TestDoRequestNewWayObj(t *testing.T) { reqObj := &api.Pod{JSONBase: api.JSONBase{ID: "foo"}} reqBodyExpected, _ := api.Encode(reqObj) @@ -164,3 +204,12 @@ func TestVerbs(t *testing.T) { t.Errorf("Delete verb is wrong") } } + +func TestAbsPath(t *testing.T) { + expectedPath := "/bar/foo" + c := New("", nil) + r := c.Post().Path("/foo").AbsPath(expectedPath) + if r.path != expectedPath { + t.Errorf("unexpected path: %s, expected %s", r.path, expectedPath) + } +} diff --git a/pkg/cloudcfg/proxy_server.go b/pkg/cloudcfg/proxy_server.go index f0ae0bbda7f..040ce865e73 100644 --- a/pkg/cloudcfg/proxy_server.go +++ b/pkg/cloudcfg/proxy_server.go @@ -62,13 +62,11 @@ func (s *ProxyServer) doError(w http.ResponseWriter, err error) { } func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { - result := s.Client.Verb(r.Method).Path(r.URL.Path).Do() + result := s.Client.Verb(r.Method).AbsPath(r.URL.Path).Body(r.Body).Do() if result.Error() != nil { s.doError(w, result.Error()) return } - w.WriteHeader(http.StatusOK) - w.Header().Add("Content-type", "application/json") data, err := result.Raw() if err != nil { s.doError(w, err)