Skip to content

Commit b0ba045

Browse files
authored
Fix panic due to nil map (Azure#19396)
* Fix panic due to nil map * rename fake endpoint
1 parent 1e99b00 commit b0ba045

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

sdk/data/azappconfig/policy_sync_token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type syncTokenPolicy struct {
2626
}
2727

2828
func newSyncTokenPolicy() *syncTokenPolicy {
29-
return &syncTokenPolicy{}
29+
return &syncTokenPolicy{syncTokens: map[string]syncToken{}}
3030
}
3131

3232
func parseToken(tok string) (syncToken, error) {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
// Copyright (c) Microsoft Corporation. All rights reserved.
5+
// Licensed under the MIT License. See License.txt in the project root for license information.
6+
7+
package azappconfig
8+
9+
import (
10+
"context"
11+
"errors"
12+
"net/http"
13+
"testing"
14+
15+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
16+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
17+
"github.com/Azure/azure-sdk-for-go/sdk/data/azappconfig/internal/generated"
18+
"github.com/stretchr/testify/require"
19+
)
20+
21+
// TransportFunc is a helper to use a first-class func to satisfy the Transporter interface.
22+
type TransportFunc func(*http.Request) (*http.Response, error)
23+
24+
// Do implements the Transporter interface for the TransportFunc type.
25+
func (pf TransportFunc) Do(req *http.Request) (*http.Response, error) {
26+
return pf(req)
27+
}
28+
29+
type nonRetriableError struct {
30+
error
31+
}
32+
33+
func (nonRetriableError) NonRetriable() {}
34+
35+
func TestSyncTokenPolicy(t *testing.T) {
36+
stp := newSyncTokenPolicy()
37+
require.NotNil(t, stp)
38+
39+
pl := runtime.NewPipeline("TestSyncTokenPolicy", generated.ModuleVersion, runtime.PipelineOptions{PerRetry: []policy.Policy{stp}}, &policy.ClientOptions{
40+
Transport: TransportFunc(func(req *http.Request) (*http.Response, error) {
41+
return &http.Response{
42+
StatusCode: http.StatusOK,
43+
Body: http.NoBody,
44+
Header: http.Header{
45+
"Sync-Token": []string{"jtqGc1I4=MDoyOA==;sn=28"},
46+
},
47+
}, nil
48+
}),
49+
})
50+
51+
req, err := runtime.NewRequest(context.Background(), http.MethodGet, "http://test.contoso.com")
52+
require.NoError(t, err)
53+
54+
resp, err := pl.Do(req)
55+
require.NoError(t, err)
56+
require.NotNil(t, resp)
57+
st := stp.syncTokens["jtqGc1I4"]
58+
require.NotZero(t, st)
59+
require.Equal(t, "jtqGc1I4", st.id)
60+
require.Equal(t, int64(28), st.seqNo)
61+
require.Equal(t, "MDoyOA==", st.value)
62+
}
63+
64+
func TestSyncTokenPolicyError(t *testing.T) {
65+
stp := newSyncTokenPolicy()
66+
require.NotNil(t, stp)
67+
68+
pl := runtime.NewPipeline("TestSyncTokenPolicy", generated.ModuleVersion, runtime.PipelineOptions{PerRetry: []policy.Policy{stp}}, &policy.ClientOptions{
69+
Transport: TransportFunc(func(req *http.Request) (*http.Response, error) {
70+
return nil, nonRetriableError{errors.New("failed")}
71+
}),
72+
})
73+
74+
req, err := runtime.NewRequest(context.Background(), http.MethodGet, "http://test.contoso.com")
75+
require.NoError(t, err)
76+
77+
resp, err := pl.Do(req)
78+
require.Error(t, err)
79+
require.Nil(t, resp)
80+
}

0 commit comments

Comments
 (0)