1package autorest 2 3// Copyright 2017 Microsoft Corporation 4// 5// Licensed under the Apache License, Version 2.0 (the "License"); 6// you may not use this file except in compliance with the License. 7// You may obtain a copy of the License at 8// 9// http://www.apache.org/licenses/LICENSE-2.0 10// 11// Unless required by applicable law or agreed to in writing, software 12// distributed under the License is distributed on an "AS IS" BASIS, 13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14// See the License for the specific language governing permissions and 15// limitations under the License. 16 17import ( 18 "net/http" 19 "testing" 20) 21 22func TestNewSharedKeyAuthorizer(t *testing.T) { 23 auth, err := NewSharedKeyAuthorizer("golangrocksonazure", "YmFy", SharedKey) 24 if err != nil { 25 t.Fatalf("create shared key authorizer: %v", err) 26 } 27 req, err := http.NewRequest(http.MethodGet, "https://golangrocksonazure.blob.core.windows.net/some/blob.dat", nil) 28 if err != nil { 29 t.Fatalf("create HTTP request: %v", err) 30 } 31 req.Header.Add(headerAcceptCharset, "UTF-8") 32 req.Header.Add(headerContentType, "application/json") 33 req.Header.Add(headerXMSDate, "Wed, 23 Sep 2015 16:40:05 GMT") 34 req.Header.Add(headerContentLength, "0") 35 req.Header.Add(headerXMSVersion, "2015-02-21") 36 req.Header.Add(headerAccept, "application/json;odata=nometadata") 37 req, err = Prepare(req, auth.WithAuthorization()) 38 if err != nil { 39 t.Fatalf("prepare HTTP request: %v", err) 40 } 41 const expected = "SharedKey golangrocksonazure:nYRqgbumDOTPs+Vv1FLH+hm0KPjwwt+Fmj/i16W+lO0=" 42 if auth := req.Header.Get(headerAuthorization); auth != expected { 43 t.Fatalf("expected: %s, go %s", expected, auth) 44 } 45} 46 47func TestNewSharedKeyAuthorizerWithRoot(t *testing.T) { 48 auth, err := NewSharedKeyAuthorizer("golangrocksonazure", "YmFy", SharedKey) 49 if err != nil { 50 t.Fatalf("create shared key authorizer: %v", err) 51 } 52 req, err := http.NewRequest(http.MethodGet, "https://golangrocksonazure.blob.core.windows.net/?comp=properties&restype=service", nil) 53 if err != nil { 54 t.Fatalf("create HTTP request: %v", err) 55 } 56 req.Header.Add(headerAcceptCharset, "UTF-8") 57 req.Header.Add(headerContentType, "application/json") 58 req.Header.Add(headerXMSDate, "Tue, 10 Mar 2020 10:04:41 GMT") 59 req.Header.Add(headerContentLength, "0") 60 req.Header.Add(headerXMSVersion, "2018-11-09") 61 req.Header.Add(headerAccept, "application/json;odata=nometadata") 62 req, err = Prepare(req, auth.WithAuthorization()) 63 if err != nil { 64 t.Fatalf("prepare HTTP request: %v", err) 65 } 66 const expected = "SharedKey golangrocksonazure:BfdIC0K5OwkRbZjewqRXgjQJ2PBMZDoaBCCL3qhrEIs=" 67 if auth := req.Header.Get(headerAuthorization); auth != expected { 68 t.Fatalf("expected: %s, go %s", expected, auth) 69 } 70} 71 72func TestNewSharedKeyAuthorizerWithoutRoot(t *testing.T) { 73 auth, err := NewSharedKeyAuthorizer("golangrocksonazure", "YmFy", SharedKey) 74 if err != nil { 75 t.Fatalf("create shared key authorizer: %v", err) 76 } 77 req, err := http.NewRequest(http.MethodGet, "https://golangrocksonazure.blob.core.windows.net?comp=properties&restype=service", nil) 78 if err != nil { 79 t.Fatalf("create HTTP request: %v", err) 80 } 81 req.Header.Add(headerAcceptCharset, "UTF-8") 82 req.Header.Add(headerContentType, "application/json") 83 req.Header.Add(headerXMSDate, "Tue, 10 Mar 2020 10:04:41 GMT") 84 req.Header.Add(headerContentLength, "0") 85 req.Header.Add(headerXMSVersion, "2018-11-09") 86 req.Header.Add(headerAccept, "application/json;odata=nometadata") 87 req, err = Prepare(req, auth.WithAuthorization()) 88 if err != nil { 89 t.Fatalf("prepare HTTP request: %v", err) 90 } 91 const expected = "SharedKey golangrocksonazure:BfdIC0K5OwkRbZjewqRXgjQJ2PBMZDoaBCCL3qhrEIs=" 92 if auth := req.Header.Get(headerAuthorization); auth != expected { 93 t.Fatalf("expected: %s, go %s", expected, auth) 94 } 95} 96 97func TestNewSharedKeyForTableAuthorizer(t *testing.T) { 98 auth, err := NewSharedKeyAuthorizer("golangrocksonazure", "YmFy", SharedKeyForTable) 99 if err != nil { 100 t.Fatalf("create shared key authorizer: %v", err) 101 } 102 req, err := http.NewRequest(http.MethodGet, "https://golangrocksonazure.table.core.windows.net/tquery()", nil) 103 if err != nil { 104 t.Fatalf("create HTTP request: %v", err) 105 } 106 req.Header.Add(headerAcceptCharset, "UTF-8") 107 req.Header.Add(headerContentType, "application/json") 108 req.Header.Add(headerXMSDate, "Wed, 23 Sep 2015 16:40:05 GMT") 109 req.Header.Add(headerContentLength, "0") 110 req.Header.Add(headerXMSVersion, "2015-02-21") 111 req.Header.Add(headerAccept, "application/json;odata=nometadata") 112 req, err = Prepare(req, auth.WithAuthorization()) 113 if err != nil { 114 t.Fatalf("prepare HTTP request: %v", err) 115 } 116 const expected = "SharedKey golangrocksonazure:73oeIBA2dulLhOBdAlM3U0+DKIWS0UW6InBWCHpOY50=" 117 if auth := req.Header.Get(headerAuthorization); auth != expected { 118 t.Fatalf("expected: %s, go %s", expected, auth) 119 } 120} 121 122func TestNewSharedKeyLiteAuthorizer(t *testing.T) { 123 auth, err := NewSharedKeyAuthorizer("golangrocksonazure", "YmFy", SharedKeyLite) 124 if err != nil { 125 t.Fatalf("create shared key authorizer: %v", err) 126 } 127 128 req, err := http.NewRequest(http.MethodGet, "https://golangrocksonazure.file.core.windows.net/some/file.dat", nil) 129 if err != nil { 130 t.Fatalf("create HTTP request: %v", err) 131 } 132 req.Header.Add(headerAcceptCharset, "UTF-8") 133 req.Header.Add(headerContentType, "application/json") 134 req.Header.Add(headerXMSDate, "Wed, 23 Sep 2015 16:40:05 GMT") 135 req.Header.Add(headerContentLength, "0") 136 req.Header.Add(headerXMSVersion, "2015-02-21") 137 req.Header.Add(headerAccept, "application/json;odata=nometadata") 138 req, err = Prepare(req, auth.WithAuthorization()) 139 if err != nil { 140 t.Fatalf("prepare HTTP request: %v", err) 141 } 142 const expected = "SharedKeyLite golangrocksonazure:0VODf/mHRDa7lMShzTKbow7lxptaIZ0qIAcVD0lG9PE=" 143 if auth := req.Header.Get(headerAuthorization); auth != expected { 144 t.Fatalf("expected: %s, go %s", expected, auth) 145 } 146} 147 148func TestNewSharedKeyLiteForTableAuthorizer(t *testing.T) { 149 auth, err := NewSharedKeyAuthorizer("golangrocksonazure", "YmFy", SharedKeyLiteForTable) 150 if err != nil { 151 t.Fatalf("create shared key authorizer: %v", err) 152 } 153 154 req, err := http.NewRequest(http.MethodGet, "https://golangrocksonazure.table.core.windows.net/tquery()", nil) 155 if err != nil { 156 t.Fatalf("create HTTP request: %v", err) 157 } 158 req.Header.Add(headerAcceptCharset, "UTF-8") 159 req.Header.Add(headerContentType, "application/json") 160 req.Header.Add(headerXMSDate, "Wed, 23 Sep 2015 16:40:05 GMT") 161 req.Header.Add(headerContentLength, "0") 162 req.Header.Add(headerXMSVersion, "2015-02-21") 163 req.Header.Add(headerAccept, "application/json;odata=nometadata") 164 req, err = Prepare(req, auth.WithAuthorization()) 165 if err != nil { 166 t.Fatalf("prepare HTTP request: %v", err) 167 } 168 const expected = "SharedKeyLite golangrocksonazure:NusXSFXAvHqr6EQNXnZZ50CvU1sX0iP/FFDHehnixLc=" 169 if auth := req.Header.Get(headerAuthorization); auth != expected { 170 t.Fatalf("expected: %s, go %s", expected, auth) 171 } 172} 173