-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify_test.go
58 lines (51 loc) · 1.24 KB
/
verify_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package client
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestOCIVerify(t *testing.T) {
tt := []struct {
name string
transport http.RoundTripper
handler http.HandlerFunc
valid bool
}{
{"golden", &DefaultTransport, testVerifyHandler, true},
{"bad round trip", &badTransport{}, testVerifyHandler, false},
{"verify failed", &DefaultTransport, testInvalidVerifyHandler, false},
}
for _, tc := range tt {
tf := func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(tc.handler))
defer srv.Close()
cfg := Config{
Host: srv.URL,
Transport: tc.transport,
Auth: &TokenAuth{"token"},
}
c, err := New(cfg)
if err != nil {
t.Fatal(err)
}
api := NewDistributionAPI(c)
err = api.Verify()
if err == nil && !tc.valid {
t.Fatal("expected invalid test-case")
}
}
t.Run(tc.name, tf)
}
}
func testVerifyHandler(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/v2" {
w.Header().Set(headerVersionCheck, "registry/2.0")
w.WriteHeader(http.StatusOK)
}
}
func testInvalidVerifyHandler(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/v2" {
w.Header().Set(headerVersionCheck, "invalid")
w.WriteHeader(http.StatusOK)
}
}