...

Source file src/github.com/google/go-containerregistry/pkg/v1/remote/transport/basic_test.go

Documentation: github.com/google/go-containerregistry/pkg/v1/remote/transport

     1  // Copyright 2018 Google LLC All Rights Reserved.
     2  //
     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  package transport
    16  
    17  import (
    18  	"net/http"
    19  	"net/http/httptest"
    20  	"net/url"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/google/go-containerregistry/pkg/authn"
    25  )
    26  
    27  func TestBasicTransport(t *testing.T) {
    28  	username := "foo"
    29  	password := "bar"
    30  	server := httptest.NewServer(
    31  		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    32  			hdr := r.Header.Get("Authorization")
    33  			if !strings.HasPrefix(hdr, "Basic ") {
    34  				t.Errorf("Header.Get(Authorization); got %v, want Basic prefix", hdr)
    35  			}
    36  			user, pass, _ := r.BasicAuth()
    37  			if user != username || pass != password {
    38  				t.Error("Invalid credentials.")
    39  			}
    40  			if r.URL.Path == "/v2/auth" {
    41  				http.Redirect(w, r, "/redirect", http.StatusMovedPermanently)
    42  				return
    43  			}
    44  			w.WriteHeader(http.StatusOK)
    45  		}))
    46  	defer server.Close()
    47  
    48  	inner := &http.Transport{
    49  		Proxy: func(req *http.Request) (*url.URL, error) {
    50  			return url.Parse(server.URL)
    51  		},
    52  	}
    53  
    54  	basic := &authn.Basic{Username: username, Password: password}
    55  	client := http.Client{Transport: &basicTransport{inner: inner, auth: basic, target: "gcr.io"}}
    56  
    57  	_, err := client.Get("http://gcr.io/v2/auth")
    58  	if err != nil {
    59  		t.Errorf("Unexpected error during Get: %v", err)
    60  	}
    61  }
    62  
    63  func TestBasicTransportRegistryToken(t *testing.T) {
    64  	token := "mytoken"
    65  	for _, tc := range []struct {
    66  		auth    authn.Authenticator
    67  		hdr     string
    68  		wantErr bool
    69  	}{{
    70  		auth: authn.FromConfig(authn.AuthConfig{RegistryToken: token}),
    71  		hdr:  "Bearer mytoken",
    72  	}, {
    73  		auth: authn.FromConfig(authn.AuthConfig{Auth: token}),
    74  		hdr:  "Basic mytoken",
    75  	}, {
    76  		auth: authn.Anonymous,
    77  		hdr:  "",
    78  	}, {
    79  		auth:    &badAuth{},
    80  		hdr:     "",
    81  		wantErr: true,
    82  	}} {
    83  		server := httptest.NewServer(
    84  			http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    85  				hdr := r.Header.Get("Authorization")
    86  				want := tc.hdr
    87  				if hdr != want {
    88  					t.Errorf("Header.Get(Authorization); got %v, want %s", hdr, want)
    89  				}
    90  				if r.URL.Path == "/v2/auth" {
    91  					http.Redirect(w, r, "/redirect", http.StatusMovedPermanently)
    92  					return
    93  				}
    94  				w.WriteHeader(http.StatusOK)
    95  			}))
    96  		defer server.Close()
    97  
    98  		inner := &http.Transport{
    99  			Proxy: func(req *http.Request) (*url.URL, error) {
   100  				return url.Parse(server.URL)
   101  			},
   102  		}
   103  
   104  		client := http.Client{Transport: &basicTransport{inner: inner, auth: tc.auth, target: "gcr.io"}}
   105  
   106  		_, err := client.Get("http://gcr.io/v2/auth")
   107  		if err != nil && !tc.wantErr {
   108  			t.Errorf("Unexpected error during Get: %v", err)
   109  		}
   110  	}
   111  }
   112  
   113  func TestBasicTransportWithEmptyAuthnCred(t *testing.T) {
   114  	server := httptest.NewServer(
   115  		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   116  			if c, ok := r.Header["Authorization"]; ok && c[0] == "" {
   117  				t.Error("got empty Authorization header")
   118  			}
   119  			if r.URL.Path == "/v2/auth" {
   120  				http.Redirect(w, r, "/redirect", http.StatusMovedPermanently)
   121  				return
   122  			}
   123  			w.WriteHeader(http.StatusOK)
   124  		}))
   125  	defer server.Close()
   126  
   127  	inner := &http.Transport{
   128  		Proxy: func(req *http.Request) (*url.URL, error) {
   129  			return url.Parse(server.URL)
   130  		},
   131  	}
   132  
   133  	client := http.Client{Transport: &basicTransport{inner: inner, auth: authn.Anonymous, target: "gcr.io"}}
   134  	_, err := client.Get("http://gcr.io/v2/auth")
   135  	if err != nil {
   136  		t.Errorf("Unexpected error during Get: %v", err)
   137  	}
   138  }
   139  

View as plain text