From 79888bfff76e124492704157d7122e79e4205d1f Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 16 Sep 2016 15:17:49 -0700 Subject: [PATCH] tests: Add auth-handler. (#2721) Fixes #2658 --- cmd/auth-handler_test.go | 95 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/cmd/auth-handler_test.go b/cmd/auth-handler_test.go index ada9bf3c4..884b47fa3 100644 --- a/cmd/auth-handler_test.go +++ b/cmd/auth-handler_test.go @@ -20,9 +20,104 @@ import ( "bytes" "io" "net/http" + "net/url" "testing" ) +// Test get request auth type. +func TestGetRequestAuthType(t *testing.T) { + type testCase struct { + req *http.Request + authT authType + } + testCases := []testCase{ + // Test case - 1 + // Check for generic signature v4 header. + { + req: &http.Request{ + URL: &url.URL{ + Host: "localhost:9000", + Scheme: "http", + Path: "/", + }, + Header: http.Header{ + "Authorization": []string{"AWS4-HMAC-SHA256 "}, + "X-Amz-Content-Sha256": []string{streamingContentSHA256}, + }, + Method: "PUT", + }, + authT: authTypeStreamingSigned, + }, + // Test case - 2 + // Check for JWT header. + { + req: &http.Request{ + URL: &url.URL{ + Host: "localhost:9000", + Scheme: "http", + Path: "/", + }, + Header: http.Header{ + "Authorization": []string{"Bearer 12313123"}, + }, + }, + authT: authTypeJWT, + }, + // Test case - 3 + // Empty authorization header. + { + req: &http.Request{ + URL: &url.URL{ + Host: "localhost:9000", + Scheme: "http", + Path: "/", + }, + Header: http.Header{ + "Authorization": []string{""}, + }, + }, + authT: authTypeUnknown, + }, + // Test case - 4 + // Check for presigned. + { + req: &http.Request{ + URL: &url.URL{ + Host: "localhost:9000", + Scheme: "http", + Path: "/", + RawQuery: "X-Amz-Credential=EXAMPLEINVALIDEXAMPL%2Fs3%2F20160314%2Fus-east-1", + }, + }, + authT: authTypePresigned, + }, + // Test case - 5 + // Check for post policy. + { + req: &http.Request{ + URL: &url.URL{ + Host: "localhost:9000", + Scheme: "http", + Path: "/", + }, + Header: http.Header{ + "Content-Type": []string{"multipart/form-data"}, + }, + Method: "POST", + }, + authT: authTypePostPolicy, + }, + } + + // .. Tests all request auth type. + for i, testc := range testCases { + authT := getRequestAuthType(testc.req) + if authT != testc.authT { + t.Errorf("Test %d: Expected %d, got %d", i+1, testc.authT, authT) + } + } +} + // Test all s3 supported auth types. func TestS3SupportedAuthType(t *testing.T) { type testCase struct {