From 651594636917989d2c6f042f56ff7eed2147ea0f Mon Sep 17 00:00:00 2001 From: Jacob Kozol Date: Tue, 29 Oct 2019 16:03:09 +0100 Subject: [PATCH] weldr/tests: Add ability to ignore fields in the response Certain fields (timestamps, uuids, etc.) are difficult to test for. The dropFields function allows specific fields to be removed from an unmarshalled json response body. --- internal/weldr/api_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go index 783a736b8..4a3720102 100644 --- a/internal/weldr/api_test.go +++ b/internal/weldr/api_test.go @@ -78,6 +78,36 @@ func sendHTTP(api *weldr.API, external bool, method, path, body string) *http.Re } } +// this function serves to drop fields that shouldn't be tested from the unmarshalled json objects +func dropFields(obj interface{}, fields ...string) { + switch v := obj.(type) { + // if the interface type is a map attempt to delete the fields + case map[string]interface{}: + for i, field := range fields { + if _, ok := v[field]; ok { + delete(v, field) + // if the field is found remove it from the fields slice + if len(fields) < i-1 { + fields = append(fields[:i], fields[i+1:]...) + } else { + fields = fields[:i] + } + } + } + // call dropFields on the remaining elements since they may contain a map containing the field + for _, val := range v { + dropFields(val, fields...) + } + // if the type is a list of interfaces call dropFields on each interface + case []interface{}: + for _, element := range v { + dropFields(element, fields...) + } + default: + return + } +} + func testRoute(t *testing.T, api *weldr.API, external bool, method, path, body string, expectedStatus int, expectedJSON string) { resp := sendHTTP(api, external, method, path, body) if resp == nil {