mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
cleanup misleading errors in openAPI tesing (#91321)
* cleanup misleading errors in openAPI tesing * make sure test case would fail
This commit is contained in:
parent
dee4a7cd84
commit
0459c2afab
@ -454,8 +454,7 @@ func returnedOpenAPI() *openapi_v2.Document {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func openapiSchemaDeprecatedFakeServer(status int) (*httptest.Server, error) {
|
func openapiSchemaDeprecatedFakeServer(status int, t *testing.T) (*httptest.Server, error) {
|
||||||
var sErr error
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.URL.Path == "/openapi/v2" {
|
if req.URL.Path == "/openapi/v2" {
|
||||||
// write the error status for the new endpoint request
|
// write the error status for the new endpoint request
|
||||||
@ -463,54 +462,81 @@ func openapiSchemaDeprecatedFakeServer(status int) (*httptest.Server, error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.URL.Path != "/swagger-2.0.0.pb-v1" {
|
if req.URL.Path != "/swagger-2.0.0.pb-v1" {
|
||||||
sErr = fmt.Errorf("Unexpected url %v", req.URL)
|
errMsg := fmt.Sprintf("Unexpected url %v", req.URL)
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
w.Write([]byte(errMsg))
|
||||||
|
t.Errorf("testing should fail as %s", errMsg)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if req.Method != "GET" {
|
if req.Method != "GET" {
|
||||||
sErr = fmt.Errorf("Unexpected method %v", req.Method)
|
errMsg := fmt.Sprintf("Unexpected method %v", req.Method)
|
||||||
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
w.Write([]byte(errMsg))
|
||||||
|
t.Errorf("testing should fail as %s", errMsg)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mime.AddExtensionType(".pb-v1", "application/com.github.googleapis.gnostic.OpenAPIv2@68f4ded+protobuf")
|
mime.AddExtensionType(".pb-v1", "application/com.github.googleapis.gnostic.OpenAPIv2@68f4ded+protobuf")
|
||||||
|
|
||||||
output, err := proto.Marshal(returnedOpenAPI())
|
output, err := proto.Marshal(returnedOpenAPI())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sErr = err
|
errMsg := fmt.Sprintf("Unexpected marshal error: %v", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
w.Write([]byte(errMsg))
|
||||||
|
t.Errorf("testing should fail as %s", errMsg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(output)
|
w.Write(output)
|
||||||
}))
|
}))
|
||||||
return server, sErr
|
|
||||||
|
return server, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func openapiSchemaFakeServer() (*httptest.Server, error) {
|
func openapiSchemaFakeServer(t *testing.T) (*httptest.Server, error) {
|
||||||
var sErr error
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.URL.Path != "/openapi/v2" {
|
if req.URL.Path != "/openapi/v2" {
|
||||||
sErr = fmt.Errorf("Unexpected url %v", req.URL)
|
errMsg := fmt.Sprintf("Unexpected url %v", req.URL)
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
w.Write([]byte(errMsg))
|
||||||
|
t.Errorf("testing should fail as %s", errMsg)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if req.Method != "GET" {
|
if req.Method != "GET" {
|
||||||
sErr = fmt.Errorf("Unexpected method %v", req.Method)
|
errMsg := fmt.Sprintf("Unexpected method %v", req.Method)
|
||||||
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
w.Write([]byte(errMsg))
|
||||||
|
t.Errorf("testing should fail as %s", errMsg)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
decipherableFormat := req.Header.Get("Accept")
|
decipherableFormat := req.Header.Get("Accept")
|
||||||
if decipherableFormat != "application/com.github.proto-openapi.spec.v2@v1.0+protobuf" {
|
if decipherableFormat != "application/com.github.proto-openapi.spec.v2@v1.0+protobuf" {
|
||||||
sErr = fmt.Errorf("Unexpected accept mime type %v", decipherableFormat)
|
errMsg := fmt.Sprintf("Unexpected accept mime type %v", decipherableFormat)
|
||||||
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
||||||
|
w.Write([]byte(errMsg))
|
||||||
|
t.Errorf("testing should fail as %s", errMsg)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mime.AddExtensionType(".pb-v1", "application/com.github.googleapis.gnostic.OpenAPIv2@68f4ded+protobuf")
|
mime.AddExtensionType(".pb-v1", "application/com.github.googleapis.gnostic.OpenAPIv2@68f4ded+protobuf")
|
||||||
|
|
||||||
output, err := proto.Marshal(returnedOpenAPI())
|
output, err := proto.Marshal(returnedOpenAPI())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sErr = err
|
errMsg := fmt.Sprintf("Unexpected marshal error: %v", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
w.Write([]byte(errMsg))
|
||||||
|
t.Errorf("testing should fail as %s", errMsg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(output)
|
w.Write(output)
|
||||||
}))
|
}))
|
||||||
return server, sErr
|
|
||||||
|
return server, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetOpenAPISchema(t *testing.T) {
|
func TestGetOpenAPISchema(t *testing.T) {
|
||||||
server, err := openapiSchemaFakeServer()
|
server, err := openapiSchemaFakeServer(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error starting fake server: %v", err)
|
t.Errorf("unexpected error starting fake server: %v", err)
|
||||||
}
|
}
|
||||||
@ -527,7 +553,7 @@ func TestGetOpenAPISchema(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetOpenAPISchemaForbiddenFallback(t *testing.T) {
|
func TestGetOpenAPISchemaForbiddenFallback(t *testing.T) {
|
||||||
server, err := openapiSchemaDeprecatedFakeServer(http.StatusForbidden)
|
server, err := openapiSchemaDeprecatedFakeServer(http.StatusForbidden, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error starting fake server: %v", err)
|
t.Errorf("unexpected error starting fake server: %v", err)
|
||||||
}
|
}
|
||||||
@ -544,7 +570,7 @@ func TestGetOpenAPISchemaForbiddenFallback(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetOpenAPISchemaNotFoundFallback(t *testing.T) {
|
func TestGetOpenAPISchemaNotFoundFallback(t *testing.T) {
|
||||||
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotFound)
|
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotFound, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error starting fake server: %v", err)
|
t.Errorf("unexpected error starting fake server: %v", err)
|
||||||
}
|
}
|
||||||
@ -561,7 +587,7 @@ func TestGetOpenAPISchemaNotFoundFallback(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetOpenAPISchemaNotAcceptableFallback(t *testing.T) {
|
func TestGetOpenAPISchemaNotAcceptableFallback(t *testing.T) {
|
||||||
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotAcceptable)
|
server, err := openapiSchemaDeprecatedFakeServer(http.StatusNotAcceptable, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error starting fake server: %v", err)
|
t.Errorf("unexpected error starting fake server: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user