Use json response to validate method sent via proxy

This commit is contained in:
Stephen Heywood 2021-01-18 12:52:15 +13:00
parent e29a3f21bc
commit 6c1f3b7d5d

View File

@ -21,6 +21,7 @@ package network
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/json"
"fmt" "fmt"
"math" "math"
"net/http" "net/http"
@ -59,6 +60,11 @@ const (
podRetryTimeout = 1 * time.Minute podRetryTimeout = 1 * time.Minute
) )
type jsonResponse struct {
Method string
Body string
}
var _ = SIGDescribe("Proxy", func() { var _ = SIGDescribe("Proxy", func() {
version := "v1" version := "v1"
ginkgo.Context("version "+version, func() { ginkgo.Context("version "+version, func() {
@ -280,7 +286,7 @@ var _ = SIGDescribe("Proxy", func() {
Containers: []v1.Container{{ Containers: []v1.Container{{
Image: imageutils.GetE2EImage(imageutils.Agnhost), Image: imageutils.GetE2EImage(imageutils.Agnhost),
Name: "agnhost", Name: "agnhost",
Command: []string{"/agnhost", "porter"}, Command: []string{"/agnhost", "porter", "--json-response"},
Env: []v1.EnvVar{{ Env: []v1.EnvVar{{
Name: "SERVE_PORT_80", Name: "SERVE_PORT_80",
Value: msg, Value: msg,
@ -327,7 +333,7 @@ var _ = SIGDescribe("Proxy", func() {
} }
// All methods for Pod ProxyWithPath return 200 // All methods for Pod ProxyWithPath return 200
// response body returns 'foo' for all methods but HEAD // For all methods other than HEAD the response body returns 'foo' with the received http method
httpVerbs := []string{"DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"} httpVerbs := []string{"DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"}
for _, httpVerb := range httpVerbs { for _, httpVerb := range httpVerbs {
@ -344,16 +350,25 @@ var _ = SIGDescribe("Proxy", func() {
buf.ReadFrom(resp.Body) buf.ReadFrom(resp.Body)
response := buf.String() response := buf.String()
framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s", httpVerb, resp.StatusCode, response) switch httpVerb {
framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) case "HEAD":
framework.Logf("http.Client request:%s | StatusCode:%d", httpVerb, resp.StatusCode)
framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode)
default:
var jr *jsonResponse
err = json.Unmarshal([]byte(response), &jr)
framework.ExpectNoError(err, "Failed to process jsonResponse: %v | err: %v ", buf.String(), err)
if httpVerb != "HEAD" { framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s | Method: %s", httpVerb, resp.StatusCode, jr.Body, jr.Method)
framework.ExpectEqual(response, msg, "The resp.Body returned: %v", resp.Body) framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode)
framework.ExpectEqual(msg, jr.Body, "The resp.Body returned: %v", jr.Body)
framework.ExpectEqual(httpVerb, jr.Method, "The resp.Body returned: %v", jr.Body)
} }
} }
// All methods for Service ProxyWithPath return 200 // All methods for Service ProxyWithPath return 200
// response body returns 'foo' for all methods but HEAD // For all methods other than HEAD the response body returns 'foo' with the received http method
for _, httpVerb := range httpVerbs { for _, httpVerb := range httpVerbs {
urlString := f.ClientConfig().Host + "/api/v1/namespaces/" + ns + "/services/test-service/proxy/some/path/with/" + httpVerb urlString := f.ClientConfig().Host + "/api/v1/namespaces/" + ns + "/services/test-service/proxy/some/path/with/" + httpVerb
@ -369,11 +384,20 @@ var _ = SIGDescribe("Proxy", func() {
buf.ReadFrom(resp.Body) buf.ReadFrom(resp.Body)
response := buf.String() response := buf.String()
framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s", httpVerb, resp.StatusCode, response) switch httpVerb {
framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode) case "HEAD":
framework.Logf("http.Client request:%s | StatusCode:%d", httpVerb, resp.StatusCode)
framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode)
default:
var jr *jsonResponse
err = json.Unmarshal([]byte(response), &jr)
framework.ExpectNoError(err, "Failed to process jsonResponse: %v | err: %v ", buf.String(), err)
if httpVerb != "HEAD" { framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s | Method: %s", httpVerb, resp.StatusCode, jr.Body, jr.Method)
framework.ExpectEqual(response, msg, "The resp.Body returned: %v", resp.Body) framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode)
framework.ExpectEqual(msg, jr.Body, "The resp.Body returned: %v", jr.Body)
framework.ExpectEqual(httpVerb, jr.Method, "The resp.Body returned: %v", jr.Body)
} }
} }
}) })