add includeStreamFrames option to PacketPacker

This commit is contained in:
Lucas Clemente 2016-04-26 19:04:42 +02:00
parent 270b2f0f99
commit 5d8293716e
3 changed files with 45 additions and 22 deletions

View file

@ -21,7 +21,7 @@ var _ = Describe("Packet packer", func() {
})
It("returns nil when no packet is queued", func() {
p, err := packer.PackPacket([]frames.Frame{})
p, err := packer.PackPacket([]frames.Frame{}, true)
Expect(p).To(BeNil())
Expect(err).ToNot(HaveOccurred())
})
@ -32,7 +32,7 @@ var _ = Describe("Packet packer", func() {
Data: []byte{0xDE, 0xCA, 0xFB, 0xAD},
}
packer.AddStreamFrame(f)
p, err := packer.PackPacket([]frames.Frame{})
p, err := packer.PackPacket([]frames.Frame{}, true)
Expect(p).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
b := &bytes.Buffer{}
@ -41,6 +41,25 @@ var _ = Describe("Packet packer", func() {
Expect(p.raw).To(ContainSubstring(string(b.Bytes())))
})
It("does not pack stream frames if includeStreamFrames=false", func() {
f := frames.StreamFrame{
StreamID: 5,
Data: []byte{0xDE, 0xCA, 0xFB, 0xAD},
}
packer.AddStreamFrame(f)
p, err := packer.PackPacket([]frames.Frame{}, false)
Expect(err).ToNot(HaveOccurred())
Expect(p).To(BeNil())
})
It("packs only control frames", func() {
p, err := packer.PackPacket([]frames.Frame{&frames.ConnectionCloseFrame{}}, false)
Expect(p).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
Expect(len(p.frames)).To(Equal(1))
Expect(p.raw).NotTo(HaveLen(0))
})
It("packs multiple stream frames into single packet", func() {
f1 := frames.StreamFrame{
StreamID: 5,
@ -52,7 +71,7 @@ var _ = Describe("Packet packer", func() {
}
packer.AddStreamFrame(f1)
packer.AddStreamFrame(f2)
p, err := packer.PackPacket([]frames.Frame{})
p, err := packer.PackPacket([]frames.Frame{}, true)
Expect(p).ToNot(BeNil())
Expect(err).ToNot(HaveOccurred())
b := &bytes.Buffer{}
@ -72,10 +91,10 @@ var _ = Describe("Packet packer", func() {
// packer.AddFrame(f)
// counter++
// }
// payloadFrames, err := packer.composeNextPacket([]frames.Frame{})
// payloadFrames, err := packer.composeNextPacket([]frames.Frame{}, true)
// Expect(err).ToNot(HaveOccurred())
// Expect(len(payloadFrames)).To(Equal(maxFramesPerPacket))
// payloadFrames, err = packer.composeNextPacket([]frames.Frame{})
// payloadFrames, err = packer.composeNextPacket([]frames.Frame{}, true)
// Expect(err).ToNot(HaveOccurred())
// Expect(len(payloadFrames)).To(Equal(counter - maxFramesPerPacket))
// })
@ -88,10 +107,10 @@ var _ = Describe("Packet packer", func() {
Offset: 1,
}
packer.AddStreamFrame(f)
payloadFrames, err := packer.composeNextPacket([]frames.Frame{})
payloadFrames, err := packer.composeNextPacket([]frames.Frame{}, true)
Expect(err).ToNot(HaveOccurred())
Expect(len(payloadFrames)).To(Equal(1))
payloadFrames, err = packer.composeNextPacket([]frames.Frame{})
payloadFrames, err = packer.composeNextPacket([]frames.Frame{}, true)
Expect(err).ToNot(HaveOccurred())
Expect(len(payloadFrames)).To(Equal(0))
})
@ -108,13 +127,13 @@ var _ = Describe("Packet packer", func() {
}
packer.AddStreamFrame(f1)
packer.AddStreamFrame(f2)
p, err := packer.PackPacket([]frames.Frame{})
p, err := packer.PackPacket([]frames.Frame{}, true)
Expect(err).ToNot(HaveOccurred())
Expect(len(p.raw)).To(Equal(protocol.MaxPacketSize))
p, err = packer.PackPacket([]frames.Frame{})
p, err = packer.PackPacket([]frames.Frame{}, true)
Expect(err).ToNot(HaveOccurred())
Expect(len(p.raw)).To(Equal(protocol.MaxPacketSize))
p, err = packer.PackPacket([]frames.Frame{})
p, err = packer.PackPacket([]frames.Frame{}, true)
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
})
@ -125,7 +144,7 @@ var _ = Describe("Packet packer", func() {
Offset: 1,
}
packer.AddStreamFrame(f)
p, err := packer.PackPacket([]frames.Frame{})
p, err := packer.PackPacket([]frames.Frame{}, true)
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
Expect(len(p.raw)).To(Equal(protocol.MaxPacketSize))
@ -137,10 +156,10 @@ var _ = Describe("Packet packer", func() {
Offset: 1,
}
packer.AddStreamFrame(f)
payloadFrames, err := packer.composeNextPacket([]frames.Frame{})
payloadFrames, err := packer.composeNextPacket([]frames.Frame{}, true)
Expect(err).ToNot(HaveOccurred())
Expect(len(payloadFrames)).To(Equal(1))
payloadFrames, err = packer.composeNextPacket([]frames.Frame{})
payloadFrames, err = packer.composeNextPacket([]frames.Frame{}, true)
Expect(err).ToNot(HaveOccurred())
Expect(len(payloadFrames)).To(Equal(1))
})