Source file src/cmd/go/internal/auth/auth_test.go

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package auth
     6  
     7  import (
     8  	"net/http"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestCredentialCache(t *testing.T) {
    14  	testCases := []netrcLine{
    15  		{"api.github.com", "user", "pwd"},
    16  		{"test.host", "user2", "pwd2"},
    17  		{"oneline", "user3", "pwd3"},
    18  		{"hasmacro.too", "user4", "pwd4"},
    19  		{"hasmacro.too", "user5", "pwd5"},
    20  	}
    21  	for _, tc := range testCases {
    22  		want := http.Request{Header: make(http.Header)}
    23  		want.SetBasicAuth(tc.login, tc.password)
    24  		storeCredential([]string{tc.machine}, want.Header)
    25  		got := &http.Request{Header: make(http.Header)}
    26  		ok := loadCredential(got, tc.machine)
    27  		if !ok || !reflect.DeepEqual(got.Header, want.Header) {
    28  			t.Errorf("loadCredential:\nhave %q\nwant %q", got.Header, want.Header)
    29  		}
    30  	}
    31  }
    32  
    33  func TestCredentialCacheDelete(t *testing.T) {
    34  	// Store a credential for api.github.com
    35  	want := http.Request{Header: make(http.Header)}
    36  	want.SetBasicAuth("user", "pwd")
    37  	storeCredential([]string{"api.github.com"}, want.Header)
    38  	got := &http.Request{Header: make(http.Header)}
    39  	ok := loadCredential(got, "api.github.com")
    40  	if !ok || !reflect.DeepEqual(got.Header, want.Header) {
    41  		t.Errorf("parseNetrc:\nhave %q\nwant %q", got.Header, want.Header)
    42  	}
    43  	// Providing an empty header for api.github.com should clear credentials.
    44  	want = http.Request{Header: make(http.Header)}
    45  	storeCredential([]string{"api.github.com"}, want.Header)
    46  	got = &http.Request{Header: make(http.Header)}
    47  	ok = loadCredential(got, "api.github.com")
    48  	if ok {
    49  		t.Errorf("loadCredential:\nhave %q\nwant %q", got.Header, want.Header)
    50  	}
    51  }
    52  

View as plain text