...

Source file src/oras.land/oras-go/pkg/auth/client_opts_test.go

Documentation: oras.land/oras-go/pkg/auth

     1  /*
     2  Copyright The ORAS Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package auth
    17  
    18  import (
    19  	"context"
    20  	"net/http"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/suite"
    24  )
    25  
    26  type ClientOptsSuite struct {
    27  	suite.Suite
    28  }
    29  
    30  func (suite *ClientOptsSuite) TestWithLoginContext() {
    31  	settings := &LoginSettings{}
    32  	suite.Nil(settings.Context, "settings.Context is nil by default")
    33  
    34  	ctx := context.Background()
    35  	opt := WithLoginContext(ctx)
    36  	opt(settings)
    37  	suite.Equal(ctx, settings.Context, "Able to override settings.Context")
    38  }
    39  
    40  func (suite *ClientOptsSuite) TestWithLoginHostname() {
    41  	settings := &LoginSettings{}
    42  	suite.Equal("", settings.Hostname, "settings.Hostname is empty string by default")
    43  
    44  	hostname := "example.com"
    45  	opt := WithLoginHostname(hostname)
    46  	opt(settings)
    47  	suite.Equal(hostname, settings.Hostname, "Able to override settings.Hostname")
    48  }
    49  
    50  func (suite *ClientOptsSuite) TestWithLoginUsername() {
    51  	settings := &LoginSettings{}
    52  	suite.Equal("", settings.Username, "settings.Username is empty string by default")
    53  
    54  	username := "fran"
    55  	opt := WithLoginUsername(username)
    56  	opt(settings)
    57  	suite.Equal(username, settings.Username, "Able to override settings.Username")
    58  }
    59  
    60  func (suite *ClientOptsSuite) TestWithLoginSecret() {
    61  	settings := &LoginSettings{}
    62  	suite.Equal("", settings.Secret, "settings.Secret is empty string by default")
    63  
    64  	secret := "shhhhhhhhhh"
    65  	opt := WithLoginSecret(secret)
    66  	opt(settings)
    67  	suite.Equal(secret, settings.Secret, "Able to override settings.Secret")
    68  }
    69  
    70  func (suite *ClientOptsSuite) TestWithLoginInsecure() {
    71  	settings := &LoginSettings{}
    72  	suite.Equal(false, settings.Insecure, "settings.Insecure is false by default")
    73  
    74  	opt := WithLoginInsecure()
    75  	opt(settings)
    76  	suite.Equal(true, settings.Insecure, "Able to override settings.Insecure")
    77  }
    78  
    79  func (suite *ClientOptsSuite) TestWithLoginUserAgent() {
    80  	settings := &LoginSettings{}
    81  	suite.Equal("", settings.UserAgent, "settings.UserAgent is empty string by default")
    82  
    83  	userAgent := "superclient"
    84  	opt := WithLoginUserAgent(userAgent)
    85  	opt(settings)
    86  	suite.Equal(userAgent, settings.UserAgent, "Able to override settings.UserAgent")
    87  }
    88  
    89  func (suite *ClientOptsSuite) TestWithResolverClient() {
    90  	settings := &ResolverSettings{}
    91  	suite.Nil(settings.Client, "settings.Client is nil by default")
    92  
    93  	defaultClient := http.DefaultClient
    94  	opt := WithResolverClient(defaultClient)
    95  	opt(settings)
    96  	suite.Equal(defaultClient, settings.Client, "Able to override settings.Client")
    97  }
    98  
    99  func (suite *ClientOptsSuite) TestWithResolverPlainHTTP() {
   100  	settings := &ResolverSettings{}
   101  	suite.Equal(false, settings.PlainHTTP, "settings.PlainHTTP is false by default")
   102  
   103  	plainHTTP := true
   104  	opt := WithResolverPlainHTTP()
   105  	opt(settings)
   106  	suite.Equal(plainHTTP, settings.PlainHTTP, "Able to override settings.PlainHTTP")
   107  }
   108  
   109  func (suite *ClientOptsSuite) TestWithResolverHeaders() {
   110  	settings := &ResolverSettings{}
   111  	suite.Nil(settings.Headers, "settings.Headers is nil by default")
   112  
   113  	key := "User-Agent"
   114  	value := "oras-go/test"
   115  	headers := http.Header{}
   116  	headers.Set(key, value)
   117  	opt := WithResolverHeaders(headers)
   118  	opt(settings)
   119  	suite.Equal(settings.Headers.Get(key), value, "Able to override settings.Headers")
   120  }
   121  
   122  func TestClientOptsSuite(t *testing.T) {
   123  	suite.Run(t, new(ClientOptsSuite))
   124  }
   125  

View as plain text