generated from denpeshkov/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
159 lines (138 loc) · 4.47 KB
/
http_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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package httpsign
import (
"context"
"crypto"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/denpeshkov/httpsign/hmac"
)
const (
key = "test-key"
kid = "test-key-id"
)
func TestQueryEncode(t *testing.T) {
tests := []struct {
values url.Values
encoded string
}{
{nil, ""},
{url.Values{}, ""},
{url.Values{"k": {"v"}}, "k=v"},
{url.Values{"k1": {"v1"}, "k2": {"v2"}}, "k1=v1&k2=v2"},
{url.Values{"k1": {"v1_1", "v1_2"}, "k2": {"v2"}}, "k1=v1_1&k1=v1_2&k2=v2"},
{url.Values{"k1": {"v1_2", "v1_1"}, "k2": {"v2"}}, "k1=v1_1&k1=v1_2&k2=v2"},
{url.Values{"k1": {"v1"}, "k2": {"v2"}, "k3": {"v3"}}, "k1=v1&k2=v2&k3=v3"},
{url.Values{"k1": {"v1"}, "k2": {"v2_1", "v2_2"}, "k3": {"v3_1", "v3_2"}}, "k1=v1&k2=v2_1&k2=v2_2&k3=v3_1&k3=v3_2"},
{url.Values{"k1": {"v1"}, "k2": {"v2_2", "v2_1"}, "k3": {"v3_1", "v3_2"}}, "k1=v1&k2=v2_1&k2=v2_2&k3=v3_1&k3=v3_2"},
{url.Values{"k1": {"v1"}, "k2": {"v2_2", "v2_1"}, "k3": {"v3_2", "v3_1"}}, "k1=v1&k2=v2_1&k2=v2_2&k3=v3_1&k3=v3_2"},
{url.Values{"k": {"v4", "v3", "v2", "v1"}}, "k=v1&k=v2&k=v3&k=v4"},
}
for _, tt := range tests {
if got := (query{tt.values}).Encode(); got != tt.encoded {
t.Errorf(`Encode(%+v) = %q, want %q`, tt.values, got, tt.encoded)
}
}
}
func loggingErrorHandler(t *testing.T) func(w http.ResponseWriter, r *http.Request, err error) {
return func(w http.ResponseWriter, r *http.Request, err error) {
t.Helper()
t.Logf("Handle request for URL: %q, error: %v", r.URL, err)
DefaultErrorHandler(w, r, err)
}
}
type staticSignerSource struct{ s Signer }
func (s staticSignerSource) Signer(context.Context, string) (Signer, error) { return s.s, nil }
type staticVerifierSource struct{ v Verifier }
func (s staticVerifierSource) Verifier(context.Context, string) (Verifier, error) { return s.v, nil }
func TestTransportMiddleware(t *testing.T) {
urls := []string{
"", "?k1=v1", "?k1=v&k2=v", "?k1=v1&k1=v2&k2=v",
"/", "/?k1=v1", "/?k1=v&k2=v", "/?k1=v1&k1=v2&k2=v",
"/p", "/p?k1=v1", "/p?k1=v&k2=v", "/p?k1=v1&k1=v2&k2=v",
"/p/h", "/p/h?k1=v", "/p/h?k1=v&k2=v", "/p/h?k1=v1&k1=v2&k2=v",
}
sigver, err := hmac.New([]byte(key), crypto.SHA256)
if err != nil {
t.Fatalf("Failed to create HMAC: %v", err)
}
c := http.Client{Transport: NewTransport(staticSignerSource{sigver}, kid)}
mw := Middleware(staticVerifierSource{sigver}, loggingErrorHandler(t))
var h http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "test response body")
})
h = mw(h)
s := httptest.NewServer(h)
defer s.Close()
for _, u := range urls {
u, err := url.JoinPath(s.URL, u)
if err != nil {
t.Fatalf("JoinPath(%q, %q) failed: %v", s.URL, u, err)
}
resp, err := c.Get(u)
if err != nil {
t.Fatalf("Get(%s) failed: %v", u, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Get(%q); code: %d, want %d", u, resp.StatusCode, http.StatusOK)
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
t.Logf("Response body: %q", body)
}
}
}
func TestMiddleware(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "http://example.com", nil)
if err != nil {
t.Fatalf("Failed to build request: %v", err)
}
sigver, err := hmac.New([]byte(key), crypto.SHA256)
if err != nil {
t.Fatalf("Failed to create HMAC: %v", err)
}
if err := Sign(sigver, time.Now(), req); err != nil {
t.Fatalf("Sign() failed: %v", err)
}
req.Header.Add(KidHeader, "kid")
tests := []struct {
name string
headerf func(h http.Header)
}{
{
name: "missing " + TimestampHeader,
headerf: func(h http.Header) { h.Del(TimestampHeader) },
},
{
name: "missing " + KidHeader,
headerf: func(h http.Header) { h.Del(KidHeader) },
},
{
name: "missing " + SignatureHeader,
headerf: func(h http.Header) { h.Del(SignatureHeader) },
},
{
name: "invalid signature",
headerf: func(h http.Header) { h.Set(SignatureHeader, h.Get(SignatureHeader)+"malformed") },
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := req.Clone(context.Background())
tt.headerf(r.Header)
w := httptest.NewRecorder()
h := Middleware(staticVerifierSource{sigver}, loggingErrorHandler(t))(http.NotFoundHandler())
h.ServeHTTP(w, r)
//nolint:bodyclose // Returned body is a NopCloser.
if c := w.Result().StatusCode; c != http.StatusUnauthorized {
t.Errorf("Status = %d, want %d", c, http.StatusUnauthorized)
}
})
}
}