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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
package handler
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"git.sigsum.org/sigsum-go/pkg/types"
)
type dummyConfig struct {
prefix string
}
func (c dummyConfig) Prefix() string { return c.prefix }
func (c dummyConfig) LogID() string { return "dummyLogID" }
func (c dummyConfig) Deadline() time.Duration { return time.Nanosecond }
// TestPath checks that Path works for an endpoint (add-leaf)
func TestPath(t *testing.T) {
testFun := func(_ context.Context, _ Config, _ http.ResponseWriter, _ *http.Request) (int, error) {
return 0, nil
}
for _, table := range []struct {
description string
prefix string
want string
}{
{
description: "no prefix",
want: "/sigsum/v0/add-leaf",
},
{
description: "a prefix",
prefix: "test-prefix",
want: "/test-prefix/sigsum/v0/add-leaf",
},
} {
testConfig := dummyConfig{
prefix: table.prefix,
}
h := Handler{testConfig, testFun, types.EndpointAddLeaf, http.MethodPost}
if got, want := h.Path(), table.want; got != want {
t.Errorf("got path %v but wanted %v", got, want)
}
}
}
// func TestServeHTTP(t *testing.T) {
// h.ServeHTTP(w http.ResponseWriter, r *http.Request)
// }
func TestVerifyMethod(t *testing.T) {
badMethod := http.MethodHead
for _, h := range []Handler{
{
Endpoint: types.EndpointAddLeaf,
Method: http.MethodPost,
},
{
Endpoint: types.EndpointGetTreeHeadToCosign,
Method: http.MethodGet,
},
} {
for _, method := range []string{
http.MethodGet,
http.MethodPost,
badMethod,
} {
url := h.Endpoint.Path("http://log.example.com", "fixme")
req, err := http.NewRequest(method, url, nil)
if err != nil {
t.Fatalf("must create HTTP request: %v", err)
}
w := httptest.NewRecorder()
code := h.verifyMethod(w, req)
if got, want := code == 0, h.Method == method; got != want {
t.Errorf("%s %s: got %v but wanted %v: %v", method, url, got, want, err)
continue
}
if code == 0 {
continue
}
if method == badMethod {
if got, want := code, http.StatusBadRequest; got != want {
t.Errorf("%s %s: got status %d, wanted %d", method, url, got, want)
}
if _, ok := w.Header()["Allow"]; ok {
t.Errorf("%s %s: got Allow header, wanted none", method, url)
}
continue
}
if got, want := code, http.StatusMethodNotAllowed; got != want {
t.Errorf("%s %s: got status %d, wanted %d", method, url, got, want)
} else if methods, ok := w.Header()["Allow"]; !ok {
t.Errorf("%s %s: got no allow header, expected one", method, url)
} else if got, want := len(methods), 1; got != want {
t.Errorf("%s %s: got %d allowed method(s), wanted %d", method, url, got, want)
} else if got, want := methods[0], h.Method; got != want {
t.Errorf("%s %s: got allowed method %s, wanted %s", method, url, got, want)
}
}
}
}
// func TestHandle(t *testing.T) {
// h.handle(w http.ResponseWriter, r *http.Request)
// }
|