Ignore special HTTP fields in response validation tests (#19530)

This commit is contained in:
Anton Averchenkov
2023-03-14 13:21:20 -04:00
committed by GitHub
parent cc3b0130b6
commit ce420de231
3 changed files with 73 additions and 13 deletions

View File

@@ -44,18 +44,6 @@ var pathFetchReadSchema = map[int][]framework.Response{
Description: `Issuing CA Chain`, Description: `Issuing CA Chain`,
Required: false, 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,
},
}, },
}}, }},
} }

View File

@@ -66,6 +66,24 @@ func validateResponseDataImpl(schema *framework.Response, data map[string]interf
return fmt.Errorf("failed to unmashal data: %w", err) 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 // Validate
fd := framework.FieldData{ fd := framework.FieldData{
Raw: dataWithStringValues, Raw: dataWithStringValues,
@@ -79,7 +97,7 @@ func validateResponseDataImpl(schema *framework.Response, data map[string]interf
return fd.Validate() 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 { func FindResponseSchema(t *testing.T, paths []*framework.Path, pathIdx int, operation logical.Operation) *framework.Response {
t.Helper() t.Helper()

View File

@@ -249,6 +249,60 @@ func TestValidateResponse(t *testing.T) {
strict: false, strict: false,
errorExpected: 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 { for name, tc := range cases {