...
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 "strings"
22 "testing"
23 "time"
24
25 "github.com/sigstore/rekor/pkg/generated/client/index"
26 "go.uber.org/goleak"
27 )
28
29 func TestGetRekorClientWithUserAgent(t *testing.T) {
30 t.Parallel()
31 expectedUserAgent := "test User-Agent"
32 requestReceived := false
33 testServer := httptest.NewServer(http.HandlerFunc(
34 func(w http.ResponseWriter, r *http.Request) {
35 requestReceived = true
36 file := []byte{}
37
38 got := r.UserAgent()
39 if got != expectedUserAgent {
40 t.Errorf("wanted User-Agent %q, got %q", expectedUserAgent, got)
41 }
42 w.WriteHeader(http.StatusOK)
43 _, _ = w.Write(file)
44 }))
45 defer testServer.Close()
46
47 client, err := GetRekorClient(testServer.URL, WithUserAgent(expectedUserAgent))
48 if err != nil {
49 t.Error(err)
50 }
51 _, _ = client.Tlog.GetLogInfo(nil)
52 if !requestReceived {
53 t.Fatal("no requests were received")
54 }
55 }
56
57 func TestGetRekorClientWithCustomPath(t *testing.T) {
58 t.Parallel()
59 requestReceived := false
60 pathAdd := "/custom"
61
62 testServer := httptest.NewServer(http.HandlerFunc(
63 func(w http.ResponseWriter, r *http.Request) {
64 requestReceived = true
65 if !strings.HasPrefix(r.URL.Path, pathAdd) {
66 t.Errorf("Expected request to be sent to /test, got %s", r.URL.Path)
67 }
68 w.WriteHeader(http.StatusOK)
69 }))
70 defer testServer.Close()
71
72 testServer.URL += pathAdd
73
74 client, err := GetRekorClient(testServer.URL)
75 if err != nil {
76 t.Error(err)
77 }
78 _, _ = client.Tlog.GetLogInfo(nil)
79 if !requestReceived {
80 t.Fatal("no requests were received")
81 }
82 }
83
84 func TestGetRekorClientWithRetryCount(t *testing.T) {
85 t.Parallel()
86 expectedCount := 2
87 actualCount := 0
88 testServer := httptest.NewServer(http.HandlerFunc(
89 func(w http.ResponseWriter, _ *http.Request) {
90 actualCount++
91 file := []byte{}
92
93 if actualCount < expectedCount {
94 w.WriteHeader(http.StatusInternalServerError)
95 } else {
96 w.WriteHeader(http.StatusOK)
97 _, _ = w.Write(file)
98 }
99 }))
100 defer testServer.Close()
101
102 client, err := GetRekorClient(testServer.URL, WithRetryCount(2))
103 if err != nil {
104 t.Error(err)
105 }
106 _, err = client.Tlog.GetLogInfo(nil)
107 if err != nil {
108 t.Fatalf("unexpected error: %v", err)
109 }
110 }
111
112 func TestRekorLeakedGoroutine_SearchByHash(t *testing.T) {
113 testServer := httptest.NewUnstartedServer(http.HandlerFunc(
114 func(w http.ResponseWriter, _ *http.Request) {
115 file := []byte("ok")
116
117 w.WriteHeader(http.StatusOK)
118 _, _ = w.Write(file)
119 }))
120 testServer.EnableHTTP2 = true
121 testServer.StartTLS()
122
123 time.Sleep(1 * time.Second)
124
125 opt := goleak.IgnoreCurrent()
126 defer func() {
127 goleak.VerifyNone(t, opt)
128
129 testServer.Close()
130 }()
131 rekor, _ := GetRekorClient(testServer.URL, WithInsecureTLS(true))
132 rekor.Index.SearchIndex(index.NewSearchIndexParams())
133 }
134
View as plain text