mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Several small fixes.
This commit is contained in:
parent
20ba523266
commit
84e5c37f74
@ -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.
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user