use a better mockStream in h2quic tests

This commit is contained in:
Marten Seemann
2016-12-16 10:32:46 +07:00
parent a7afd72795
commit a8bbe66d5c
4 changed files with 34 additions and 30 deletions

View File

@@ -13,7 +13,7 @@ var _ = Describe("Request body", func() {
BeforeEach(func() {
stream = &mockStream{}
stream.Write([]byte("foobar")) // provides data to be read
stream.dataToRead.Write([]byte("foobar")) // provides data to be read
rb = newRequestBody(stream)
})

View File

@@ -45,7 +45,7 @@ var _ = Describe("Request", func() {
req, err := http.NewRequest("GET", "https://quic.clemente.io/index.html?foo=bar", nil)
Expect(err).ToNot(HaveOccurred())
rw.WriteRequest(req, 1337)
headerFrame, headerFields := decode(headerStream.Bytes())
headerFrame, headerFields := decode(headerStream.dataWritten.Bytes())
Expect(headerFrame.StreamID).To(Equal(uint32(1337)))
Expect(headerFrame.HasPriority()).To(BeTrue())
Expect(headerFields).To(HaveKeyWithValue(":authority", "quic.clemente.io"))
@@ -60,7 +60,7 @@ var _ = Describe("Request", func() {
req, err := http.NewRequest("POST", "https://quic.clemente.io/upload.html", strings.NewReader(form.Encode()))
Expect(err).ToNot(HaveOccurred())
rw.WriteRequest(req, 5)
_, headerFields := decode(headerStream.Bytes())
_, headerFields := decode(headerStream.dataWritten.Bytes())
Expect(headerFields).To(HaveKeyWithValue(":method", "POST"))
Expect(headerFields).To(HaveKey("content-length"))
contentLength, err := strconv.Atoi(headerFields["content-length"])
@@ -82,7 +82,7 @@ var _ = Describe("Request", func() {
req.AddCookie(cookie1)
req.AddCookie(cookie2)
rw.WriteRequest(req, 11)
_, headerFields := decode(headerStream.Bytes())
_, headerFields := decode(headerStream.dataWritten.Bytes())
Expect(headerFields).To(HaveKeyWithValue("cookie", "Cookie #1=Value #1; Cookie #2=Value #2"))
})
})

View File

@@ -12,10 +12,11 @@ import (
type mockStream struct {
id protocol.StreamID
bytes.Buffer
remoteClosed bool
dataToRead bytes.Buffer
dataWritten bytes.Buffer
reset bool
closed bool
remoteClosed bool
}
func (s *mockStream) Close() error { s.closed = true; return nil }
@@ -23,6 +24,9 @@ func (s *mockStream) Reset(error) { s.reset = true }
func (s *mockStream) CloseRemote(offset protocol.ByteCount) { s.remoteClosed = true }
func (s mockStream) StreamID() protocol.StreamID { return s.id }
func (s *mockStream) Read(p []byte) (int, error) { return s.dataToRead.Read(p) }
func (s *mockStream) Write(p []byte) (int, error) { return s.dataWritten.Write(p) }
var _ = Describe("Response Writer", func() {
var (
w *responseWriter
@@ -38,7 +42,7 @@ var _ = Describe("Response Writer", func() {
It("writes status", func() {
w.WriteHeader(http.StatusTeapot)
Expect(headerStream.Bytes()).To(Equal([]byte{
Expect(headerStream.dataWritten.Bytes()).To(Equal([]byte{
0x0, 0x0, 0x5, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, 'H', 0x3, '4', '1', '8',
}))
})
@@ -46,7 +50,7 @@ var _ = Describe("Response Writer", func() {
It("writes headers", func() {
w.Header().Add("content-length", "42")
w.WriteHeader(http.StatusTeapot)
Expect(headerStream.Bytes()).To(Equal([]byte{
Expect(headerStream.dataWritten.Bytes()).To(Equal([]byte{
0x0, 0x0, 0x9, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, 0x48, 0x3, 0x34, 0x31, 0x38, 0x5c, 0x2, 0x34, 0x32,
}))
})
@@ -55,7 +59,7 @@ var _ = Describe("Response Writer", func() {
w.Header().Add("set-cookie", "test1=1; Max-Age=7200; path=/")
w.Header().Add("set-cookie", "test2=2; Max-Age=7200; path=/")
w.WriteHeader(http.StatusTeapot)
Expect(headerStream.Bytes()).To(Equal([]byte{0x00, 0x00, 0x33, 0x01, 0x04, 0x00, 0x00, 0x00, 0x05,
Expect(headerStream.dataWritten.Bytes()).To(Equal([]byte{0x00, 0x00, 0x33, 0x01, 0x04, 0x00, 0x00, 0x00, 0x05,
0x48, 0x03, 0x34, 0x31, 0x38, 0x77, 0x95, 0x49, 0x50, 0x90, 0xc0, 0x1f, 0xb5, 0x34, 0x0f, 0xca, 0xd0, 0xcc,
0x58, 0x1d, 0x10, 0x01, 0xf6, 0xa5, 0x63, 0x4c, 0xf0, 0x31, 0x77, 0x95, 0x49, 0x50, 0x91, 0x40, 0x2f, 0xb5,
0x34, 0x0f, 0xca, 0xd0, 0xcc, 0x58, 0x1d, 0x10, 0x01, 0xf6, 0xa5, 0x63, 0x4c, 0xf0, 0x31}))
@@ -66,11 +70,11 @@ var _ = Describe("Response Writer", func() {
Expect(n).To(Equal(6))
Expect(err).ToNot(HaveOccurred())
// Should have written 200 on the header stream
Expect(headerStream.Bytes()).To(Equal([]byte{
Expect(headerStream.dataWritten.Bytes()).To(Equal([]byte{
0x0, 0x0, 0x1, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, 0x88,
}))
// And foobar on the data stream
Expect(dataStream.Bytes()).To(Equal([]byte{
Expect(dataStream.dataWritten.Bytes()).To(Equal([]byte{
0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72,
}))
})
@@ -81,11 +85,11 @@ var _ = Describe("Response Writer", func() {
Expect(n).To(Equal(6))
Expect(err).ToNot(HaveOccurred())
// Should have written 418 on the header stream
Expect(headerStream.Bytes()).To(Equal([]byte{
Expect(headerStream.dataWritten.Bytes()).To(Equal([]byte{
0x0, 0x0, 0x5, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, 'H', 0x3, '4', '1', '8',
}))
// And foobar on the data stream
Expect(dataStream.Bytes()).To(Equal([]byte{
Expect(dataStream.dataWritten.Bytes()).To(Equal([]byte{
0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72,
}))
})
@@ -93,7 +97,7 @@ var _ = Describe("Response Writer", func() {
It("does not WriteHeader() twice", func() {
w.WriteHeader(200)
w.WriteHeader(500)
Expect(headerStream.Bytes()).To(Equal([]byte{0x0, 0x0, 0x1, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, 0x88})) // 0x88 is 200
Expect(headerStream.dataWritten.Bytes()).To(Equal([]byte{0x0, 0x0, 0x1, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, 0x88})) // 0x88 is 200
})
It("doesn't allow writes if the status code doesn't allow a body", func() {
@@ -101,6 +105,6 @@ var _ = Describe("Response Writer", func() {
n, err := w.Write([]byte("foobar"))
Expect(n).To(BeZero())
Expect(err).To(MatchError(http.ErrBodyNotAllowed))
Expect(dataStream.Bytes()).To(HaveLen(0))
Expect(dataStream.dataWritten.Bytes()).To(HaveLen(0))
})
})

View File

@@ -70,7 +70,7 @@ var _ = Describe("H2 server", func() {
Expect(r.RemoteAddr).To(Equal("127.0.0.1:42"))
handlerCalled = true
})
headerStream.Write([]byte{
headerStream.dataToRead.Write([]byte{
0x0, 0x0, 0x11, 0x1, 0x5, 0x0, 0x0, 0x0, 0x5,
// Taken from https://http2.github.io/http2-spec/compression.html#request.examples.with.huffman.coding
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,
@@ -84,7 +84,7 @@ var _ = Describe("H2 server", func() {
It("returns 200 with an empty handler", func() {
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
headerStream.Write([]byte{
headerStream.dataToRead.Write([]byte{
0x0, 0x0, 0x11, 0x1, 0x5, 0x0, 0x0, 0x0, 0x5,
// Taken from https://http2.github.io/http2-spec/compression.html#request.examples.with.huffman.coding
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,
@@ -92,7 +92,7 @@ var _ = Describe("H2 server", func() {
err := s.handleRequest(session, headerStream, &sync.Mutex{}, hpackDecoder, h2framer)
Expect(err).NotTo(HaveOccurred())
Eventually(func() []byte {
return headerStream.Buffer.Bytes()
return headerStream.dataWritten.Bytes()
}).Should(Equal([]byte{0x0, 0x0, 0x1, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, 0x88})) // 0x88 is 200
})
@@ -100,7 +100,7 @@ var _ = Describe("H2 server", func() {
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("foobar")
})
headerStream.Write([]byte{
headerStream.dataToRead.Write([]byte{
0x0, 0x0, 0x11, 0x1, 0x5, 0x0, 0x0, 0x0, 0x5,
// Taken from https://http2.github.io/http2-spec/compression.html#request.examples.with.huffman.coding
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,
@@ -108,7 +108,7 @@ var _ = Describe("H2 server", func() {
err := s.handleRequest(session, headerStream, &sync.Mutex{}, hpackDecoder, h2framer)
Expect(err).NotTo(HaveOccurred())
Eventually(func() []byte {
return headerStream.Buffer.Bytes()
return headerStream.dataWritten.Bytes()
}).Should(Equal([]byte{0x0, 0x0, 0x1, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5, 0x8e})) // 0x82 is 500
})
@@ -118,7 +118,7 @@ var _ = Describe("H2 server", func() {
Expect(r.Host).To(Equal("www.example.com"))
handlerCalled = true
})
headerStream.Write([]byte{
headerStream.dataToRead.Write([]byte{
0x0, 0x0, 0x11, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5,
// Taken from https://http2.github.io/http2-spec/compression.html#request.examples.with.huffman.coding
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,
@@ -137,7 +137,7 @@ var _ = Describe("H2 server", func() {
Expect(r.Method).To(Equal("POST"))
handlerCalled = true
})
headerStream.Write([]byte{0x0, 0x0, 0x20, 0x1, 0x24, 0x0, 0x0, 0x0, 0x5, 0x0, 0x0, 0x0, 0x0, 0xff, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, 0x83, 0x84, 0x87, 0x5c, 0x1, 0x37, 0x7a, 0x85, 0xed, 0x69, 0x88, 0xb4, 0xc7})
headerStream.dataToRead.Write([]byte{0x0, 0x0, 0x20, 0x1, 0x24, 0x0, 0x0, 0x0, 0x5, 0x0, 0x0, 0x0, 0x0, 0xff, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, 0x83, 0x84, 0x87, 0x5c, 0x1, 0x37, 0x7a, 0x85, 0xed, 0x69, 0x88, 0xb4, 0xc7})
err := s.handleRequest(session, headerStream, &sync.Mutex{}, hpackDecoder, h2framer)
Expect(err).NotTo(HaveOccurred())
Eventually(func() bool { return dataStream.reset }).Should(BeTrue())
@@ -156,8 +156,8 @@ var _ = Describe("H2 server", func() {
n, _ := r.Body.Read(b)
Expect(n).ToNot(BeZero())
})
headerStream.Write([]byte{0x0, 0x0, 0x20, 0x1, 0x24, 0x0, 0x0, 0x0, 0x5, 0x0, 0x0, 0x0, 0x0, 0xff, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, 0x83, 0x84, 0x87, 0x5c, 0x1, 0x37, 0x7a, 0x85, 0xed, 0x69, 0x88, 0xb4, 0xc7})
dataStream.Write([]byte("foo=bar"))
headerStream.dataToRead.Write([]byte{0x0, 0x0, 0x20, 0x1, 0x24, 0x0, 0x0, 0x0, 0x5, 0x0, 0x0, 0x0, 0x0, 0xff, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, 0x83, 0x84, 0x87, 0x5c, 0x1, 0x37, 0x7a, 0x85, 0xed, 0x69, 0x88, 0xb4, 0xc7})
dataStream.dataToRead.Write([]byte("foo=bar"))
err := s.handleRequest(session, headerStream, &sync.Mutex{}, hpackDecoder, h2framer)
Expect(err).NotTo(HaveOccurred())
Eventually(func() bool { return handlerCalled }).Should(BeTrue())
@@ -165,7 +165,7 @@ var _ = Describe("H2 server", func() {
})
It("errors when non-header frames are received", func() {
headerStream.Write([]byte{
headerStream.dataToRead.Write([]byte{
0x0, 0x0, 0x06, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5,
'f', 'o', 'o', 'b', 'a', 'r',
})
@@ -181,7 +181,7 @@ var _ = Describe("H2 server", func() {
handlerCalled = true
})
headerStream := &mockStream{id: 3}
headerStream.Write([]byte{
headerStream.dataToRead.Write([]byte{
0x0, 0x0, 0x11, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5,
// Taken from https://http2.github.io/http2-spec/compression.html#request.examples.with.huffman.coding
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,
@@ -197,7 +197,7 @@ var _ = Describe("H2 server", func() {
handlerCalled = true
})
headerStream := &mockStream{id: 5}
headerStream.Write([]byte{
headerStream.dataToRead.Write([]byte{
0x0, 0x0, 0x11, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5,
// Taken from https://http2.github.io/http2-spec/compression.html#request.examples.with.huffman.coding
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,
@@ -210,7 +210,7 @@ var _ = Describe("H2 server", func() {
s.CloseAfterFirstRequest = true
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
headerStream := &mockStream{id: 3}
headerStream.Write([]byte{
headerStream.dataToRead.Write([]byte{
0x0, 0x0, 0x11, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5,
// Taken from https://http2.github.io/http2-spec/compression.html#request.examples.with.huffman.coding
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,
@@ -227,7 +227,7 @@ var _ = Describe("H2 server", func() {
handlerCalled = true
}))
headerStream := &mockStream{id: 3}
headerStream.Write([]byte{
headerStream.dataToRead.Write([]byte{
0x0, 0x0, 0x11, 0x1, 0x4, 0x0, 0x0, 0x0, 0x5,
// Taken from https://http2.github.io/http2-spec/compression.html#request.examples.with.huffman.coding
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,