...

Source file src/github.com/sigstore/rekor/pkg/client/options_test.go

Documentation: github.com/sigstore/rekor/pkg/client

     1  // Copyright 2021 The Sigstore Authors.
     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 client
    16  
    17  import (
    18  	"log"
    19  	"net/http"
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  func TestMakeOptions(t *testing.T) {
    27  	customLogger := log.New(os.Stdout, "", log.LstdFlags)
    28  
    29  	tests := []struct {
    30  		desc string
    31  
    32  		opts []Option
    33  		want *options
    34  	}{{
    35  		desc: "no opts",
    36  		want: &options{RetryCount: DefaultRetryCount},
    37  	}, {
    38  		desc: "WithUserAgent",
    39  		opts: []Option{WithUserAgent("test user agent")},
    40  		want: &options{UserAgent: "test user agent", RetryCount: DefaultRetryCount},
    41  	}, {
    42  		desc: "WithRetryCount",
    43  		opts: []Option{WithRetryCount(2)},
    44  		want: &options{UserAgent: "", RetryCount: 2},
    45  	}, {
    46  		desc: "WithLogger",
    47  		opts: []Option{WithLogger(customLogger)},
    48  		want: &options{UserAgent: "", RetryCount: DefaultRetryCount, Logger: customLogger},
    49  	}, {
    50  		desc: "WithLoggerNil",
    51  		opts: []Option{WithLogger(nil)},
    52  		want: &options{UserAgent: "", RetryCount: DefaultRetryCount},
    53  	}, {
    54  		desc: "WithInsecureTLSEnabled",
    55  		opts: []Option{WithInsecureTLS(true)},
    56  		want: &options{UserAgent: "", RetryCount: DefaultRetryCount, InsecureTLS: true},
    57  	}, {
    58  		desc: "WithInsecureTLSDisabled",
    59  		opts: []Option{WithInsecureTLS(false)},
    60  		want: &options{UserAgent: "", RetryCount: DefaultRetryCount, InsecureTLS: false},
    61  	}}
    62  	for _, tc := range tests {
    63  		t.Run(tc.desc, func(t *testing.T) {
    64  			got := makeOptions(tc.opts...)
    65  			if d := cmp.Diff(tc.want, got, cmp.Comparer(func(a, b *log.Logger) bool { return a == b })); d != "" {
    66  				t.Errorf("makeOptions(%v) returned unexpected result (-want +got): %s", tc.desc, d)
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  type mockRoundTripper struct {
    73  	gotReqs []*http.Request
    74  
    75  	resp *http.Response
    76  	err  error
    77  }
    78  
    79  // RoundTrip implements `http.RoundTripper`
    80  func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    81  	m.gotReqs = append(m.gotReqs, req)
    82  	return m.resp, m.err
    83  }
    84  
    85  func TestCreateRoundTripper(t *testing.T) {
    86  	t.Run("always returns non-nil", func(t *testing.T) {
    87  		got := createRoundTripper(nil, &options{})
    88  		if got == nil {
    89  			t.Errorf("createRoundTripper() should never return a nil `http.RoundTripper`")
    90  		}
    91  	})
    92  
    93  	testReq, err := http.NewRequest("GET", "http://www.example.com/test", nil)
    94  	if err != nil {
    95  		t.Fatalf("http.NewRequest() failed: %v", err)
    96  	}
    97  
    98  	testResp := &http.Response{
    99  		Status:     "OK",
   100  		StatusCode: 200,
   101  		Request:    testReq,
   102  	}
   103  
   104  	expectedUserAgent := "test UserAgent"
   105  
   106  	m := &mockRoundTripper{}
   107  	rt := createRoundTripper(m, &options{
   108  		UserAgent: expectedUserAgent,
   109  	})
   110  	m.resp = testResp
   111  
   112  	gotResp, err := rt.RoundTrip(testReq)
   113  	if err != nil {
   114  		t.Errorf("RoundTrip() returned error: %v", err)
   115  	}
   116  	if len(m.gotReqs) < 1 {
   117  		t.Fatalf("inner RoundTripper.RoundTrip() was not called")
   118  	}
   119  	gotReq := m.gotReqs[0]
   120  	gotReqUserAgent := gotReq.UserAgent()
   121  	if gotReqUserAgent != expectedUserAgent {
   122  		t.Errorf("rt.RoundTrip() did not set the User-Agent properly. Wanted: %q, got: %q", expectedUserAgent, gotReqUserAgent)
   123  	}
   124  
   125  	if testResp != gotResp {
   126  		t.Errorf("roundTripper.RoundTrip() should have returned exactly the response of the inner RoundTripper. Wanted %v, got %v", testResp, gotResp)
   127  	}
   128  }
   129  

View as plain text