From ce420de231badfa49bedf7e59f3f21e79048747c Mon Sep 17 00:00:00 2001 From: Anton Averchenkov <84287187+averche@users.noreply.github.com> Date: Tue, 14 Mar 2023 13:21:20 -0400 Subject: [PATCH] Ignore special HTTP fields in response validation tests (#19530) --- builtin/logical/pki/path_fetch.go | 12 ----- .../testhelpers/schema/response_validation.go | 20 ++++++- .../schema/response_validation_test.go | 54 +++++++++++++++++++ 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/builtin/logical/pki/path_fetch.go b/builtin/logical/pki/path_fetch.go index 9a6679e86d..52c8260c19 100644 --- a/builtin/logical/pki/path_fetch.go +++ b/builtin/logical/pki/path_fetch.go @@ -44,18 +44,6 @@ var pathFetchReadSchema = map[int][]framework.Response{ Description: `Issuing CA Chain`, Required: false, }, - "http_content_type": { - Type: framework.TypeString, - Required: false, - }, - "http_raw_body": { - Type: framework.TypeString, - Required: false, - }, - "http_status_code": { - Type: framework.TypeString, - Required: false, - }, }, }}, } diff --git a/sdk/helper/testhelpers/schema/response_validation.go b/sdk/helper/testhelpers/schema/response_validation.go index 1238c595e5..7ebe9da9d1 100644 --- a/sdk/helper/testhelpers/schema/response_validation.go +++ b/sdk/helper/testhelpers/schema/response_validation.go @@ -66,6 +66,24 @@ func validateResponseDataImpl(schema *framework.Response, data map[string]interf return fmt.Errorf("failed to unmashal data: %w", err) } + // these are special fields that will not show up in the final response and + // should be ignored + for _, field := range []string{ + logical.HTTPContentType, + logical.HTTPRawBody, + logical.HTTPStatusCode, + logical.HTTPRawBodyAlreadyJSONDecoded, + logical.HTTPCacheControlHeader, + logical.HTTPPragmaHeader, + logical.HTTPWWWAuthenticateHeader, + } { + delete(dataWithStringValues, field) + + if _, ok := schema.Fields[field]; ok { + return fmt.Errorf("encountered a reserved field in response schema: %s", field) + } + } + // Validate fd := framework.FieldData{ Raw: dataWithStringValues, @@ -79,7 +97,7 @@ func validateResponseDataImpl(schema *framework.Response, data map[string]interf return fd.Validate() } -// FindResponseSchema is a test helper to extract the response schema from a given framework path / operation +// FindResponseSchema is a test helper to extract response schema from the given framework path / operation func FindResponseSchema(t *testing.T, paths []*framework.Path, pathIdx int, operation logical.Operation) *framework.Response { t.Helper() diff --git a/sdk/helper/testhelpers/schema/response_validation_test.go b/sdk/helper/testhelpers/schema/response_validation_test.go index 976389c444..d2533b65de 100644 --- a/sdk/helper/testhelpers/schema/response_validation_test.go +++ b/sdk/helper/testhelpers/schema/response_validation_test.go @@ -249,6 +249,60 @@ func TestValidateResponse(t *testing.T) { strict: false, errorExpected: false, }, + + "empty schema, response has http_raw_body, strict": { + schema: &framework.Response{ + Fields: map[string]*framework.FieldSchema{}, + }, + response: map[string]interface{}{ + "http_raw_body": "foo", + }, + strict: true, + errorExpected: false, + }, + + "empty schema, response has http_raw_body, not strict": { + schema: &framework.Response{ + Fields: map[string]*framework.FieldSchema{}, + }, + response: map[string]interface{}{ + "http_raw_body": "foo", + }, + strict: false, + errorExpected: false, + }, + + "schema has http_raw_body, strict": { + schema: &framework.Response{ + Fields: map[string]*framework.FieldSchema{ + "http_raw_body": { + Type: framework.TypeString, + Required: false, + }, + }, + }, + response: map[string]interface{}{ + "http_raw_body": "foo", + }, + strict: true, + errorExpected: true, + }, + + "schema has http_raw_body, not strict": { + schema: &framework.Response{ + Fields: map[string]*framework.FieldSchema{ + "http_raw_body": { + Type: framework.TypeString, + Required: false, + }, + }, + }, + response: map[string]interface{}{ + "http_raw_body": "foo", + }, + strict: false, + errorExpected: true, + }, } for name, tc := range cases {