...

Source file src/github.com/sigstore/timestamp-authority/pkg/client/options_test.go

Documentation: github.com/sigstore/timestamp-authority/pkg/client

     1  // Copyright 2022 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  	"net/http"
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  )
    23  
    24  func TestMakeOptions(t *testing.T) {
    25  	tests := []struct {
    26  		desc string
    27  
    28  		opts []Option
    29  		want *options
    30  	}{{
    31  		desc: "no opts",
    32  		want: &options{},
    33  	}, {
    34  		desc: "WithUserAgent",
    35  		opts: []Option{WithUserAgent("test user agent")},
    36  		want: &options{UserAgent: "test user agent"},
    37  	}}
    38  	for _, tc := range tests {
    39  		t.Run(tc.desc, func(t *testing.T) {
    40  			got := makeOptions(tc.opts...)
    41  			if d := cmp.Diff(tc.want, got); d != "" {
    42  				t.Errorf("makeOptions() returned unexpected result (-want +got): %s", d)
    43  			}
    44  		})
    45  	}
    46  }
    47  
    48  type mockRoundTripper struct {
    49  	gotReqs []*http.Request
    50  
    51  	resp *http.Response
    52  	err  error
    53  }
    54  
    55  // RoundTrip implements `http.RoundTripper`
    56  func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    57  	m.gotReqs = append(m.gotReqs, req)
    58  	return m.resp, m.err
    59  }
    60  
    61  func TestCreateRoundTripper(t *testing.T) {
    62  	t.Run("always returns non-nil", func(t *testing.T) {
    63  		got := createRoundTripper(nil, &options{})
    64  		if got == nil {
    65  			t.Errorf("createRoundTripper() should never return a nil `http.RoundTripper`")
    66  		}
    67  	})
    68  
    69  	testReq, err := http.NewRequest("GET", "http://www.example.com/test", nil)
    70  	if err != nil {
    71  		t.Fatalf("http.NewRequest() failed: %v", err)
    72  	}
    73  
    74  	testResp := &http.Response{
    75  		Status:     "OK",
    76  		StatusCode: 200,
    77  		Request:    testReq,
    78  	}
    79  
    80  	expectedUserAgent := "test UserAgent"
    81  	expectedContentType := "test ContentType"
    82  
    83  	m := &mockRoundTripper{}
    84  	rt := createRoundTripper(m, &options{
    85  		UserAgent:   expectedUserAgent,
    86  		ContentType: expectedContentType,
    87  	})
    88  	m.resp = testResp
    89  
    90  	gotResp, err := rt.RoundTrip(testReq)
    91  	if err != nil {
    92  		t.Errorf("RoundTrip() returned error: %v", err)
    93  	}
    94  	if len(m.gotReqs) < 1 {
    95  		t.Fatalf("inner RoundTripper.RoundTrip() was not called")
    96  	}
    97  	gotReq := m.gotReqs[0]
    98  	gotReqUserAgent := gotReq.UserAgent()
    99  	if gotReqUserAgent != expectedUserAgent {
   100  		t.Errorf("rt.RoundTrip() did not set the User-Agent properly. Wanted: %q, got: %q", expectedUserAgent, gotReqUserAgent)
   101  	}
   102  	gotReqContentType := gotReq.Header.Get("Content-Type")
   103  	if gotReqContentType != expectedContentType {
   104  		t.Errorf("rt.RoundTrip() did not set the Content-Type properly. Wanted: %q, got: %q", expectedContentType, gotReqContentType)
   105  	}
   106  
   107  	if testResp != gotResp {
   108  		t.Errorf("roundTripper.RoundTrip() should have returned exactly the response of the inner RoundTripper. Wanted %v, got %v", testResp, gotResp)
   109  	}
   110  }
   111  

View as plain text