Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: vasyaxparfenov <[email protected]>
  • Loading branch information
vasyaxparfenov committed Nov 4, 2023
1 parent 443640b commit cce6435
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions provider/awsv4_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package provider

import (
"crypto/sha256"
"encoding/hex"
"io"
"net/http"
"strings"
"testing"
)

type MockRoundTripper struct {
}

func (roundTripper *MockRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
return new(http.Response), nil
}

func TestRoundTripWithEmptyBody(t *testing.T) {
sut := Wrap(new(MockRoundTripper))

request := new(http.Request)
request.Header = make(http.Header)

_, err := sut.RoundTrip(request)

if err != nil {
t.Fatal(err)
}

if header, contains := request.Header["X-Amz-Content-Sha256"]; !contains || len(header) != 1 || header[0] != emptyStringSHA256 {
t.Fatal("Request with empty body doesn't contain X-Amz-Content-Sha256 header with empty string hash value.")
}
}

func TestRoundTripWithBody(t *testing.T) {
sut := Wrap(new(MockRoundTripper))
request := new(http.Request)
request.Header = make(http.Header)

body := "body"
request.Body = io.NopCloser(strings.NewReader(body))

hasher := sha256.New()
hasher.Write([]byte(body))
hashBytes := hasher.Sum(nil)
expectedHash := hex.EncodeToString(hashBytes)

_, err := sut.RoundTrip(request)
if err != nil {
t.Fatal(err)
}

if header, contains := request.Header["X-Amz-Content-Sha256"]; !contains || len(header) != 1 || header[0] != expectedHash {
t.Fatal("Request with body doesn't contain X-Amz-Content-Sha256 header with correct hash value.")
}
}

0 comments on commit cce6435

Please sign in to comment.