a7a57746885e3dd45e8badd2d2f623922ea020f9
[platform/upstream/gcc.git] / libgo / go / net / http / httputil / chunked_test.go
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.
4
5 // This code is duplicated in net/http and net/http/httputil.
6 // Please make any changes in both files.
7
8 package httputil
9
10 import (
11         "bufio"
12         "bytes"
13         "fmt"
14         "io"
15         "io/ioutil"
16         "strings"
17         "testing"
18 )
19
20 func TestChunk(t *testing.T) {
21         var b bytes.Buffer
22
23         w := newChunkedWriter(&b)
24         const chunk1 = "hello, "
25         const chunk2 = "world! 0123456789abcdef"
26         w.Write([]byte(chunk1))
27         w.Write([]byte(chunk2))
28         w.Close()
29
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)
32         }
33
34         r := newChunkedReader(&b)
35         data, err := ioutil.ReadAll(r)
36         if err != nil {
37                 t.Logf(`data: "%s"`, data)
38                 t.Fatalf("ReadAll from reader: %v", err)
39         }
40         if g, e := string(data), chunk1+chunk2; g != e {
41                 t.Errorf("chunk reader read %q; want %q", g, e)
42         }
43 }
44
45 func TestChunkReadMultiple(t *testing.T) {
46         // Bunch of small chunks, all read together.
47         {
48                 var b bytes.Buffer
49                 w := newChunkedWriter(&b)
50                 w.Write([]byte("foo"))
51                 w.Write([]byte("bar"))
52                 w.Close()
53
54                 r := newChunkedReader(&b)
55                 buf := make([]byte, 10)
56                 n, err := r.Read(buf)
57                 if n != 6 || err != io.EOF {
58                         t.Errorf("Read = %d, %v; want 6, EOF", n, err)
59                 }
60                 buf = buf[:n]
61                 if string(buf) != "foobar" {
62                         t.Errorf("Read = %q; want %q", buf, "foobar")
63                 }
64         }
65
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.
68         {
69                 var b bytes.Buffer
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))
79                 w.Close()
80
81                 r := newChunkedReader(bufio.NewReaderSize(&b, 16))
82                 buf := make([]byte, len(fillBufChunk)+len(shortChunk))
83                 n, err := r.Read(buf)
84                 if n != len(fillBufChunk) || err != nil {
85                         t.Errorf("Read = %d, %v; want %d, nil", n, err, len(fillBufChunk))
86                 }
87                 buf = buf[:n]
88                 if string(buf) != fillBufChunk {
89                         t.Errorf("Read = %q; want %q", buf, fillBufChunk)
90                 }
91
92                 n, err = r.Read(buf)
93                 if n != len(shortChunk) || err != io.EOF {
94                         t.Errorf("Read = %d, %v; want %d, EOF", n, err, len(shortChunk))
95                 }
96         }
97
98         // And test that we see an EOF chunk, even though our buffer is already full:
99         {
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)
105                 }
106                 if string(buf) != "foo" {
107                         t.Errorf("buf = %q; want foo", buf)
108                 }
109         }
110 }
111
112 func TestChunkReaderAllocs(t *testing.T) {
113         if testing.Short() {
114                 t.Skip("skipping in short mode")
115         }
116         var buf bytes.Buffer
117         w := newChunkedWriter(&buf)
118         a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
119         w.Write(a)
120         w.Write(b)
121         w.Write(c)
122         w.Close()
123
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() {
128                 byter.Seek(0, 0)
129                 bufr.Reset(byter)
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)
134                 }
135                 if err != io.ErrUnexpectedEOF {
136                         t.Fatalf("read error = %v; want ErrUnexpectedEOF", err)
137                 }
138         })
139         if mallocs > 1.5 {
140                 t.Errorf("mallocs = %v; want 1", mallocs)
141         }
142 }
143
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)
148                 if err != nil {
149                         t.Fatalf("on %d: %v", i, err)
150                 }
151                 if got != i {
152                         t.Errorf("for input %q = %d; want %d", line, got, i)
153                 }
154         }
155         _, err := parseHexUint([]byte("bogus"))
156         if err == nil {
157                 t.Error("expected error on bogus input")
158         }
159 }