github.com/aavshr/aws-sdk-go@v1.41.3/aws/signer/v4/functional_1_5_test.go (about)

     1  //go:build go1.5
     2  // +build go1.5
     3  
     4  package v4_test
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/aavshr/aws-sdk-go/aws/signer/v4"
    13  	"github.com/aavshr/aws-sdk-go/awstesting/unit"
    14  )
    15  
    16  func TestStandaloneSign(t *testing.T) {
    17  	creds := unit.Session.Config.Credentials
    18  	signer := v4.NewSigner(creds)
    19  
    20  	for _, c := range standaloneSignCases {
    21  		host := fmt.Sprintf("https://%s.%s.%s.amazonaws.com",
    22  			c.SubDomain, c.Region, c.Service)
    23  
    24  		req, err := http.NewRequest("GET", host, nil)
    25  		if err != nil {
    26  			t.Errorf("expected no error, but received %v", err)
    27  		}
    28  
    29  		// URL.EscapedPath() will be used by the signer to get the
    30  		// escaped form of the request's URI path.
    31  		req.URL.Path = c.OrigURI
    32  		req.URL.RawQuery = c.OrigQuery
    33  
    34  		_, err = signer.Sign(req, nil, c.Service, c.Region, time.Unix(0, 0))
    35  		if err != nil {
    36  			t.Errorf("expected no error, but received %v", err)
    37  		}
    38  
    39  		actual := req.Header.Get("Authorization")
    40  		if e, a := c.ExpSig, actual; e != a {
    41  			t.Errorf("expected %v, but received %v", e, a)
    42  		}
    43  		if e, a := c.OrigURI, req.URL.Path; e != a {
    44  			t.Errorf("expected %v, but received %v", e, a)
    45  		}
    46  		if e, a := c.EscapedURI, req.URL.EscapedPath(); e != a {
    47  			t.Errorf("expected %v, but received %v", e, a)
    48  		}
    49  	}
    50  }
    51  
    52  func TestStandaloneSign_RawPath(t *testing.T) {
    53  	creds := unit.Session.Config.Credentials
    54  	signer := v4.NewSigner(creds)
    55  
    56  	for _, c := range standaloneSignCases {
    57  		host := fmt.Sprintf("https://%s.%s.%s.amazonaws.com",
    58  			c.SubDomain, c.Region, c.Service)
    59  
    60  		req, err := http.NewRequest("GET", host, nil)
    61  		if err != nil {
    62  			t.Errorf("expected no error, but received %v", err)
    63  		}
    64  
    65  		// URL.EscapedPath() will be used by the signer to get the
    66  		// escaped form of the request's URI path.
    67  		req.URL.Path = c.OrigURI
    68  		req.URL.RawPath = c.EscapedURI
    69  		req.URL.RawQuery = c.OrigQuery
    70  
    71  		_, err = signer.Sign(req, nil, c.Service, c.Region, time.Unix(0, 0))
    72  		if err != nil {
    73  			t.Errorf("expected no error, but received %v", err)
    74  		}
    75  
    76  		actual := req.Header.Get("Authorization")
    77  		if e, a := c.ExpSig, actual; e != a {
    78  			t.Errorf("expected %v, but received %v", e, a)
    79  		}
    80  		if e, a := c.OrigURI, req.URL.Path; e != a {
    81  			t.Errorf("expected %v, but received %v", e, a)
    82  		}
    83  		if e, a := c.EscapedURI, req.URL.EscapedPath(); e != a {
    84  			t.Errorf("expected %v, but received %v", e, a)
    85  		}
    86  	}
    87  }