MCPcopy Create free account
hub / github.com/imroc/req / TestChunkReadMultiple

Function TestChunkReadMultiple

internal/chunked_test.go:42–107  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

40}
41
42func TestChunkReadMultiple(t *testing.T) {
43 // Bunch of small chunks, all read together.
44 {
45 var b bytes.Buffer
46 w := NewChunkedWriter(&b)
47 w.Write([]byte("foo"))
48 w.Write([]byte("bar"))
49 w.Close()
50
51 r := NewChunkedReader(&b)
52 buf := make([]byte, 10)
53 n, err := r.Read(buf)
54 if n != 6 || err != io.EOF {
55 t.Errorf("Read = %d, %v; want 6, EOF", n, err)
56 }
57 buf = buf[:n]
58 if string(buf) != "foobar" {
59 t.Errorf("Read = %q; want %q", buf, "foobar")
60 }
61 }
62
63 // One big chunk followed by a little chunk, but the small bufio.Reader size
64 // should prevent the second chunk header from being read.
65 {
66 var b bytes.Buffer
67 w := NewChunkedWriter(&b)
68 // fillBufChunk is 11 bytes + 3 bytes header + 2 bytes footer = 16 bytes,
69 // the same as the bufio ReaderSize below (the minimum), so even
70 // though we're going to try to Read with a buffer larger enough to also
71 // receive "foo", the second chunk header won't be read yet.
72 const fillBufChunk = "0123456789a"
73 const shortChunk = "foo"
74 w.Write([]byte(fillBufChunk))
75 w.Write([]byte(shortChunk))
76 w.Close()
77
78 r := NewChunkedReader(bufio.NewReaderSize(&b, 16))
79 buf := make([]byte, len(fillBufChunk)+len(shortChunk))
80 n, err := r.Read(buf)
81 if n != len(fillBufChunk) || err != nil {
82 t.Errorf("Read = %d, %v; want %d, nil", n, err, len(fillBufChunk))
83 }
84 buf = buf[:n]
85 if string(buf) != fillBufChunk {
86 t.Errorf("Read = %q; want %q", buf, fillBufChunk)
87 }
88
89 n, err = r.Read(buf)
90 if n != len(shortChunk) || err != io.EOF {
91 t.Errorf("Read = %d, %v; want %d, EOF", n, err, len(shortChunk))
92 }
93 }
94
95 // And test that we see an EOF chunk, even though our buffer is already full:
96 {
97 r := NewChunkedReader(bufio.NewReader(strings.NewReader("3\r\nfoo\r\n0\r\n")))
98 buf := make([]byte, 3)
99 n, err := r.Read(buf)

Callers

nothing calls this directly

Calls 6

NewChunkedWriterFunction · 0.85
NewChunkedReaderFunction · 0.85
WriteMethod · 0.65
ErrorfMethod · 0.65
CloseMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…