-
Notifications
You must be signed in to change notification settings - Fork 2
/
http2_test.go
160 lines (146 loc) · 4.38 KB
/
http2_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
160
package httpsign
import (
"bufio"
"bytes"
"crypto/tls"
"github.com/andreyvit/diff"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"text/template"
)
var wantFields = `"kuku": my awesome header
"@query": ?k1=v1&k2
"@method": GET
"@target-uri": {{.Scheme}}://127.0.0.1:{{.Port}}/path?k1=v1&k2
"@request-target": /path?k1=v1&k2
"@authority": 127.0.0.1:{{.Port}}
"@scheme": {{.Scheme}}
"@target-uri": {{.Scheme}}://127.0.0.1:{{.Port}}/path?k1=v1&k2
"@path": /path
"@query": ?k1=v1&k2
"@query-param";name="k1": v1
"@query-param";name="k2":
"@signature-params": ("kuku" "@query" "@method" "@target-uri" "@request-target" "@authority" "@scheme" "@target-uri" "@path" "@query" "@query-param";name="k1" "@query-param";name="k2");alg="hmac-sha256";keyid="key1"`
func execTemplate(t template.Template, name string, data interface{}) (string, error) {
buf := &bytes.Buffer{}
err := t.ExecuteTemplate(buf, name, data)
return buf.String(), err
}
func newClientRequest(t *testing.T, method, url, body string) *http.Request {
in := strings.NewReader(body)
req, err := http.NewRequest(method, url, bufio.NewReader(in))
if err != nil {
t.Errorf("could not read request")
}
return req
}
var ts *httptest.Server // global, so can be used *inside* the server, too
func testHTTP(t *testing.T, proto string) {
simpleHandler := func(w http.ResponseWriter, r *http.Request) {
reqProto := r.Proto
if reqProto != proto {
t.Errorf("expected %s, got %s", proto, reqProto)
}
var scheme string
if ts.TLS == nil {
scheme = "http"
} else {
scheme = "https"
}
sp := bytes.Split([]byte(ts.URL), []byte(":"))
portval, err := strconv.Atoi(string(sp[2]))
if err != nil {
t.Errorf("cannot parse server port number")
}
tpl, err := template.New("fields").Parse(wantFields)
if err != nil {
t.Errorf("could not parse template")
}
type inputs struct {
Port int
Scheme string
}
// Use the Template facility to create the list of expected signed fields
wf, err := execTemplate(*tpl, "fields", inputs{Port: portval, Scheme: scheme})
if err != nil {
t.Errorf("execTemplate failed")
}
verifier, err := NewHMACSHA256Verifier(bytes.Repeat([]byte{0x03}, 64),
NewVerifyConfig().SetVerifyCreated(false).SetKeyID("key1"),
Headers("@query"))
if err != nil {
t.Errorf("could not create verifier")
}
sigInput, err := verifyRequestDebug("sig1", *verifier, r)
if err != nil {
t.Errorf("failed to verify request: sig input: %s\nerr: %v", sigInput, err)
}
if sigInput != wf {
t.Errorf("unexpected fields: %s\n", diff.CharacterDiff(sigInput, wantFields))
}
w.WriteHeader(200)
}
// And run the client code...
simpleClient(t, proto, simpleHandler)
}
func simpleClient(t *testing.T, proto string, simpleHandler func(w http.ResponseWriter, r *http.Request)) {
// Client code
switch proto {
case "HTTP/1.1":
ts = httptest.NewServer(http.HandlerFunc(simpleHandler))
case "HTTP/2.0":
ts = httptest.NewUnstartedServer(http.HandlerFunc(simpleHandler))
ts.EnableHTTP2 = true
ts.StartTLS()
default:
t.Errorf("no server")
}
defer ts.Close()
signer, err := NewHMACSHA256Signer(bytes.Repeat([]byte{0x03}, 64),
NewSignConfig().SetKeyID("key1").SignCreated(false),
*NewFields().AddHeaders("kuku", "@query", "@method", "@target-uri", "@request-target", "@authority", "@scheme", "@target-uri",
"@path", "@query").AddQueryParam("k1").AddQueryParam("k2"))
if err != nil {
t.Errorf("failed to create signer")
}
var client *Client
switch proto {
case "HTTP/1.1":
client = NewDefaultClient(NewClientConfig().SetSignatureName("sig1").SetSigner(signer))
case "HTTP/2.0":
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // Do not verify server certificate
ForceAttemptHTTP2: true,
}
c := &http.Client{Transport: tr}
client = NewClient(*c, NewClientConfig().SetSignatureName("sig1").SetSigner(signer))
default:
t.Errorf("no client for you")
}
req := newClientRequest(t, "GET", ts.URL+"/path"+"?k1=v1&k2", "")
req.Header.Set("Kuku", "my awesome header")
res, err := client.Do(req)
if err != nil {
t.Errorf("%v", err)
}
if res != nil {
_, err = io.ReadAll(res.Body)
_ = res.Body.Close()
if err != nil {
t.Errorf("%v", err)
}
if res.Status != "200 OK" {
t.Errorf("Bad status returned")
}
}
}
func TestHTTP11(t *testing.T) {
testHTTP(t, "HTTP/1.1")
}
func TestHTTP20(t *testing.T) {
testHTTP(t, "HTTP/2.0")
}