...

Source file src/github.com/Azure/go-autorest/autorest/authorization_sas_test.go

Documentation: github.com/Azure/go-autorest/autorest

     1  package 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  
    17  import (
    18  	"net/http"
    19  	"net/url"
    20  	"testing"
    21  )
    22  
    23  func TestSasNewSasAuthorizerEmptyToken(t *testing.T) {
    24  	auth, err := NewSASTokenAuthorizer("")
    25  	if err == nil {
    26  		t.Fatalf("azure: SASTokenAuthorizer#NewSASTokenAuthorizer didn't return an error")
    27  	}
    28  
    29  	if auth != nil {
    30  		t.Fatalf("azure: SASTokenAuthorizer#NewSASTokenAuthorizer returned an authorizer")
    31  	}
    32  }
    33  
    34  func TestSasNewSasAuthorizerEmptyTokenWithWhitespace(t *testing.T) {
    35  	auth, err := NewSASTokenAuthorizer("  ")
    36  	if err == nil {
    37  		t.Fatalf("azure: SASTokenAuthorizer#NewSASTokenAuthorizer didn't return an error")
    38  	}
    39  
    40  	if auth != nil {
    41  		t.Fatalf("azure: SASTokenAuthorizer#NewSASTokenAuthorizer returned an authorizer")
    42  	}
    43  }
    44  
    45  func TestSasNewSasAuthorizerValidToken(t *testing.T) {
    46  	auth, err := NewSASTokenAuthorizer("abc123")
    47  	if err != nil {
    48  		t.Fatalf("azure: SASTokenAuthorizer#NewSASTokenAuthorizer returned an error")
    49  	}
    50  
    51  	if auth == nil {
    52  		t.Fatalf("azure: SASTokenAuthorizer#NewSASTokenAuthorizer didn't return an authorizer")
    53  	}
    54  }
    55  
    56  func TestSasAuthorizerRequest(t *testing.T) {
    57  	testData := []struct {
    58  		name     string
    59  		token    string
    60  		input    string
    61  		expected string
    62  	}{
    63  		{
    64  			name:     "empty querystring without a prefix",
    65  			token:    "abc123",
    66  			input:    "https://example.com/foo/bar",
    67  			expected: "https://example.com/foo/bar?abc123",
    68  		},
    69  		{
    70  			name:     "empty querystring with a prefix",
    71  			token:    "?abc123",
    72  			input:    "https://example.com/foo/bar",
    73  			expected: "https://example.com/foo/bar?abc123",
    74  		},
    75  		{
    76  			name:     "existing querystring without a prefix",
    77  			token:    "abc123",
    78  			input:    "https://example.com/foo/bar?hello=world",
    79  			expected: "https://example.com/foo/bar?hello=world&abc123",
    80  		},
    81  		{
    82  			name:     "existing querystring with a prefix",
    83  			token:    "?abc123",
    84  			input:    "https://example.com/foo/bar?hello=world",
    85  			expected: "https://example.com/foo/bar?hello=world&abc123",
    86  		},
    87  	}
    88  
    89  	for _, v := range testData {
    90  		t.Logf("[DEBUG] Testing Case %q..", v.name)
    91  		auth, err := NewSASTokenAuthorizer(v.token)
    92  		if err != nil {
    93  			t.Fatalf("azure: SASTokenAuthorizer#WithAuthorization expected %q but got an error", v.expected)
    94  		}
    95  		url, _ := url.ParseRequestURI(v.input)
    96  		httpReq := &http.Request{
    97  			URL: url,
    98  		}
    99  
   100  		req, err := Prepare(httpReq, auth.WithAuthorization())
   101  		if err != nil {
   102  			t.Fatalf("azure: SASTokenAuthorizer#WithAuthorization returned an error (%v)", err)
   103  		}
   104  
   105  		if req.URL.String() != v.expected {
   106  			t.Fatalf("azure: SASTokenAuthorizer#WithAuthorization failed to set QueryString header - got %q but expected %q", req.URL.String(), v.expected)
   107  		}
   108  
   109  		if req.Header.Get(http.CanonicalHeaderKey("Authorization")) != "" {
   110  			t.Fatal("azure: SASTokenAuthorizer#WithAuthorization set an Authorization header when it shouldn't!")
   111  		}
   112  	}
   113  }
   114  

View as plain text