...

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

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

     1  //
     2  // Copyright 2022 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package client
    17  
    18  import (
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"testing"
    22  )
    23  
    24  func TestGetTimestampClientWithOptions(t *testing.T) {
    25  	t.Parallel()
    26  	expectedUserAgent := "test User-Agent"
    27  	expectedContentType := "application/timestamp-query"
    28  	requestReceived := false
    29  	testServer := httptest.NewServer(http.HandlerFunc(
    30  		func(w http.ResponseWriter, r *http.Request) {
    31  			file := []byte{}
    32  
    33  			got := r.UserAgent()
    34  			if got != expectedUserAgent {
    35  				w.WriteHeader(http.StatusInternalServerError)
    36  				return
    37  			}
    38  
    39  			var expectedAccept string
    40  			if r.URL.Path == "/api/v1/timestamp/certchain" {
    41  				expectedAccept = "application/pem-certificate-chain"
    42  			} else if r.URL.Path == "/api/v1/timestamp" {
    43  				expectedAccept = "application/timestamp-reply"
    44  			}
    45  
    46  			accept := r.Header["Accept"][0]
    47  			if accept != expectedAccept {
    48  				w.WriteHeader(http.StatusInternalServerError)
    49  				return
    50  			}
    51  
    52  			w.WriteHeader(http.StatusOK)
    53  			_, _ = w.Write(file)
    54  
    55  			requestReceived = true
    56  		}))
    57  	defer testServer.Close()
    58  
    59  	client, err := GetTimestampClient(testServer.URL, WithUserAgent(expectedUserAgent), WithContentType(expectedContentType))
    60  	if err != nil {
    61  		t.Error(err)
    62  	}
    63  	_, _ = client.Timestamp.GetTimestampCertChain(nil)
    64  	if !requestReceived {
    65  		t.Fatal("no requests were received")
    66  	}
    67  	// reset
    68  	requestReceived = false
    69  
    70  	_, _ = client.Timestamp.GetTimestampResponse(nil, nil)
    71  	if !requestReceived {
    72  		t.Fatal("no requests were received")
    73  	}
    74  }
    75  

View as plain text