Skip to content

Commit

Permalink
azcontainerregistry: move method to function (#23270)
Browse files Browse the repository at this point in the history
The Go documentation strongly discourages structs from having methods
with both pointer recievers and non-pointer recievers. For the auth
policy, `getChallengeRequest` presumably did not have a pointer reciever
since it did not make use of the struct at all. We can simply move this
to a function.

Signed-off-by: Steve Kuznetsov <[email protected]>
  • Loading branch information
stevekuznetsov authored Aug 6, 2024
1 parent 40dad0a commit ea67e9c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
6 changes: 3 additions & 3 deletions sdk/containers/azcontainerregistry/authentication_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"net/http"
"strings"
"sync/atomic"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/internal/temporal"
)

Expand Down Expand Up @@ -77,7 +77,7 @@ func (p *authenticationPolicy) Do(req *policy.Request) (*http.Response, error) {
} else {
// do challenge process for the initial request
var challengeReq *policy.Request
challengeReq, err = p.getChallengeRequest(*req)
challengeReq, err = getChallengeRequest(*req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func findServiceAndScope(resp *http.Response) (string, string, error) {
return valuesMap["service"], valuesMap["scope"], nil
}

func (p authenticationPolicy) getChallengeRequest(oriReq policy.Request) (*policy.Request, error) {
func getChallengeRequest(oriReq policy.Request) (*policy.Request, error) {
copied := oriReq.Clone(oriReq.Raw().Context())
err := copied.SetBody(nil, "")
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,13 @@ func Test_authenticationPolicy_anonymousAccess(t *testing.T) {
require.Error(t, err)
}

func Test_authenticationPolicy_getChallengeRequest(t *testing.T) {
func Test_getChallengeRequest(t *testing.T) {
oriReq, err := runtime.NewRequest(context.Background(), http.MethodPost, "https://test.com")
require.NoError(t, err)
testBody := []byte("test")
err = oriReq.SetBody(streaming.NopCloser(bytes.NewReader(testBody)), "text/plain")
require.NoError(t, err)
p := &authenticationPolicy{}
challengeReq, err := p.getChallengeRequest(*oriReq)
challengeReq, err := getChallengeRequest(*oriReq)
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("%d", len(testBody)), oriReq.Raw().Header.Get("Content-Length"))
require.Equal(t, "", challengeReq.Raw().Header.Get("Content-Length"))
Expand Down

0 comments on commit ea67e9c

Please sign in to comment.