...
Run Format

Source file src/net/http/internal/chunked_test.go

Documentation: net/http/internal

     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  package internal
     6  
     7  import (
     8  	"bufio"
     9  	"bytes"
    10  	"fmt"
    11  	"io"
    12  	"io/ioutil"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  func TestChunk(t *testing.T) {
    18  	var b bytes.Buffer
    19  
    20  	w := NewChunkedWriter(&b)
    21  	const chunk1 = "hello, "
    22  	const chunk2 = "world! 0123456789abcdef"
    23  	w.Write([]byte(chunk1))
    24  	w.Write([]byte(chunk2))
    25  	w.Close()
    26  
    27  	if g, e := b.String(), "7\r\nhello, \r\n17\r\nworld! 0123456789abcdef\r\n0\r\n"; g != e {
    28  		t.Fatalf("chunk writer wrote %q; want %q", g, e)
    29  	}
    30  
    31  	r := NewChunkedReader(&b)
    32  	data, err := ioutil.ReadAll(r)
    33  	if err != nil {
    34  		t.Logf(`data: "%s"`, data)
    35  		t.Fatalf("ReadAll from reader: %v", err)
    36  	}
    37  	if g, e := string(data), chunk1+chunk2; g != e {
    38  		t.Errorf("chunk reader read %q; want %q", g, e)
    39  	}
    40  }
    41  
    42  func 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)
   100  		if n != 3 || err != io.EOF {
   101  			t.Errorf("Read = %d, %v; want 3, EOF", n, err)
   102  		}
   103  		if string(buf) != "foo" {
   104  			t.Errorf("buf = %q; want foo", buf)
   105  		}
   106  	}
   107  }
   108  
   109  func TestChunkReaderAllocs(t *testing.T) {
   110  	if testing.Short() {
   111  		t.Skip("skipping in short mode")
   112  	}
   113  	var buf bytes.Buffer
   114  	w := NewChunkedWriter(&buf)
   115  	a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
   116  	w.Write(a)
   117  	w.Write(b)
   118  	w.Write(c)
   119  	w.Close()
   120  
   121  	readBuf := make([]byte, len(a)+len(b)+len(c)+1)
   122  	byter := bytes.NewReader(buf.Bytes())
   123  	bufr := bufio.NewReader(byter)
   124  	mallocs := testing.AllocsPerRun(100, func() {
   125  		byter.Seek(0, io.SeekStart)
   126  		bufr.Reset(byter)
   127  		r := NewChunkedReader(bufr)
   128  		n, err := io.ReadFull(r, readBuf)
   129  		if n != len(readBuf)-1 {
   130  			t.Fatalf("read %d bytes; want %d", n, len(readBuf)-1)
   131  		}
   132  		if err != io.ErrUnexpectedEOF {
   133  			t.Fatalf("read error = %v; want ErrUnexpectedEOF", err)
   134  		}
   135  	})
   136  	if mallocs > 1.5 {
   137  		t.Errorf("mallocs = %v; want 1", mallocs)
   138  	}
   139  }
   140  
   141  func TestParseHexUint(t *testing.T) {
   142  	type testCase struct {
   143  		in      string
   144  		want    uint64
   145  		wantErr string
   146  	}
   147  	tests := []testCase{
   148  		{"x", 0, "invalid byte in chunk length"},
   149  		{"0000000000000000", 0, ""},
   150  		{"0000000000000001", 1, ""},
   151  		{"ffffffffffffffff", 1<<64 - 1, ""},
   152  		{"000000000000bogus", 0, "invalid byte in chunk length"},
   153  		{"00000000000000000", 0, "http chunk length too large"}, // could accept if we wanted
   154  		{"10000000000000000", 0, "http chunk length too large"},
   155  		{"00000000000000001", 0, "http chunk length too large"}, // could accept if we wanted
   156  	}
   157  	for i := uint64(0); i <= 1234; i++ {
   158  		tests = append(tests, testCase{in: fmt.Sprintf("%x", i), want: i})
   159  	}
   160  	for _, tt := range tests {
   161  		got, err := parseHexUint([]byte(tt.in))
   162  		if tt.wantErr != "" {
   163  			if !strings.Contains(fmt.Sprint(err), tt.wantErr) {
   164  				t.Errorf("parseHexUint(%q) = %v, %v; want error %q", tt.in, got, err, tt.wantErr)
   165  			}
   166  		} else {
   167  			if err != nil || got != tt.want {
   168  				t.Errorf("parseHexUint(%q) = %v, %v; want %v", tt.in, got, err, tt.want)
   169  			}
   170  		}
   171  	}
   172  }
   173  
   174  func TestChunkReadingIgnoresExtensions(t *testing.T) {
   175  	in := "7;ext=\"some quoted string\"\r\n" + // token=quoted string
   176  		"hello, \r\n" +
   177  		"17;someext\r\n" + // token without value
   178  		"world! 0123456789abcdef\r\n" +
   179  		"0;someextension=sometoken\r\n" // token=token
   180  	data, err := ioutil.ReadAll(NewChunkedReader(strings.NewReader(in)))
   181  	if err != nil {
   182  		t.Fatalf("ReadAll = %q, %v", data, err)
   183  	}
   184  	if g, e := string(data), "hello, world! 0123456789abcdef"; g != e {
   185  		t.Errorf("read %q; want %q", g, e)
   186  	}
   187  }
   188  
   189  // Issue 17355: ChunkedReader shouldn't block waiting for more data
   190  // if it can return something.
   191  func TestChunkReadPartial(t *testing.T) {
   192  	pr, pw := io.Pipe()
   193  	go func() {
   194  		pw.Write([]byte("7\r\n1234567"))
   195  	}()
   196  	cr := NewChunkedReader(pr)
   197  	readBuf := make([]byte, 7)
   198  	n, err := cr.Read(readBuf)
   199  	if err != nil {
   200  		t.Fatal(err)
   201  	}
   202  	want := "1234567"
   203  	if n != 7 || string(readBuf) != want {
   204  		t.Fatalf("Read: %v %q; want %d, %q", n, readBuf[:n], len(want), want)
   205  	}
   206  	go func() {
   207  		pw.Write([]byte("xx"))
   208  	}()
   209  	_, err = cr.Read(readBuf)
   210  	if got := fmt.Sprint(err); !strings.Contains(got, "malformed") {
   211  		t.Fatalf("second read = %v; want malformed error", err)
   212  	}
   213  
   214  }
   215  

View as plain text