...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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