1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This code is duplicated in net/http and net/http/httputil.
6 // Please make any changes in both files.
20 func TestChunk(t *testing.T) {
23 w := newChunkedWriter(&b)
24 const chunk1 = "hello, "
25 const chunk2 = "world! 0123456789abcdef"
26 w.Write([]byte(chunk1))
27 w.Write([]byte(chunk2))
30 if g, e := b.String(), "7\r\nhello, \r\n17\r\nworld! 0123456789abcdef\r\n0\r\n"; g != e {
31 t.Fatalf("chunk writer wrote %q; want %q", g, e)
34 r := newChunkedReader(&b)
35 data, err := ioutil.ReadAll(r)
37 t.Logf(`data: "%s"`, data)
38 t.Fatalf("ReadAll from reader: %v", err)
40 if g, e := string(data), chunk1+chunk2; g != e {
41 t.Errorf("chunk reader read %q; want %q", g, e)
45 func TestChunkReadMultiple(t *testing.T) {
46 // Bunch of small chunks, all read together.
49 w := newChunkedWriter(&b)
50 w.Write([]byte("foo"))
51 w.Write([]byte("bar"))
54 r := newChunkedReader(&b)
55 buf := make([]byte, 10)
57 if n != 6 || err != io.EOF {
58 t.Errorf("Read = %d, %v; want 6, EOF", n, err)
61 if string(buf) != "foobar" {
62 t.Errorf("Read = %q; want %q", buf, "foobar")
66 // One big chunk followed by a little chunk, but the small bufio.Reader size
67 // should prevent the second chunk header from being read.
70 w := newChunkedWriter(&b)
71 // fillBufChunk is 11 bytes + 3 bytes header + 2 bytes footer = 16 bytes,
72 // the same as the bufio ReaderSize below (the minimum), so even
73 // though we're going to try to Read with a buffer larger enough to also
74 // receive "foo", the second chunk header won't be read yet.
75 const fillBufChunk = "0123456789a"
76 const shortChunk = "foo"
77 w.Write([]byte(fillBufChunk))
78 w.Write([]byte(shortChunk))
81 r := newChunkedReader(bufio.NewReaderSize(&b, 16))
82 buf := make([]byte, len(fillBufChunk)+len(shortChunk))
84 if n != len(fillBufChunk) || err != nil {
85 t.Errorf("Read = %d, %v; want %d, nil", n, err, len(fillBufChunk))
88 if string(buf) != fillBufChunk {
89 t.Errorf("Read = %q; want %q", buf, fillBufChunk)
93 if n != len(shortChunk) || err != io.EOF {
94 t.Errorf("Read = %d, %v; want %d, EOF", n, err, len(shortChunk))
98 // And test that we see an EOF chunk, even though our buffer is already full:
100 r := newChunkedReader(bufio.NewReader(strings.NewReader("3\r\nfoo\r\n0\r\n")))
101 buf := make([]byte, 3)
102 n, err := r.Read(buf)
103 if n != 3 || err != io.EOF {
104 t.Errorf("Read = %d, %v; want 3, EOF", n, err)
106 if string(buf) != "foo" {
107 t.Errorf("buf = %q; want foo", buf)
112 func TestChunkReaderAllocs(t *testing.T) {
114 t.Skip("skipping in short mode")
117 w := newChunkedWriter(&buf)
118 a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
124 readBuf := make([]byte, len(a)+len(b)+len(c)+1)
125 byter := bytes.NewReader(buf.Bytes())
126 bufr := bufio.NewReader(byter)
127 mallocs := testing.AllocsPerRun(100, func() {
130 r := newChunkedReader(bufr)
131 n, err := io.ReadFull(r, readBuf)
132 if n != len(readBuf)-1 {
133 t.Fatalf("read %d bytes; want %d", n, len(readBuf)-1)
135 if err != io.ErrUnexpectedEOF {
136 t.Fatalf("read error = %v; want ErrUnexpectedEOF", err)
140 t.Errorf("mallocs = %v; want 1", mallocs)
144 func TestParseHexUint(t *testing.T) {
145 for i := uint64(0); i <= 1234; i++ {
146 line := []byte(fmt.Sprintf("%x", i))
147 got, err := parseHexUint(line)
149 t.Fatalf("on %d: %v", i, err)
152 t.Errorf("for input %q = %d; want %d", line, got, i)
155 _, err := parseHexUint([]byte("bogus"))
157 t.Error("expected error on bogus input")