From eb1bc67db1a7f9cca50761711b824a3e2240c43f Mon Sep 17 00:00:00 2001 From: Aditya Manthramurthy Date: Fri, 4 Nov 2016 16:52:22 -0400 Subject: [PATCH] Refactor: simplify signV4TrimAll() (#3179) --- cmd/signature-v4-utils.go | 12 ++---------- cmd/signature-v4-utils_test.go | 7 ++++--- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/cmd/signature-v4-utils.go b/cmd/signature-v4-utils.go index 675c9b9b6..4fd4371f9 100644 --- a/cmd/signature-v4-utils.go +++ b/cmd/signature-v4-utils.go @@ -161,15 +161,7 @@ func extractSignedHeaders(signedHeaders []string, reqHeaders http.Header) (http. // Trim leading and trailing spaces and replace sequential spaces with one space, following Trimall() // in http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html func signV4TrimAll(input string) string { - // Remove all whitespaces first - cleanWhiteSpaces := func(r rune) rune { - switch r { - case '\t', '\n', '\u000b', '\r', '\f': - return ' ' - } - return r - } - input = strings.Map(cleanWhiteSpaces, input) - // Compress adjacent spaces to one space and return + // Compress adjacent spaces (a space is determined by + // unicode.IsSpace() internally here) to one space and return return strings.Join(strings.Fields(input), " ") } diff --git a/cmd/signature-v4-utils_test.go b/cmd/signature-v4-utils_test.go index 71995bd94..3dff1db1a 100644 --- a/cmd/signature-v4-utils_test.go +++ b/cmd/signature-v4-utils_test.go @@ -212,8 +212,8 @@ func TestFindHost(t *testing.T) { } } -// TestSigV4TrimAll - tests the logic of TrimAll() function -func TestSigV4TrimAll(t *testing.T) { +// TestSignV4TrimAll - tests the logic of TrimAll() function +func TestSignV4TrimAll(t *testing.T) { testCases := []struct { // Input. inputStr string @@ -229,13 +229,14 @@ func TestSigV4TrimAll(t *testing.T) { {" a b c ", "a b c"}, {"a \t b c ", "a b c"}, {"\"a \t b c ", "\"a b c"}, + {" \t\n\u000b\r\fa \t\n\u000b\r\f b \t\n\u000b\r\f c \t\n\u000b\r\f", "a b c"}, } // Tests generated values from url encoded name. for i, testCase := range testCases { result := signV4TrimAll(testCase.inputStr) if testCase.result != result { - t.Errorf("Test %d: Expected sigV4TrimAll result to be \"%s\", but found it to be \"%s\" instead", i+1, testCase.result, result) + t.Errorf("Test %d: Expected signV4TrimAll result to be \"%s\", but found it to be \"%s\" instead", i+1, testCase.result, result) } } }