Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tiloKo committed Mar 5, 2024
1 parent 8c4888a commit 8cd3334
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
63 changes: 63 additions & 0 deletions pkg/abap/build/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,43 @@
package build

import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"testing"
"time"

"golang.org/x/exp/slices" //in 1.21 will be a standard package "slices"

piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/stretchr/testify/assert"
)

type HeaderVerifyingMockClient struct {
Header map[string][]string
}

func (mc *HeaderVerifyingMockClient) SetOptions(opts piperhttp.ClientOptions) {}
func (mc *HeaderVerifyingMockClient) SendRequest(Method, Url string, bdy io.Reader, hdr http.Header, cookies []*http.Cookie) (*http.Response, error) {
for requiredHeaderKey, requiredHeaderValues := range mc.Header {
suppliedHeaderValues, existingHeader := hdr[requiredHeaderKey]
if existingHeader {
for _, element := range requiredHeaderValues {
existingValue := slices.Contains(suppliedHeaderValues, element)
if !existingValue {
return nil, fmt.Errorf("header %s does not contain expected value %s", requiredHeaderKey, element)
}
}
} else {
return nil, fmt.Errorf("Expected header %s not part of the http request", requiredHeaderKey)
}
}

return &http.Response{Body: io.NopCloser(bytes.NewReader([]byte("")))}, nil
}

func TestCreateUrl(t *testing.T) {
//arrange global
conn := new(Connector)
Expand Down Expand Up @@ -59,3 +89,36 @@ func TestCreateUrl(t *testing.T) {
assert.Equal(t, "/BUILD/CORE_SRV/builds('123456789')?format=json&sap-client=001&top=2", url)
})
}

func TestInitAAKaaSHeader(t *testing.T) {
conn := new(Connector)

client := HeaderVerifyingMockClient{}
client.Header = make(map[string][]string)
client.Header["Accept"] = []string{"application/json"}
client.Header["Content-Type"] = []string{"application/json"}
client.Header["User-Agent"] = []string{"Piper-abapAddonAssemblyKit/1.0"}
t.Run("InitAAKaaS success no hash", func(t *testing.T) {
conn.InitAAKaaS("endpoint", "user", "pw", &client, "")
_, err := conn.Get("something")
assert.NoError(t, err)
})
t.Run("InitAAKaaS success with hash", func(t *testing.T) {
client.Header["build-config-token"] = []string{"hash"}
conn.InitAAKaaS("endpoint", "user", "pw", &client, "hash")
_, err := conn.Get("something")
assert.NoError(t, err)
})
t.Run("InitAAKaaS sanity check Header", func(t *testing.T) {
client.Header["FAIL"] = []string{"verify HeaderVerifyingMockClient works"}
conn.InitAAKaaS("endpoint", "user", "pw", &client, "hash")
_, err := conn.Get("something")
assert.Error(t, err)
})
t.Run("InitAAKaaS sanity check wrong Value in existing Header", func(t *testing.T) {
client.Header["Accept"] = []string{"verify HeaderVerifyingMockClient works"}
conn.InitAAKaaS("endpoint", "user", "pw", &client, "hash")
_, err := conn.Get("something")
assert.Error(t, err)
})
}
1 change: 0 additions & 1 deletion pkg/abap/build/mockClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func (mc *MockClient) SetOptions(opts piperhttp.ClientOptions) {}
func (mc *MockClient) SendRequest(Method, Url string, bdy io.Reader, hdr http.Header, cookies []*http.Cookie) (*http.Response, error) {
response, ok := mc.getResponse(Method, Url)
if !ok {
//return nil, errors.New("No Mock data for given Method+Url")
return nil, fmt.Errorf("No Mock data for %s", Method+Url)
}
return &response, nil
Expand Down

0 comments on commit 8cd3334

Please sign in to comment.