本文整理汇总了Golang中github.com/GoogleCloudPlatform/kubernetes/pkg/util.FakeHandler.ValidateRequest方法的典型用法代码示例。如果您正苦于以下问题:Golang FakeHandler.ValidateRequest方法的具体用法?Golang FakeHandler.ValidateRequest怎么用?Golang FakeHandler.ValidateRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/GoogleCloudPlatform/kubernetes/pkg/util.FakeHandler
的用法示例。
在下文中一共展示了FakeHandler.ValidateRequest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestBind
func TestBind(t *testing.T) {
table := []struct {
binding *api.Binding
}{
{binding: &api.Binding{
ObjectMeta: api.ObjectMeta{
Namespace: api.NamespaceDefault,
Name: "foo",
},
Target: api.ObjectReference{
Name: "foohost.kubernetes.mydomain.com",
},
}},
}
for _, item := range table {
handler := util.FakeHandler{
StatusCode: 200,
ResponseBody: "",
T: t,
}
server := httptest.NewServer(&handler)
defer server.Close()
client := client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()})
b := binder{client}
if err := b.Bind(item.binding); err != nil {
t.Errorf("Unexpected error: %v", err)
continue
}
expectedBody := runtime.EncodeOrDie(testapi.Codec(), item.binding)
handler.ValidateRequest(t, testapi.ResourcePath("bindings", api.NamespaceDefault, ""), "POST", &expectedBody)
}
}
示例2: TestDoRequestAcceptedSuccess
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)
}
示例3: TestCreatePod
func TestCreatePod(t *testing.T) {
requestPod := api.Pod{
CurrentState: api.PodState{
Status: "Foobar",
},
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
}
body, _ := json.Marshal(requestPod)
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
}
testServer := httptest.NewTLSServer(&fakeHandler)
client := Client{
Host: testServer.URL,
}
receivedPod, err := client.CreatePod(requestPod)
fakeHandler.ValidateRequest(t, makeUrl("/pods"), "POST", nil)
if err != nil {
t.Errorf("Unexpected error: %#v", err)
}
if !reflect.DeepEqual(requestPod, receivedPod) {
t.Errorf("Received pod: %#v\n doesn't match expected pod: %#v", receivedPod, requestPod)
}
testServer.Close()
}
示例4: TestDoRequestAccepted
func TestDoRequestAccepted(t *testing.T) {
status := &api.Status{Status: api.StatusWorking}
expectedBody, _ := latest.Codec.Encode(status)
fakeHandler := util.FakeHandler{
StatusCode: 202,
ResponseBody: string(expectedBody),
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
c, err := RESTClientFor(&Config{Host: testServer.URL, Username: "test", Version: testapi.Version()})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
body, err := c.Get().Path("test").Do().Raw()
if err == nil {
t.Fatalf("Unexpected non-error")
}
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", fakeHandler.RequestReceived)
}
se, ok := err.(APIStatus)
if !ok {
t.Fatalf("Unexpected kind of error: %#v", err)
}
if !reflect.DeepEqual(se.Status(), *status) {
t.Errorf("Unexpected status: %#v %#v", se.Status(), status)
}
if body != nil {
t.Errorf("Expected nil body, but saw: '%s'", string(body))
}
fakeHandler.ValidateRequest(t, "/"+testapi.Version()+"/test", "GET", nil)
}
示例5: TestBind
func TestBind(t *testing.T) {
table := []struct {
binding *api.Binding
}{
{binding: &api.Binding{PodID: "foo", Host: "foohost.kubernetes.mydomain.com"}},
}
for _, item := range table {
handler := util.FakeHandler{
StatusCode: 200,
ResponseBody: "",
T: t,
}
server := httptest.NewServer(&handler)
client := client.NewOrDie(server.URL, nil)
b := binder{client}
if err := b.Bind(item.binding); err != nil {
t.Errorf("Unexpected error: %v", err)
continue
}
expectedBody := api.EncodeOrDie(item.binding)
handler.ValidateRequest(t, "/api/v1beta1/bindings", "POST", &expectedBody)
}
}
示例6: TestPollMinions
func TestPollMinions(t *testing.T) {
table := []struct {
minions []api.Minion
}{
{
minions: []api.Minion{
{JSONBase: api.JSONBase{ID: "foo"}},
{JSONBase: api.JSONBase{ID: "bar"}},
},
},
}
for _, item := range table {
ml := &api.MinionList{Items: item.minions}
handler := util.FakeHandler{
StatusCode: 200,
ResponseBody: api.EncodeOrDie(ml),
T: t,
}
server := httptest.NewServer(&handler)
cf := ConfigFactory{client.New(server.URL, nil)}
ce, err := cf.pollMinions()
if err != nil {
t.Errorf("Unexpected error: %v", err)
continue
}
handler.ValidateRequest(t, "/api/v1beta1/minions", "GET", nil)
if e, a := len(item.minions), ce.Len(); e != a {
t.Errorf("Expected %v, got %v", e, a)
}
}
}
示例7: TestDoRequestNewWayObj
func TestDoRequestNewWayObj(t *testing.T) {
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
reqBodyExpected, _ := v1beta2.Codec.Encode(reqObj)
expectedObj := &api.Service{Spec: api.ServiceSpec{Port: 12345}}
expectedBody, _ := v1beta2.Codec.Encode(expectedObj)
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(expectedBody),
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta2", Username: "user", Password: "pass"})
obj, err := c.Verb("POST").
Path("foo/bar").
Path("baz").
SelectorParam("labels", labels.Set{"name": "foo"}.AsSelector()).
Timeout(time.Second).
Body(reqObj).
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/v1beta2/foo/bar/baz?labels=name%3Dfoo", "POST", &tmpStr)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
}
示例8: TestDefaultErrorFunc
func TestDefaultErrorFunc(t *testing.T) {
testPod := &api.Pod{JSONBase: api.JSONBase{ID: "foo"}}
handler := util.FakeHandler{
StatusCode: 200,
ResponseBody: api.EncodeOrDie(testPod),
T: t,
}
mux := http.NewServeMux()
// FakeHandler musn't be sent requests other than the one you want to test.
mux.Handle("/api/v1beta1/pods/foo", &handler)
server := httptest.NewServer(mux)
factory := ConfigFactory{client.NewOrDie(server.URL, nil)}
queue := cache.NewFIFO()
errFunc := factory.makeDefaultErrorFunc(queue)
errFunc(testPod, nil)
for {
// This is a terrible way to do this but I plan on replacing this
// whole error handling system in the future. The test will time
// out if something doesn't work.
time.Sleep(10 * time.Millisecond)
got, exists := queue.Get("foo")
if !exists {
continue
}
handler.ValidateRequest(t, "/api/v1beta1/pods/foo", "GET", nil)
if e, a := testPod, got; !reflect.DeepEqual(e, a) {
t.Errorf("Expected %v, got %v", e, a)
}
break
}
}
示例9: TestUpdatePod
func TestUpdatePod(t *testing.T) {
requestPod := api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
CurrentState: api.PodState{
Status: "Foobar",
},
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
}
body, _ := json.Marshal(requestPod)
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
}
testServer := httptest.NewTLSServer(&fakeHandler)
client := Client{
Host: testServer.URL,
}
receivedPod, err := client.UpdatePod(requestPod)
fakeHandler.ValidateRequest(t, makeUrl("/pods/foo"), "PUT", nil)
if err != nil {
t.Errorf("Unexpected error: %#v", err)
}
expectEqual(t, requestPod, receivedPod)
testServer.Close()
}
示例10: TestListPods
func TestListPods(t *testing.T) {
expectedPodList := api.PodList{
Items: []api.Pod{
{
CurrentState: api.PodState{
Status: "Foobar",
},
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
},
}
body, _ := json.Marshal(expectedPodList)
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
}
testServer := httptest.NewTLSServer(&fakeHandler)
client := Client{
Host: testServer.URL,
}
receivedPodList, err := client.ListPods(nil)
fakeHandler.ValidateRequest(t, makeUrl("/pods"), "GET", nil)
if err != nil {
t.Errorf("Unexpected error in listing pods: %#v", err)
}
if !reflect.DeepEqual(expectedPodList, receivedPodList) {
t.Errorf("Unexpected pod list: %#v\nvs.\n%#v", receivedPodList, expectedPodList)
}
testServer.Close()
}
示例11: TestDoRequestNewWay
func TestDoRequestNewWay(t *testing.T) {
reqBody := "request body"
expectedObj := &api.Service{Port: 12345}
expectedBody, _ := runtime.Encode(expectedObj)
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(expectedBody),
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
auth := AuthInfo{User: "user", Password: "pass"}
c := NewOrDie(testServer.URL, &auth)
obj, err := c.Verb("POST").
Path("foo/bar").
Path("baz").
ParseSelectorParam("labels", "name=foo").
Timeout(time.Second).
Body([]byte(reqBody)).
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)
}
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo", "POST", &reqBody)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
}
示例12: TestDoRequestAccepted
func TestDoRequestAccepted(t *testing.T) {
status := api.Status{Status: api.StatusWorking}
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.Error("Unexpected non-error")
return
}
se, ok := err.(*StatusErr)
if !ok {
t.Errorf("Unexpected kind of error: %#v", err)
return
}
if !reflect.DeepEqual(se.Status, status) {
t.Errorf("Unexpected status: %#v", se.Status)
}
if body != nil {
t.Errorf("Expected nil body, but saw: '%s'", body)
}
fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil)
}
示例13: TestCreateController
func TestCreateController(t *testing.T) {
expectedController := api.ReplicationController{
JSONBase: api.JSONBase{
ID: "foo",
},
DesiredState: api.ReplicationControllerState{
Replicas: 2,
},
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
}
body, _ := json.Marshal(expectedController)
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
}
testServer := httptest.NewTLSServer(&fakeHandler)
client := Client{
Host: testServer.URL,
}
receivedController, err := client.CreateReplicationController(api.ReplicationController{
JSONBase: api.JSONBase{
ID: "foo",
},
})
expectNoError(t, err)
if !reflect.DeepEqual(expectedController, receivedController) {
t.Errorf("Unexpected controller, expected: %#v, received %#v", expectedController, receivedController)
}
fakeHandler.ValidateRequest(t, makeUrl("/replicationControllers"), "POST", nil)
testServer.Close()
}
示例14: TestDoRequestNewWay
func TestDoRequestNewWay(t *testing.T) {
reqBody := "request body"
expectedObj := &api.Service{Spec: api.ServiceSpec{Port: 12345}}
expectedBody, _ := v1beta2.Codec.Encode(expectedObj)
fakeHandler := util.FakeHandler{
StatusCode: 200,
ResponseBody: string(expectedBody),
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
c := NewOrDie(&Config{Host: testServer.URL, Version: "v1beta2", Username: "user", Password: "pass"})
obj, err := c.Verb("POST").
Prefix("foo", "bar").
Suffix("baz").
ParseSelectorParam("labels", "name=foo").
Timeout(time.Second).
Body([]byte(reqBody)).
Do().Get()
if err != nil {
t.Errorf("Unexpected error: %v %#v", err, err)
return
}
if obj == nil {
t.Error("nil obj")
} else if !api.Semantic.DeepDerivative(expectedObj, obj) {
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
}
fakeHandler.ValidateRequest(t, "/api/v1beta2/foo/bar/baz?labels=name%3Dfoo&timeout=1s", "POST", &reqBody)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
}
示例15: TestDoRequestCreated
func TestDoRequestCreated(t *testing.T) {
status := &api.Status{Status: api.StatusSuccess}
expectedBody, _ := latest.Codec.Encode(status)
fakeHandler := util.FakeHandler{
StatusCode: 201,
ResponseBody: string(expectedBody),
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
c, err := RESTClientFor(&Config{Host: testServer.URL, Username: "user", Password: "pass", Version: testapi.Version()})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
created := false
body, err := c.Get().Path("test").Do().WasCreated(&created).Raw()
if err != nil {
t.Errorf("Unexpected error %#v", err)
}
if !created {
t.Errorf("Expected object to be created")
}
statusOut, err := latest.Codec.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, "/"+testapi.Version()+"/test", "GET", nil)
}