forked from quic-go/quic-go
add request filling to h2quic
This commit is contained in:
51
h2quic/request.go
Normal file
51
h2quic/request.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package h2quic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"golang.org/x/net/http2/hpack"
|
||||
)
|
||||
|
||||
func requestFromHeaders(headers []hpack.HeaderField) (*http.Request, error) {
|
||||
var path, authority, method string
|
||||
httpHeaders := http.Header{}
|
||||
|
||||
for _, h := range headers {
|
||||
switch h.Name {
|
||||
case ":path":
|
||||
path = h.Value
|
||||
case ":method":
|
||||
method = h.Value
|
||||
case ":authority":
|
||||
authority = h.Value
|
||||
default:
|
||||
if !h.IsPseudo() {
|
||||
httpHeaders.Add(h.Name, h.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(path) == 0 || len(authority) == 0 || len(method) == 0 {
|
||||
return nil, errors.New(":path, :authority and :method must not be empty")
|
||||
}
|
||||
|
||||
u, err := url.Parse(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &http.Request{
|
||||
Method: method,
|
||||
URL: u,
|
||||
Proto: "HTTP/2.0",
|
||||
ProtoMajor: 2,
|
||||
ProtoMinor: 0,
|
||||
Header: httpHeaders,
|
||||
Body: nil,
|
||||
// ContentLength: -1,
|
||||
Host: authority,
|
||||
RequestURI: path,
|
||||
}, nil
|
||||
}
|
||||
75
h2quic/request_test.go
Normal file
75
h2quic/request_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package h2quic
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/net/http2/hpack"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Request", func() {
|
||||
It("populates request", func() {
|
||||
headers := []hpack.HeaderField{
|
||||
hpack.HeaderField{":path", "/foo", false},
|
||||
hpack.HeaderField{":authority", "quic.clemente.io", false},
|
||||
hpack.HeaderField{":method", "GET", false},
|
||||
}
|
||||
req, err := requestFromHeaders(headers)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(req.Method).To(Equal("GET"))
|
||||
Expect(req.URL.Path).To(Equal("/foo"))
|
||||
Expect(req.Proto).To(Equal("HTTP/2.0"))
|
||||
Expect(req.ProtoMajor).To(Equal(2))
|
||||
Expect(req.ProtoMinor).To(Equal(0))
|
||||
Expect(req.Header).To(BeEmpty())
|
||||
Expect(req.Body).To(BeNil())
|
||||
Expect(req.Host).To(Equal("quic.clemente.io"))
|
||||
Expect(req.RequestURI).To(Equal("/foo"))
|
||||
})
|
||||
|
||||
It("handles other headers", func() {
|
||||
headers := []hpack.HeaderField{
|
||||
hpack.HeaderField{":path", "/foo", false},
|
||||
hpack.HeaderField{":authority", "quic.clemente.io", false},
|
||||
hpack.HeaderField{":method", "GET", false},
|
||||
hpack.HeaderField{"content-length", "42", false},
|
||||
hpack.HeaderField{"duplicate-header", "1", false},
|
||||
hpack.HeaderField{"duplicate-header", "2", false},
|
||||
}
|
||||
req, err := requestFromHeaders(headers)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(req.Header).To(Equal(http.Header{
|
||||
"Content-Length": []string{"42"},
|
||||
"Duplicate-Header": []string{"1", "2"},
|
||||
}))
|
||||
})
|
||||
|
||||
It("errors with missing path", func() {
|
||||
headers := []hpack.HeaderField{
|
||||
hpack.HeaderField{":authority", "quic.clemente.io", false},
|
||||
hpack.HeaderField{":method", "GET", false},
|
||||
}
|
||||
_, err := requestFromHeaders(headers)
|
||||
Expect(err).To(MatchError(":path, :authority and :method must not be empty"))
|
||||
})
|
||||
|
||||
It("errors with missing method", func() {
|
||||
headers := []hpack.HeaderField{
|
||||
hpack.HeaderField{":path", "/foo", false},
|
||||
hpack.HeaderField{":authority", "quic.clemente.io", false},
|
||||
}
|
||||
_, err := requestFromHeaders(headers)
|
||||
Expect(err).To(MatchError(":path, :authority and :method must not be empty"))
|
||||
})
|
||||
|
||||
It("errors with missing authority", func() {
|
||||
headers := []hpack.HeaderField{
|
||||
hpack.HeaderField{":path", "/foo", false},
|
||||
hpack.HeaderField{":method", "GET", false},
|
||||
}
|
||||
_, err := requestFromHeaders(headers)
|
||||
Expect(err).To(MatchError(":path, :authority and :method must not be empty"))
|
||||
})
|
||||
})
|
||||
@@ -59,6 +59,7 @@ func (w *responseWriter) WriteHeader(status int) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Test
|
||||
func (w *responseWriter) Write(p []byte) (int, error) {
|
||||
if !w.headerWritten {
|
||||
w.WriteHeader(200)
|
||||
|
||||
Reference in New Issue
Block a user