Skip to content

Commit cba8e61

Browse files
additional tests
1 parent d98bba3 commit cba8e61

9 files changed

+717
-31
lines changed

credential_authorization_provider_adapter.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ import (
66

77
"github.com/michaeldcanady/servicenow-sdk-go/core"
88
"github.com/michaeldcanady/servicenow-sdk-go/internal"
9+
newInt "github.com/michaeldcanady/servicenow-sdk-go/internal/new"
910
abstractions "github.com/microsoft/kiota-abstractions-go"
1011
"github.com/microsoft/kiota-abstractions-go/authentication"
1112
)
1213

13-
const (
14-
authorizationHeader = "Authorization"
15-
)
16-
1714
var _ authentication.AuthenticationProvider = (*credentialAuthenticationProviderAdapter)(nil)
1815

1916
// credentialAuthenticationProviderAdapter adapter for core.Credential to kiota AuthenticationProvider
@@ -33,20 +30,24 @@ func newCredentialAuthenticationProviderAdapter(credential core.Credential) (*cr
3330
}
3431

3532
// AuthenticateRequest authenticates the provided RequestInformation.
36-
func (provider *credentialAuthenticationProviderAdapter) AuthenticateRequest(ctx context.Context, request *abstractions.RequestInformation, _ map[string]interface{}) error {
37-
if request == nil {
33+
func (provider *credentialAuthenticationProviderAdapter) AuthenticateRequest(_ context.Context, request *abstractions.RequestInformation, _ map[string]interface{}) error {
34+
if newInt.IsNil(provider) {
35+
return nil
36+
}
37+
38+
if newInt.IsNil(request) {
3839
return errors.New("request is nil")
3940
}
4041
if request.Headers == nil {
4142
request.Headers = abstractions.NewRequestHeaders()
4243
}
43-
if !request.Headers.ContainsKey(authorizationHeader) {
44+
if !request.Headers.ContainsKey(newInt.RequestHeaderAuthorization.String()) {
4445
authString, err := provider.cred.GetAuthentication()
4546
if err != nil {
4647
return err
4748
}
4849
if authString != "" {
49-
request.Headers.Add(authorizationHeader, authString)
50+
request.Headers.Add(newInt.RequestHeaderAuthorization.String(), authString)
5051
}
5152
}
5253

credential_authorization_provider_adapter_test.go

+95-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/michaeldcanady/servicenow-sdk-go/internal/mocking"
8+
abstractions "github.com/microsoft/kiota-abstractions-go"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -44,7 +45,100 @@ func TestCredentialAuthenticationProviderAdapter_AuthenticateRequest(t *testing.
4445
tests := []struct {
4546
name string
4647
test func(*testing.T)
47-
}{}
48+
}{
49+
{
50+
name: "successful",
51+
test: func(t *testing.T) {
52+
credential := mocking.NewMockCredential()
53+
credential.On("GetAuthentication").Return("authentication", nil)
54+
55+
ctx := mocking.NewMockContext()
56+
requestInformation := abstractions.NewRequestInformation()
57+
additionalInfo := map[string]interface{}{}
58+
59+
requestAdapter := &credentialAuthenticationProviderAdapter{
60+
cred: credential,
61+
}
62+
63+
err := requestAdapter.AuthenticateRequest(ctx, requestInformation, additionalInfo)
64+
65+
assert.Nil(t, err)
66+
assert.Equal(t, []string{"authentication"}, requestInformation.Headers.Get("Authorization"))
67+
},
68+
},
69+
{
70+
name: "nil provider",
71+
test: func(t *testing.T) {
72+
credential := mocking.NewMockCredential()
73+
credential.On("GetAuthentication").Return("authentication", nil)
74+
75+
ctx := mocking.NewMockContext()
76+
requestInformation := abstractions.NewRequestInformation()
77+
additionalInfo := map[string]interface{}{}
78+
79+
requestAdapter := (*credentialAuthenticationProviderAdapter)(nil)
80+
81+
err := requestAdapter.AuthenticateRequest(ctx, requestInformation, additionalInfo)
82+
83+
assert.Nil(t, err)
84+
},
85+
},
86+
{
87+
name: "nil request",
88+
test: func(t *testing.T) {
89+
credential := mocking.NewMockCredential()
90+
ctx := mocking.NewMockContext()
91+
additionalInfo := map[string]interface{}{}
92+
93+
requestAdapter := &credentialAuthenticationProviderAdapter{
94+
cred: credential,
95+
}
96+
97+
err := requestAdapter.AuthenticateRequest(ctx, nil, additionalInfo)
98+
99+
assert.Equal(t, errors.New("request is nil"), err)
100+
},
101+
},
102+
{
103+
name: "nil headers",
104+
test: func(t *testing.T) {
105+
credential := mocking.NewMockCredential()
106+
credential.On("GetAuthentication").Return("authentication", nil)
107+
108+
ctx := mocking.NewMockContext()
109+
requestInformation := &abstractions.RequestInformation{}
110+
additionalInfo := map[string]interface{}{}
111+
112+
requestAdapter := &credentialAuthenticationProviderAdapter{
113+
cred: credential,
114+
}
115+
116+
err := requestAdapter.AuthenticateRequest(ctx, requestInformation, additionalInfo)
117+
118+
assert.Nil(t, err)
119+
assert.Equal(t, []string{"authentication"}, requestInformation.Headers.Get("Authorization"))
120+
},
121+
},
122+
{
123+
name: "authorization error",
124+
test: func(t *testing.T) {
125+
credential := mocking.NewMockCredential()
126+
credential.On("GetAuthentication").Return("", errors.New("bad auth"))
127+
128+
ctx := mocking.NewMockContext()
129+
requestInformation := abstractions.NewRequestInformation()
130+
additionalInfo := map[string]interface{}{}
131+
132+
requestAdapter := &credentialAuthenticationProviderAdapter{
133+
cred: credential,
134+
}
135+
136+
err := requestAdapter.AuthenticateRequest(ctx, requestInformation, additionalInfo)
137+
138+
assert.Equal(t, errors.New("bad auth"), err)
139+
},
140+
},
141+
}
48142

49143
for _, test := range tests {
50144
t.Run(test.name, test.test)
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package mocking
2+
3+
import (
4+
"context"
5+
6+
abstractions "github.com/microsoft/kiota-abstractions-go"
7+
"github.com/microsoft/kiota-abstractions-go/serialization"
8+
"github.com/microsoft/kiota-abstractions-go/store"
9+
"github.com/stretchr/testify/mock"
10+
)
11+
12+
type MockRequestAdapter struct {
13+
mock.Mock
14+
}
15+
16+
func NewMockRequestAdapter() *MockRequestAdapter {
17+
return &MockRequestAdapter{
18+
mock.Mock{},
19+
}
20+
}
21+
22+
// Send executes the HTTP request specified by the given RequestInformation and returns the deserialized response model.
23+
func (rA *MockRequestAdapter) Send(context context.Context, requestInfo *abstractions.RequestInformation, constructor serialization.ParsableFactory, errorMappings abstractions.ErrorMappings) (serialization.Parsable, error) {
24+
args := rA.Called(context, requestInfo, constructor, errorMappings)
25+
return args.Get(0).(serialization.Parsable), args.Error(1)
26+
}
27+
28+
// SendEnum executes the HTTP request specified by the given RequestInformation and returns the deserialized response model.
29+
func (rA *MockRequestAdapter) SendEnum(context context.Context, requestInfo *abstractions.RequestInformation, parser serialization.EnumFactory, errorMappings abstractions.ErrorMappings) (any, error) {
30+
args := rA.Called(context, requestInfo, parser, errorMappings)
31+
return args.Get(0), args.Error(1)
32+
}
33+
34+
// SendCollection executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection.
35+
func (rA *MockRequestAdapter) SendCollection(context context.Context, requestInfo *abstractions.RequestInformation, constructor serialization.ParsableFactory, errorMappings abstractions.ErrorMappings) ([]serialization.Parsable, error) {
36+
args := rA.Called(context, requestInfo, constructor, errorMappings)
37+
return args.Get(0).([]serialization.Parsable), args.Error(1)
38+
}
39+
40+
// SendEnumCollection executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection.
41+
func (rA *MockRequestAdapter) SendEnumCollection(context context.Context, requestInfo *abstractions.RequestInformation, parser serialization.EnumFactory, errorMappings abstractions.ErrorMappings) ([]any, error) {
42+
args := rA.Called(context, requestInfo, parser, errorMappings)
43+
return args.Get(0).([]any), args.Error(1)
44+
}
45+
46+
// SendPrimitive executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model.
47+
func (rA *MockRequestAdapter) SendPrimitive(context context.Context, requestInfo *abstractions.RequestInformation, typeName string, errorMappings abstractions.ErrorMappings) (any, error) {
48+
args := rA.Called(context, requestInfo, typeName, errorMappings)
49+
return args.Get(0), args.Error(1)
50+
}
51+
52+
// SendPrimitiveCollection executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model collection.
53+
func (rA *MockRequestAdapter) SendPrimitiveCollection(context context.Context, requestInfo *abstractions.RequestInformation, typeName string, errorMappings abstractions.ErrorMappings) ([]any, error) {
54+
args := rA.Called(context, requestInfo, typeName, errorMappings)
55+
return args.Get(0).([]any), args.Error(1)
56+
}
57+
58+
// SendNoContent executes the HTTP request specified by the given RequestInformation with no return content.
59+
func (rA *MockRequestAdapter) SendNoContent(context context.Context, requestInfo *abstractions.RequestInformation, errorMappings abstractions.ErrorMappings) error {
60+
args := rA.Called(context, requestInfo, errorMappings)
61+
return args.Error(1)
62+
}
63+
64+
// GetSerializationWriterFactory returns the serialization writer factory currently in use for the request adapter service.
65+
func (rA *MockRequestAdapter) GetSerializationWriterFactory() serialization.SerializationWriterFactory {
66+
args := rA.Called()
67+
return args.Get(0).(serialization.SerializationWriterFactory)
68+
}
69+
70+
// EnableBackingStore enables the backing store proxies for the SerializationWriters and ParseNodes in use.
71+
func (rA *MockRequestAdapter) EnableBackingStore(factory store.BackingStoreFactory) {
72+
_ = rA.Called(factory)
73+
}
74+
75+
// SetBaseUrl sets the base url for every request.
76+
func (rA *MockRequestAdapter) SetBaseUrl(baseUrl string) { //nolint:stylecheck
77+
_ = rA.Called(baseUrl)
78+
}
79+
80+
// GetBaseUrl gets the base url for every request.
81+
func (rA *MockRequestAdapter) GetBaseUrl() string { //nolint:stylecheck
82+
args := rA.Called()
83+
return args.String(0)
84+
}
85+
86+
// ConvertToNativeRequest converts the given RequestInformation into a native HTTP request.
87+
func (rA *MockRequestAdapter) ConvertToNativeRequest(context context.Context, requestInfo *abstractions.RequestInformation) (any, error) {
88+
args := rA.Called(context, requestInfo)
89+
return args.Get(0), args.Error(1)
90+
}

internal/new/base_request_builder.go

+65-15
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,96 @@
11
package internal
22

3-
import abstractions "github.com/microsoft/kiota-abstractions-go"
3+
import (
4+
"errors"
5+
"strings"
46

5-
type BaseRequestBuilder interface {
7+
abstractions "github.com/microsoft/kiota-abstractions-go"
8+
)
9+
10+
// RequestBuilder
11+
type RequestBuilder interface {
612
GetPathParameters() map[string]string
7-
SetPathParameters(map[string]string)
13+
SetPathParameters(map[string]string) error
814
GetRequestAdapter() abstractions.RequestAdapter
9-
SetRequestAdapter(abstractions.RequestAdapter)
15+
SetRequestAdapter(abstractions.RequestAdapter) error
1016
GetURLTemplate() string
11-
SetURLTemplate(string)
17+
SetURLTemplate(string) error
1218
}
1319

14-
type RequestBuilder struct {
20+
type BaseRequestBuilder struct {
1521
abstractions.BaseRequestBuilder
1622
}
1723

18-
func NewRequestBuilder(requestAdapter abstractions.RequestAdapter, urlTemplate string, pathParameters map[string]string) *RequestBuilder {
19-
return &RequestBuilder{
20-
*abstractions.NewBaseRequestBuilder(requestAdapter, urlTemplate, pathParameters),
24+
func NewBaseRequestBuilder(requestAdapter abstractions.RequestAdapter, urlTemplate string, pathParameters map[string]string) *BaseRequestBuilder {
25+
return &BaseRequestBuilder{
26+
abstractions.BaseRequestBuilder{
27+
PathParameters: pathParameters,
28+
UrlTemplate: urlTemplate,
29+
RequestAdapter: requestAdapter,
30+
},
2131
}
2232
}
2333

24-
func (rB *RequestBuilder) GetPathParameters() map[string]string {
34+
func (rB *BaseRequestBuilder) GetPathParameters() map[string]string {
35+
if IsNil(rB) {
36+
return nil
37+
}
38+
2539
return rB.PathParameters
2640
}
2741

28-
func (rB *RequestBuilder) SetPathParameters(pathParameters map[string]string) {
42+
func (rB *BaseRequestBuilder) SetPathParameters(pathParameters map[string]string) error {
43+
if IsNil(rB) {
44+
return nil
45+
}
46+
47+
if IsNil(pathParameters) {
48+
return errors.New("pathParameters is nil")
49+
}
50+
2951
rB.PathParameters = pathParameters
52+
return nil
3053
}
3154

32-
func (rB *RequestBuilder) GetRequestAdapter() abstractions.RequestAdapter {
55+
func (rB *BaseRequestBuilder) GetRequestAdapter() abstractions.RequestAdapter {
56+
if IsNil(rB) {
57+
return nil
58+
}
59+
3360
return rB.RequestAdapter
3461
}
3562

36-
func (rB *RequestBuilder) SetRequestAdapter(requestAdapter abstractions.RequestAdapter) {
63+
func (rB *BaseRequestBuilder) SetRequestAdapter(requestAdapter abstractions.RequestAdapter) error {
64+
if IsNil(rB) {
65+
return nil
66+
}
67+
68+
if IsNil(requestAdapter) {
69+
return errors.New("requestAdapter is nil")
70+
}
71+
3772
rB.RequestAdapter = requestAdapter
73+
return nil
3874
}
3975

40-
func (rB *RequestBuilder) GetURLTemplate() string {
76+
func (rB *BaseRequestBuilder) GetURLTemplate() string {
77+
if IsNil(rB) {
78+
return ""
79+
}
80+
4181
return rB.UrlTemplate
4282
}
4383

44-
func (rB *RequestBuilder) SetURLTemplate(urlTemplate string) {
84+
func (rB *BaseRequestBuilder) SetURLTemplate(urlTemplate string) error {
85+
if IsNil(rB) {
86+
return nil
87+
}
88+
89+
urlTemplate = strings.TrimSpace(urlTemplate)
90+
if urlTemplate == "" {
91+
return errors.New("urlTemplate is empty")
92+
}
93+
4594
rB.UrlTemplate = urlTemplate
95+
return nil
4696
}

0 commit comments

Comments
 (0)